-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.cpp
206 lines (163 loc) · 5.23 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <sstream>
#include "myexceptions.h"
#include "recording.h"
#include "service.h"
#include <QDebug>
using namespace std;
Service::Service(Repository* repository) {
auto memory_repository = dynamic_cast<MemoryRepository*>(repository);
if (memory_repository) {
this->repository = memory_repository;
this->has_file_repository = false;
qDebug() << "It is a memory repository!";
} else {
this->file_repository = dynamic_cast<FileRepository*>(repository);
this->has_file_repository = true;
}
}
bool Service::does_service_have_file_repository() {
return has_file_repository;
}
string Service::get_file_repository_filename() {
return file_repository->get_filename();
}
void Service::add(string title, string location, string time_of_creation, string times_accessed, string footage_preview) {
/**
*
* Adds a new recording to the repository.
*
* Input:
* - title, location, time_of_creation, times_accessed, footage_preview: strings
**/
validator.validate_time_of_creation(time_of_creation);
validator.validate_times_accessed(times_accessed);
Recording recording(title, location, time_of_creation, stoi(times_accessed), footage_preview);
if (!has_file_repository) {
repository->add(recording);
} else {
file_repository->add(recording);
}
}
vector<Recording> Service::get_repository_container() {
/**
* Gets the contents of the custom-made std::vector from the repository.
*
**/
if (!has_file_repository) {
return repository->get_container();
} else {
return file_repository->get_container();
}
}
bool Service::search(string title) {
if (!has_file_repository) {
return repository->search(title);
} else {
return file_repository->search(title);
}
}
void Service::remove(string title) {
/**
*
* Removes a recording from the repository by its title.
*
* Input:
* - title: string
**/
if (!has_file_repository) {
repository->remove(title);
} else {
file_repository->remove(title);
}
}
void Service::update(string title, string location, string time_of_creation, string times_accessed, string footage_preview) {
/**
*
* Updates the details of a recording in the repository.
* In case the recording with the specified title does not exist, the method throws
* a RepositoryException.
*
* Input:
* - title, location, time_of_creation, times_accessed, footage_preview: strings
* Throws:
* - RepositoryException: in case the recording with the given title doesn't exist
**/
validator.validate_time_of_creation(time_of_creation);
validator.validate_times_accessed(times_accessed);
if (!has_file_repository && !repository->search(title)) {
RepositoryException re("RepositoryException: The element cannot be updated because it doesn't exist!\n");
throw re;
} else if (has_file_repository && !file_repository->search(title)) {
RepositoryException re("RepositoryException: The element cannot be updated because it doesn't exist!\n");
throw re;
}
if (!has_file_repository) {
repository->remove(title);
} else {
file_repository->remove(title);
}
Recording recording(title, location, time_of_creation, stoi(times_accessed), footage_preview);
if (!has_file_repository)
repository->add(recording);
else
file_repository->add(recording);
}
void Service::set_selected_recording_index(int new_index) {
if (!has_file_repository) {
repository->set_selected_recording_index(new_index);
} else {
file_repository->set_selected_recording_index(new_index);
}
}
int Service::get_selected_recording_index() {
if (!has_file_repository) {
return repository->get_selected_recording_index();
} else {
return file_repository->get_selected_recording_index();
}
}
string Service::next() {
if (!has_file_repository)
return repository->next();
else
return file_repository->next();
}
void Service::save(string title) {
if (!has_file_repository)
repository->save(title);
else
file_repository->save(title); /// has to be fixed
}
void Service::set_current_recording(int position) {
if (!has_file_repository) {
repository->set_current_recording(position);
} else {
file_repository->set_current_recording(position);
}
}
vector<Recording> Service::get_watchlist() {
if (!has_file_repository)
return repository->get_watchlist();
else
return file_repository->get_watchlist();
}
Service::~Service() {
}
void Service::set_file_repository_filename(string new_filename) {
file_repository->set_filename(new_filename);
}
void Service::set_watchlist_filename(string file_path) {
file_repository->set_watchlist_filename(file_path);
if (file_path.find(".html") != std::string::npos) {
file_repository->update_watchlist_html_file();
} else {
file_repository->update_watchlist_csv_file();
}
}
void Service::remove_from_watchlist(int element_index) {
if (!has_file_repository) {
repository->remove_from_watchlist(element_index);
} else {
file_repository->remove_from_watchlist(element_index);
}
}