-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRepository.cpp
127 lines (111 loc) · 2.66 KB
/
Repository.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
#include "Repository.h"
Repository::~Repository()
{
}
int Repository::addFilm(const Film & f)
{
int foundPos = findFilm(f);
if (foundPos == -1) {
films.push_back(f);
return 0; //0 - succesfully added
}
else
{
return -1; //-1 - is in DB
// throw FilmNotFound();
}
}
int Repository::deleteFilm(const Film & f)
{
int foundPos = findFilm(f);
if (foundPos != -1) {
films.erase(films.begin() + foundPos);
return 0; //0 - succesfully deleted
}
else
{
return -1; //-1 - isn't in DB
//throw FilmNotFound();
}
}
void Repository::updateFilm(unsigned pos, const std::string & newTitle, const std::string & newGenre, const unsigned newYear)
{
if (newTitle != "") {
films[pos].setTitle(newTitle);
return;
}
if (newGenre != "") {
films[pos].setGenre(newGenre);
return;
}
if (newYear != 0) {
films[pos].setYear(newYear);
return;
}
}
std::vector<Film> Repository::getFilms() const
{
return films;
}
int Repository::findFilm(const Film & f)
{
unsigned i;
for (i = 0; i < films.size(); i++) {
if (films[i].getTitle() == f.getTitle() && films[i].getYear() == f.getYear())
return i;
}
return -1;
}
Film Repository::findByID(const std::string & title, unsigned year)
{
for (unsigned i = 0; i < films.size(); i++)
if (films[i].getTitle() == title && films[i].getYear() == year)
return films[i];
return Film(); //if there is no such film, returns empty film
}
void Repository::incLikes(const Film& f)
{
int foundPos = findFilm(f);
films[foundPos].incLikes();
}
int Repository::findUser(const std::string& userName)
{
int found = -1;
for (unsigned i = 0; i < users.size(); i++)
if (users[i].userName == userName) {
found = i;
break;
}
return found;
}
void Repository::addUser(const User & u)
{
users.push_back(u);
}
std::vector<User>& Repository::getUsers()
{
return users;
}
int Repository::deleteUsersFilmFromWatchlist(const std::string& userName, const Film& f)
{
// try {
int foundPos = findUser(userName);
return users[foundPos].watchList.deleteFilm(f);
//}
// catch (FilmInWatchlist& e)
// {
// throw FilmInWatchlist();
// }
}
int Repository::addUsersFilmToWatchlist(const std::string & userName, const Film & f)
{
// try {
int foundPos = findUser(userName);
return users[foundPos].watchList.add(f);
//users[foundPos].watchList.add(f);
//}
// catch (FilmInWatchlist& e)
// {
// throw FilmInWatchlist();
// }
}