-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathStudentRepository.cpp
More file actions
97 lines (87 loc) · 2.99 KB
/
StudentRepository.cpp
File metadata and controls
97 lines (87 loc) · 2.99 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//
// Created by mwypych on 28/05/18.
//
#include <utility>
#include <memory>
#include <vector>
#include <stdexcept>
#include <algorithm>
#include <domain/Student.h>
#include <infrastructure/IdGenerator.h>
#include <infrastructure/StudentRepository.h>
using ::std::move;
using ::std::transform;
using ::std::to_string;
using ::std::invalid_argument;
using ::std::runtime_error;
using ::std::reference_wrapper;
using ::std::vector;
using ::std::experimental::make_optional;
StudentRepository::StudentRepository(std::unique_ptr<IdGenerator> id_generator)
: id_gen_{move(id_generator)},
students_{} {}
void StudentRepository::Insert(const Student &student) {
if (student.Id() != Student::UNKNOWN_ID) {
throw invalid_argument{"When inserting new Student into repository its id shouldn't be set!"};
}
StudentId new_id = NextId();
//FIXME: concurrent access - not safe!
auto hint = students_.find(new_id);
Student
new_student{new_id, student.FirstName(), student.LastName(), student.Program(), student.Age(), student.Pesel()};
students_.emplace_hint(hint, new_id, move(new_student));
}
void StudentRepository::Update(unsigned int id, const Student &student) {
if (student.Id() != id) {
throw invalid_argument{"One cannot modify id of the updated student!"};
}
//FIXME: concurrent access - not safe!
auto found = students_.find(id);
if (found != students_.end()) {
Student &to_update = found->second;
to_update = student;
} else {
throw runtime_error{"There is no Student in the repository with id " + to_string(id)};
}
}
StudentRepository::OptionalStudent StudentRepository::FindById(StudentRepository::StudentId id) const {
//FIXME: concurrent access - not safe!
auto found = students_.find(id);
if (found != students_.end()) {
return make_optional(found->second);
} else {
return {};
}
}
StudentRepository::OptionalMutableStudent StudentRepository::FindById(StudentRepository::StudentId id) {
//FIXME: concurrent access - not safe!
auto found = students_.find(id);
if (found != students_.end()) {
Student &to_return = found->second;
return make_optional<reference_wrapper<Student>>(to_return);
} else {
return {};
}
}
void StudentRepository::Delete(StudentRepository::StudentId id) {
if (id == Student::UNKNOWN_ID) {
throw invalid_argument{"When deleting Student from repository its id should be set!"};
}
//FIXME: concurrent access - not safe!
auto found = students_.find(id);
if (found != students_.end()) {
students_.erase(found);
} else {
throw runtime_error{"There is no Student in the repository with id " + to_string(id)};
}
}
std::vector<Student> StudentRepository::All() const {
//FIXME: concurrent access - not safe!
vector<Student> response{};
response.reserve(students_.size());
transform(students_.cbegin(), students_.cend(), back_inserter(response), [](const auto &p) { return p.second; });
return response;
}
StudentRepository::StudentId StudentRepository::NextId() const {
return id_gen_->NextId();
}