-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathStudentController.cpp
More file actions
77 lines (69 loc) · 2.2 KB
/
StudentController.cpp
File metadata and controls
77 lines (69 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//
// Created by mwypych on 28/05/18.
//
#include <utility>
#include <memory>
#include <crow_all.h>
#include <domain/Student.h>
#include <infrastructure/StudentRepository.h>
#include <infrastructure/StudentController.h>
namespace json = ::crow::json;
using crow::request;
using crow::response;
using std::move;
StudentController::StudentController(std::unique_ptr<StudentRepository> repository)
: repository_{move(repository)} {}
crow::response StudentController::CreateStudent(const request &req) {
auto x = json::load(req.body);
if (!x)
return response(400);
CROW_LOG_INFO << " - MESSAGE: " << x;
Student s = Student::StudentFromJson(x);
repository_->Insert(s);
return response(204);
}
crow::response StudentController::DeleteStudent(unsigned int id) {
auto found = repository_->FindById(id);
if (found) {
repository_->Delete(id);
CROW_LOG_INFO << " - MESSAGE: student of id: " << id << " was found";
return response(204);
} else {
CROW_LOG_INFO << " - MESSAGE: student of id: " << id << " was not found";
return response(404);
}
}
crow::response StudentController::SingleStudent(unsigned int id) {
auto found = repository_->FindById(id);
if (found) {
json::wvalue student;
student = static_cast<Student &>(*found);
CROW_LOG_INFO << " - MESSAGE: student of id: " << id << " was found";
return response(student);
} else {
CROW_LOG_INFO << " - MESSAGE: student of id: " << id << " was not found";
return response(404);
}
}
crow::response StudentController::UpdateStudent(const crow::request &req, unsigned int id) {
auto x = json::load(req.body);
if (!x)
return response(400);
CROW_LOG_INFO << " - MESSAGE: " << x;
Student s = Student::StudentFromJson(x);
if (repository_->FindById(id)) {
repository_->Update(id, s);
CROW_LOG_INFO << " - MESSAGE: student of id: " << id << " was found";
return response(204);
} else {
CROW_LOG_INFO << " - MESSAGE: student of id: " << id << " was not found";
return response(404);
}
}
crow::response StudentController::AllStudents() {
json::wvalue x;
x = repository_->All();
std::string json_message = json::dump(x);
CROW_LOG_INFO << " - MESSAGE: " << json_message;
return response(x);
}