-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.cpp
204 lines (157 loc) · 4.07 KB
/
admin.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
#include "admin.hpp"
#include <vector>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
Admin::Admin()
{
}
std::string Admin::getPath()
{
return this->relPath + this->dirPath + this->strToLower(this->username);
}
bool Admin::removeSelf()
{
if(!this->isAuth)
{
std::cout << "\nNot auth\n";
return false;
}
std::string pathProfile = this->getPath() + ".txt";
return !remove(pathProfile.c_str());
}
bool Admin::removeBook()
{
std::vector<Book> v;
if(!this->findBook(v)) return false;
int index = this->inputMinMax("\nInput number of book to delet ", 0, v.size() - 1);
int c = this->inputMinMax("\nDelet one(0) or all(1)", 0, 1);
if(c)
{
return v[index].removeSelf();
} else {
return v[index]--;
}
}
bool Admin::addBook()
{
int opt = this->inputMinMax("\nAdd one book more(0) or another book(1)", 0, 1);
if(opt)
{
Book b;
opt = this->inputMinMax("\nAdd your self(0) or file(1)", 0, 1);
if(!opt)
{
return b.setAllInfo();
} else
{
std::string path = "";
std::cout << "Input path to file: ";
std::cin >> path;
return b.setAllInfo(path);
}
return b.save();
} else
{
std::vector<Book> v;
if(!this->findBook(v)) return false;
int index = this->inputMinMax("\nInput number to inc:", 0, v.size() - 1);
return v[index]++;
}
}
bool Admin::addUser()
{
std::cout << "Add user";
User u;
if(u.signUp(1))
{
u.getInfo();
return true;
}
return false;
}
bool Admin::getUser(bool b)
{
std::string username = "";
std::string path = this->relPath + this->dirUsers;
bool b1 = false;
bool bCount = false;
std::cout << "\nInput username: ";
std::cin >> username;
b1 = this->input( username, "0", &this->validateNameAndSubject );
if(!b1) return false;
DIR* dirp = opendir(path.c_str());
struct dirent * dp;
while ((dp = readdir(dirp)) != NULL) {
std::string file = dp->d_name;
if(file.find(this->strToLower(username) + ".txt") != std::string::npos)
{
bCount = true;
User u;
if(u.auth(path + file))
{
u.initBooks();
u.getInfo();
if(b) return u.removeSelf();
}
}
}
closedir(dirp);
if(!bCount) std::cout << "\nNo Such User\n";
return true;
}
bool Admin::getUsers(std::vector<User>& vUsers)
{
std::string path = this->relPath + this->dirUsers;
DIR* dirp = opendir(path.c_str());
int i = 0;
int i2 = 0;
struct dirent * dp;
while ((dp = readdir(dirp)) != NULL) {
i2++;
if(i2 < 3) continue;
std::string file = dp->d_name;
if(file.find("_history.txt") != std::string::npos) continue;
User u;
u.auth(path + file);
vUsers.push_back(u);
std::cout << "\n" << i++;
u.getInfo();
}
closedir(dirp);
return true;
}
void Admin::getUsersInfo()
{
std::string path = this->relPath + this->dirUsers;
DIR* dirp = opendir(path.c_str());
int i = 0;
int i2 = 0;
struct dirent * dp;
while ((dp = readdir(dirp)) != NULL) {
i2++;
if(i2 < 3) continue;
std::string file = dp->d_name;
if(file.find("_history.txt") != std::string::npos) continue;
User u;
u.auth(path + file);
u.initBooks();
std::cout << "\n" << i++;
u.getInfo();
}
closedir(dirp);
}
bool Admin::removeUser()
{
int c = this->inputMinMax("Remove concret user(0) or from list(1)\n", 0, 1);
if(!c)
{
return this->getUser(true);
} else
{
std::vector<User> vUsers;
if(!this->getUsers(vUsers)) return false;
int index = this->inputMinMax("Input index to delet:", 0, vUsers.size() - 1);
return vUsers[index].removeSelf();
}
}