Skip to content

Commit 2b356d0

Browse files
committed
wip full molecule GET single molecule endpoint
1 parent fb994fc commit 2b356d0

File tree

6 files changed

+150
-2
lines changed

6 files changed

+150
-2
lines changed

resources/config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
"host": "0.0.0.0",
44
"port": 8000,
55
"swaggerHost": "localhost:8000",
6-
"dbConnectionString": "postgresql://postgres:db-pass@localhost:5432/postgres"
6+
"dbConnectionString": "postgresql://postgres:db-pass@localhost:5432/molecules"
77
},
88
"local-docker": {
99
"host": "0.0.0.0",
1010
"port": 8000,
1111
"swaggerHost": "localhost:8000",
12-
"dbConnectionString": "postgresql://postgres:db-pass@db:5432/postgres"
12+
"dbConnectionString": "postgresql://postgres:db-pass@db:5432/molecules"
1313
}
1414
}

src/controller/MoleculeController.hpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
#ifndef MoleculeController_hpp
3+
#define MoleculeController_hpp
4+
5+
#include "service/MoleculeService.hpp"
6+
7+
#include "oatpp/web/server/api/ApiController.hpp"
8+
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
9+
#include "oatpp/core/macro/codegen.hpp"
10+
11+
#include OATPP_CODEGEN_BEGIN(ApiController) //<- Begin Codegen
12+
13+
/**
14+
* User REST controller.
15+
*/
16+
class MoleculeController : public oatpp::web::server::api::ApiController {
17+
public:
18+
MoleculeController(const std::shared_ptr<ObjectMapper>& objectMapper)
19+
: oatpp::web::server::api::ApiController(objectMapper)
20+
{}
21+
private:
22+
MoleculeService m_moleculeService; // Create user service.
23+
public:
24+
25+
static std::shared_ptr<MoleculeController> createShared(
26+
OATPP_COMPONENT(std::shared_ptr<ObjectMapper>, objectMapper) // Inject objectMapper component here as default parameter
27+
){
28+
return std::make_shared<MoleculeController>(objectMapper);
29+
}
30+
31+
ENDPOINT_INFO(getMoleculeById) {
32+
info->summary = "Get one molecule by molecule id";
33+
34+
info->addResponse<Object<UserDto>>(Status::CODE_200, "application/json");
35+
info->addResponse<Object<StatusDto>>(Status::CODE_404, "application/json");
36+
info->addResponse<Object<StatusDto>>(Status::CODE_500, "application/json");
37+
38+
info->pathParams["moleculeId"].description = "Molecule Identifier";
39+
}
40+
ENDPOINT("GET", "users/{moleculeId}", getMoleculeById,
41+
PATH(String, moleculeId))
42+
{
43+
return createDtoResponse(Status::CODE_200, m_moleculeService.getMoleculeById(userId));
44+
}
45+
};
46+
47+
#include OATPP_CODEGEN_BEGIN(ApiController) //<- End Codegen
48+
49+
#endif /* MoleculeController_hpp */

src/db/MoleculeDb.hpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
#ifndef EXAMPLE_POSTGRESQL_MOLECULEDB_HPP
3+
#define EXAMPLE_POSTGRESQL_MOLECULEDB_HPP
4+
5+
#include "dto/MoleculeDto.hpp"
6+
#include "oatpp-postgresql/orm.hpp"
7+
8+
#include OATPP_CODEGEN_BEGIN(DbClient) //<- Begin Codegen
9+
10+
/**
11+
* MoleculeDb client definitions.
12+
*/
13+
class MoleculeDb : public oatpp::orm::DbClient {
14+
public:
15+
16+
MoleculeDb(const std::shared_ptr<oatpp::orm::Executor>& executor)
17+
: oatpp::orm::DbClient(executor)
18+
{
19+
20+
oatpp::orm::SchemaMigration migration(executor);
21+
// We use a pre-filled, read-only database, therefore no migration files needed
22+
// migration.addFile(1 /* start from version 1 */, DATABASE_MIGRATIONS "/001_init.sql");
23+
// TODO - Add more migrations here.
24+
// migration.migrate(); // <-- run migrations. This guy will throw on error.
25+
26+
auto version = executor->getSchemaVersion();
27+
OATPP_LOGD("MoleculeDb", "Migration - OK. Version=%d.", version);
28+
29+
}
30+
31+
32+
QUERY(getMoleculeById,
33+
"SELECT id, m::text as smiles from mols where id=:id;",
34+
PREPARE(true), //<-- user prepared statement!
35+
PARAM(oatpp::String, id))
36+
37+
};
38+
39+
#include OATPP_CODEGEN_END(DbClient) //<- End Codegen
40+
41+
#endif //EXAMPLE_POSTGRESQL_MOLECULEDB_HPP

src/dto/MoleculeDto.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef MoleculeDto_hpp
2+
#define MoleculeDto_hpp
3+
4+
#include "oatpp/core/macro/codegen.hpp"
5+
#include "oatpp/core/Types.hpp"
6+
7+
#include OATPP_CODEGEN_BEGIN(DTO)
8+
9+
class MoleculeDto : public oatpp::DTO {
10+
11+
DTO_INIT(MoleculeDto, DTO)
12+
13+
DTO_FIELD(String, id);
14+
DTO_FIELD(String, smiles, "smiles");
15+
};
16+
17+
#include OATPP_CODEGEN_END(DTO)
18+
19+
#endif /* MoleculeDto_hpp */

src/service/MoleculeService.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
#include "MoleculeService.hpp"
3+
4+
oatpp::Object<UserDto>
5+
MoleculeService::getMoleculeById(const oatpp::String &id) {
6+
7+
auto dbResult = m_database->getMoleculeById(id);
8+
OATPP_ASSERT_HTTP(dbResult->isSuccess(), Status::CODE_500,
9+
dbResult->getErrorMessage());
10+
OATPP_ASSERT_HTTP(dbResult->hasMoreToFetch(), Status::CODE_404,
11+
"User not found");
12+
13+
auto result = dbResult->fetch<oatpp::Vector<oatpp::Object<UserDto>>>();
14+
OATPP_ASSERT_HTTP(result->size() == 1, Status::CODE_500, "Unknown error");
15+
16+
return result[0];
17+
}

src/service/MoleculeService.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
#ifndef EXAMPLE_POSTGRESQL_MOLECULESERVICE_HPP
3+
#define EXAMPLE_POSTGRESQL_MOLECULESERVICE_HPP
4+
5+
#include "db/UserDb.hpp"
6+
#include "dto/PageDto.hpp"
7+
#include "dto/StatusDto.hpp"
8+
9+
#include "oatpp/web/protocol/http/Http.hpp"
10+
#include "oatpp/core/macro/component.hpp"
11+
12+
class MoleculeService {
13+
private:
14+
typedef oatpp::web::protocol::http::Status Status;
15+
private:
16+
OATPP_COMPONENT(std::shared_ptr<UserDb>, m_database); // Inject database component
17+
public:
18+
19+
oatpp::Object<UserDto> getMoleculeById(const oatpp::String& id);
20+
};
21+
22+
#endif //EXAMPLE_POSTGRESQL_MOLECULESERVICE_HPP

0 commit comments

Comments
 (0)