Skip to content

Commit c417ef1

Browse files
committed
feat(connection): #28: expand controller constructor with service creation
1 parent 30f44b1 commit c417ef1

2 files changed

Lines changed: 76 additions & 53 deletions

File tree

src/controllers/app-controller.cpp

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,53 @@
11
#include "app-controller.h"
2-
#include "services/to-dos.service.h"
2+
#include "data/db_connection.h"
33

44
// TODO(https://github.com/TourmalineCore/to-dos-api-cpp/issues/25): Create http exception handler or generic class for handling that type of errors
5-
HttpResponsePtr AppController::createInternalServerErrorResponse(const std::string& error) const
6-
{
5+
HttpResponsePtr AppController::createInternalServerErrorResponse(const std::string& error) const
6+
{
77
Json::Value jsonResponse;
88
jsonResponse["status"] = "Error";
99
jsonResponse["message"] = "Internal server error";
1010
jsonResponse["error"] = error;
11-
11+
1212
auto resp = HttpResponse::newHttpJsonResponse(jsonResponse);
1313
resp->setStatusCode(k500InternalServerError);
1414
return resp;
1515
}
1616

17-
void AppController::getToDos(const HttpRequestPtr& req,
18-
std::function<void(const HttpResponsePtr&)>&& callback) {
19-
Json::Value jsonResponse;
17+
AppController::AppController()
18+
{
19+
db_ = std::move(DbConnection::get());
20+
queries_ = std::make_unique<ToDoQueries>(*db_);
21+
commands_ = std::make_unique<ToDoCommands>(*db_);
22+
todo_service_ = std::make_unique<ToDoService>(*queries_, *commands_);
23+
}
2024

21-
try {
22-
ToDoService toDos;
25+
void AppController::getToDos(const HttpRequestPtr& req, std::function<void(const HttpResponsePtr&)>&& callback)
26+
{
27+
Json::Value jsonResponse;
2328

24-
auto resp = HttpResponse::newHttpJsonResponse(toDos.toJson());
29+
try
30+
{
31+
auto resp = HttpResponse::newHttpJsonResponse(todo_service_->toJson());
2532
resp->setStatusCode(k200OK);
2633
callback(resp);
27-
28-
} catch (const std::exception& e) {
34+
}
35+
catch (const std::exception& e)
36+
{
2937
callback(createInternalServerErrorResponse(e.what()));
3038
}
3139
}
3240

33-
void AppController::addToDo(const HttpRequestPtr& req,
34-
std::function<void(const HttpResponsePtr&)>&& callback) {
41+
void AppController::addToDo(const HttpRequestPtr& req, std::function<void(const HttpResponsePtr&)>&& callback)
42+
{
3543
Json::Value jsonResponse;
3644

37-
try {
45+
try
46+
{
3847
auto json = req->getJsonObject();
3948

40-
if (!json || !json->isMember("description")) {
49+
if (!json || !json->isMember("description"))
50+
{
4151
Json::Value result;
4252
result["status"] = "error";
4353
result["message"] = "Invalid JSON";
@@ -49,26 +59,28 @@ void AppController::addToDo(const HttpRequestPtr& req,
4959
return;
5060
}
5161

52-
ToDoService toDos;
53-
toDos.addToDo(json->get("description", "").asString());
62+
todo_service_->addToDo(json->get("description", "").asString());
5463

5564
auto resp = HttpResponse::newHttpResponse();
5665
resp->setStatusCode(k201Created);
5766
callback(resp);
58-
59-
} catch (const std::exception& e) {
67+
}
68+
catch (const std::exception& e)
69+
{
6070
callback(createInternalServerErrorResponse(e.what()));
6171
}
6272
}
6373

64-
void AppController::completeToDos(const HttpRequestPtr& req,
65-
std::function<void(const HttpResponsePtr&)>&& callback) {
74+
void AppController::completeToDos(const HttpRequestPtr& req, std::function<void(const HttpResponsePtr&)>&& callback)
75+
{
6676
Json::Value jsonResponse;
6777

68-
try {
78+
try
79+
{
6980
auto json = req->getJsonObject();
7081

71-
if (!json || !json->isMember("toDosIds")) {
82+
if (!json || !json->isMember("toDosIds"))
83+
{
7284
Json::Value result;
7385
result["status"] = "error";
7486
result["message"] = "Invalid JSON";
@@ -80,38 +92,40 @@ void AppController::completeToDos(const HttpRequestPtr& req,
8092
return;
8193
}
8294

83-
ToDoService toDos;
8495
auto toDosIds = json->get("toDosIds", Json::Value(Json::arrayValue));
8596

86-
for (const auto& id : toDosIds) {
87-
toDos.completeToDo(id.asInt());
97+
for (const auto& id : toDosIds)
98+
{
99+
todo_service_->completeToDo(id.asInt());
88100
}
89101

90102
auto resp = HttpResponse::newHttpResponse();
91103
resp->setStatusCode(k200OK);
92104
callback(resp);
93-
94-
} catch (const std::exception& e) {
105+
}
106+
catch (const std::exception& e)
107+
{
95108
callback(createInternalServerErrorResponse(e.what()));
96109
}
97110
}
98111

99-
void AppController::deleteToDo(const HttpRequestPtr& req,
100-
std::function<void(const HttpResponsePtr&)>&& callback) {
112+
void AppController::deleteToDo(const HttpRequestPtr& req, std::function<void(const HttpResponsePtr&)>&& callback)
113+
{
101114
Json::Value jsonResponse;
102115

103-
try {
116+
try
117+
{
104118
auto toDoIdStr = req->getParameter("toDoId");
105119
int toDoId = std::stoi(toDoIdStr);
106-
107-
ToDoService toDos;
108-
toDos.completeToDo(toDoId);
120+
121+
todo_service_->completeToDo(toDoId);
109122

110123
auto resp = HttpResponse::newHttpResponse();
111124
resp->setStatusCode(k200OK);
112125
callback(resp);
113-
114-
} catch (const std::exception& e) {
126+
}
127+
catch (const std::exception& e)
128+
{
115129
callback(createInternalServerErrorResponse(e.what()));
116130
}
117131
}

src/controllers/app-controller.h

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,37 @@
22

33
#include <drogon/HttpController.h>
44

5+
#include "data/commands/todo-commands.h"
6+
#include "data/queries/todo-queries.h"
7+
#include "services/to-dos.service.h"
8+
59
using namespace drogon;
610

7-
class AppController : public drogon::HttpController<AppController> {
11+
class AppController : public drogon::HttpController<AppController>
12+
{
813
public:
14+
explicit AppController();
15+
916
METHOD_LIST_BEGIN
10-
ADD_METHOD_TO(AppController::getToDos, "/to-dos", Get); // Getting a list of tasks
11-
ADD_METHOD_TO(AppController::addToDo, "/to-dos", Post); // Adding a new task
12-
ADD_METHOD_TO(AppController::completeToDos, "/to-dos/complete", Post); // Executing (deleting) a task list
13-
ADD_METHOD_TO(AppController::deleteToDo, "/to-dos", Delete); // Deleting a specific task
17+
ADD_METHOD_TO(AppController::getToDos, "/to-dos", Get); // Getting a list of tasks
18+
ADD_METHOD_TO(AppController::addToDo, "/to-dos", Post); // Adding a new task
19+
ADD_METHOD_TO(AppController::completeToDos, "/to-dos/complete", Post); // Executing (deleting) a task list
20+
ADD_METHOD_TO(AppController::deleteToDo, "/to-dos", Delete); // Deleting a specific task
1421
METHOD_LIST_END
1522

1623
HttpResponsePtr createInternalServerErrorResponse(const std::string& error) const;
1724

18-
void getToDos(const HttpRequestPtr& req,
19-
std::function<void(const HttpResponsePtr&)>&& callback);
20-
21-
void addToDo(const HttpRequestPtr& req,
22-
std::function<void(const HttpResponsePtr&)>&& callback);
23-
24-
void completeToDos(const HttpRequestPtr& req,
25-
std::function<void(const HttpResponsePtr&)>&& callback);
26-
27-
void deleteToDo(const HttpRequestPtr& req,
28-
std::function<void(const HttpResponsePtr&)>&& callback);
25+
void getToDos(const HttpRequestPtr& req, std::function<void(const HttpResponsePtr&)>&& callback);
26+
27+
void addToDo(const HttpRequestPtr& req, std::function<void(const HttpResponsePtr&)>&& callback);
28+
29+
void completeToDos(const HttpRequestPtr& req, std::function<void(const HttpResponsePtr&)>&& callback);
30+
31+
void deleteToDo(const HttpRequestPtr& req, std::function<void(const HttpResponsePtr&)>&& callback);
32+
33+
private:
34+
std::shared_ptr<odb::database> db_;
35+
std::unique_ptr<ToDoQueries> queries_;
36+
std::unique_ptr<ToDoCommands> commands_;
37+
std::unique_ptr<ToDoService> todo_service_;
2938
};

0 commit comments

Comments
 (0)