-
Notifications
You must be signed in to change notification settings - Fork 0
/
Service.cpp
188 lines (170 loc) · 5.16 KB
/
Service.cpp
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "Service.h"
#include <vector>
#include "victimValidator.h"
#include "Exceptions.h"
#include "ActionAdd.h"
#include "Action.h"
#include "ActionDelete.h"
#include "ActionUpdate.h"
#include "ActionSave.h"
using namespace std;
std::vector<Victim> Service::getAllVictimsInRepo() const
{
return this->repo.getVictims();
}
void Service::addVictimToRepository(const std::string& name, const std::string& placeOfOrigin, const int age, const std::string& photo)
{
Victim v{ name, placeOfOrigin, age, photo };
VictimValidator validator{};
validator.validateVictim(v);
if (this->repo.findVictimByName(name) != Victim{})
throw ServiceException("Victim already added!");
ActionAdd action{ this->repo, v };
std::unique_ptr<Action> pAdd = std::make_unique<ActionAdd>(action);
if (!this->undo_redo) {
this->undoStack.push_back(move(pAdd));
//reinitialize redo stack;
this->redoStack = vector<unique_ptr<Action>>();
}
this->repo.addVictim(v);
}
void Service::deleteVictimFromRepo(const std::string& name)
{
if (name == "")
throw ValidatorException("Empty name field! \n");
if (this->repo.findVictimByName(name) == Victim{})
throw ServiceException("Victim not existent! \n");
Victim vict = this->repo.findVictimByName(name);
ActionDelete action{ this->repo, vict };
unique_ptr<Action> pRemove = make_unique<ActionDelete>(action);
if (!this->undo_redo) {
this->undoStack.push_back(move(pRemove));
this->redoStack = vector<unique_ptr<Action>>();
}
this->repo.deleteVictim(name);
this->transferList.deleteFromTransferList(vict);
}
void Service::updateVictimFromRepo(const std::string& name, const std::string& newPlaceOfOrigin, const int newAge, const std::string& newPhoto)
{
if (name == "" || newPlaceOfOrigin == "" || newAge <=0 || newPhoto== "")
throw ValidatorException("Incorrect Input! \n");
if (this->repo.findVictimByName(name) == Victim{})
throw ServiceException("Victim not existent! \n");
Victim newVict = Victim{ name, newPlaceOfOrigin, newAge ,newPhoto };
Victim vict = this->repo.findVictimByName(name);
ActionUpdate action { this->repo, vict, newVict };
unique_ptr<Action> pUpdate = make_unique<ActionUpdate>(action);
if (!this->undo_redo) {
this->undoStack.push_back(move(pUpdate));
this->redoStack = vector<unique_ptr<Action>>();
}
this->repo.updateVictim(name, newPlaceOfOrigin, newAge, newPhoto);
}
std::vector<Victim> Service::filteredList(std::string place, int age)
{
std::vector<Victim> victims{};
for (auto& v : this->getAllVictimsInRepo()) {
if (v.getPlaceOfOrigin() == place && v.getAge() < age)
victims.push_back(v);
}
return victims;
}
void Service::getTransferListApp() const
{
this->transferList.getAll();
}
void Service::addToTransferListService(const std::string& name)
{
if (name == "")
throw ServiceException("Empty name! \n");
Victim victim = this->repo.findVictimByName(name);
VictimValidator validator{};
validator.validateVictim(victim);
if (victim == Victim{})
throw ServiceException("Victim not existent! \n");
ActionSave action{ this->transferList, victim };
std::unique_ptr<Action> pSave = std::make_unique<ActionSave>(action);
if (!this->undo_redo_transferList) {
this->undoStackTransferList.push_back(move(pSave));
//reinitialize redo stack;
this->redoStackTransferList = vector<unique_ptr<Action>>();
}
this->transferList.addToTransferList(victim);
}
void Service::deleteFromTransferList(const std::string& name)
{
if (name == "")
throw ServiceException("Empty name! \n");
Victim victim = this->repo.findVictimByName(name);
VictimValidator validator{};
validator.validateVictim(victim);
if (victim == Victim{})
throw ServiceException("Victim not existent! \n");
this->transferList.deleteFromTransferList(victim);
}
std::vector<Victim> Service::getAllSaved() const
{
return this->transferList.getAllVector();
}
void Service::iterator()
{
this->repo.iterator();
}
void Service::next()
{
this->repo.next();
}
Victim Service::getCurrentFromIterator()
{
return repo.getCurrentVictim();
}
void Service::undo()
{
if (undoStack.size() == 0) {
throw std::exception("Cannot undo!");
}
std::unique_ptr<Action> act = move(undoStack.back());
undo_redo = true;
act->executeUndo();
undo_redo = false;
redoStack.push_back(move(act));
undoStack.pop_back();
}
void Service::redo()
{
if (redoStack.size() == 0)
{
throw std::exception("Cannot Redo!");
}
std::unique_ptr<Action> act = move(redoStack.back());
undo_redo = true;
act->executeRedo();
undo_redo = false;
undoStack.push_back(move(act));
redoStack.pop_back();
}
void Service::undoTransferList()
{
if (undoStackTransferList.size() == 0) {
throw std::exception("Cannot undo!");
}
std::unique_ptr<Action> act = move(undoStackTransferList.back());
undo_redo_transferList = true;
act->executeUndo();
undo_redo_transferList = false;
redoStackTransferList.push_back(move(act));
undoStackTransferList.pop_back();
}
void Service::redoTransferList()
{
if (redoStackTransferList.size() == 0)
{
throw std::exception("Cannot Redo!");
}
std::unique_ptr<Action> act = move(redoStackTransferList.back());
undo_redo_transferList = true;
act->executeRedo();
undo_redo_transferList = false;
undoStackTransferList.push_back(move(act));
redoStackTransferList.pop_back();
}