-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.cpp
129 lines (122 loc) · 3.23 KB
/
user.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
#include "user.h"
#include "signup.h"
#include "login.h"
#include "adminx.h"
User::User(){}
User::User(
string username,
string password,
string region,
string email)
{
this->username = username;
this->password = password;
this->region = region;
this->email = email;
}
User::User(string firstname,string lastname,string username,string password,string email){
this->firstname=firstname;
this->lastname=lastname;
this->username=username;
this->password=password;
this->email=email;
}
vector<NewsModel> User::Search(SearchBasedOn searchBasedOn, string inp) {
if(searchBasedOn == SearchBasedOn::Keyword)
{
return _searchByKeywords(inp);
}
else if(searchBasedOn == SearchBasedOn::titles)
{
return _searchByTitles(inp);
}
else if(searchBasedOn == SearchBasedOn::date)
{
return _searchByDate(inp);
}
else
{
qDebug() << "Undefined Type\n";
return {};
}
}
vector<NewsModel> User::_searchByKeywords(string input)
{
unordered_set<string> keywords;
buildKeys(input, keywords); // build keyword for input to search with
vector<NewsModel> ans;
auto &news = Adminx::news;
map<int, bool> checked;
for(int i = 0; i < news.size(); i++) // iterates for all news
{
bool loop2 = false;
for(auto &keyword : news[i].keywords)
{
qDebug() << keyword << '\n';
}
qDebug() << "------------------------------------------------\n";
for(auto &keyword : keywords) // iterates for all keywords
{
if(news[i].keywords.find(keyword) != news[i].keywords.end()) // search if keyword is found in newModel or not
{
if(!checked.count(i))
{
ans.push_back(news[i]);
checked[i] = true; // break the second loop to prevent the repeat of newModel in answer
}
}
}
}
return ans;
}
vector<NewsModel> User::_searchByTitles(string title)
{
vector<NewsModel> ans;
for(auto &newModel : Adminx::news)
{
if(title == newModel.getTitle())
{
ans.push_back(newModel);
break;
}
}
return ans;
}
vector<NewsModel> User::_searchByDate(string date)
{
vector<NewsModel> ans;
for(auto &newModel : Adminx::news)
{
if(newModel.getDate() == date)
{
ans.push_back(newModel);
continue;
}
}
return ans;
}
void User::setUsername(string username)
{
this->username = username;
}
void User::setPassword(string password)
{
this->password = password;
}
void User::setRegion(string region)
{
this->region = region;
}
void User::setEmail(string email)
{
this->email = email;
}
string User::getFirstName(){return firstname;}
string User::getLastName(){return lastname;}
string User::getUsername() { return username; }
string User::getPassword() { return password; }
string User::getRegion() { return region; }
string User::getEmail() { return email; }
void User::addUser(User user){
Adminx::users.push_back(user);
}