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}
0 commit comments