-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.cpp
209 lines (169 loc) · 5.65 KB
/
server.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
207
208
209
#include <bits/stdc++.h>
using namespace std;
#include "server.h"
#include "database.h"
string Server::getTime(){
const int bufferLen=50;
char buffer[bufferLen];
time_t __t(time(0));
strftime(buffer,sizeof(buffer),"%Y/%02m/%d %H:%M:%S",localtime(&__t));
return buffer;
}
Server::Server(Database* ptr):db(ptr){}
ErrorCode Server::userLogin(User &user){
if (!db->findOne(user)) return noSuchUser;
if (!db->userExist(user)) return wrongPassword;
if (user["Status"]!="Accessible") return loginFailed;
return noError;
}
ErrorCode Server::modifyPassword(User user,Password newPwd){
if (!db->findOne(user)) return userNotFound;
if (!db->userExist(user)) return wrongPassword;
if (user["Status"]!="Accessible") return loginFailed;
return db->modifyPassword(user,newPwd);
}
ErrorCode Server::borrowBook(const User& currentUser,const PracticalBook& book){
PracticalBook res=book;
if (!db->findOne(res)) return bookNotFound;
if (res["Status"]!="Accessible") return bookInaccessible;
if (db->objectExist(Record({
Field("Username",currentUser["Username"]),
Field("BookNo",book["No"]),
Field("BookIndex",book["Index"]),
Field("Status","Pending"),
Field("Type","Borrowing")
})))
return requestAlreadySubmitted;
return db->add(Record({
Field("Id",db->newRecordId()),
Field("Username",currentUser["Username"]),
Field("BookNo",res["No"]),
Field("BookIndex",res["Index"]),
Field("Status","Pending"),
Field("Time",getTime()),
Field("Type","Borrowing")
}));
}
ErrorCode Server::returnBook(const User& currentUser,const PracticalBook& book){
PracticalBook res=book;
if (!db->findOne(res)) return bookNotFound;
if (res["Status"]!="Borrowed") return bookNotBorrowed;
if (db->objectExist(Record({
Field("Username",currentUser["Username"]),
Field("BookNo",book["No"]),
Field("BookIndex",book["Index"]),
Field("Status","Pending"),
Field("Type","Returning")
})))
return requestAlreadySubmitted;
if (!db->objectExist(Record({
Field("Username",currentUser["Username"]),
Field("BookNo",book["No"]),
Field("BookIndex",book["Index"]),
Field("Status","Accepted"),
Field("Type","Borrowing")
})))
return bookNotBorrowedByCurrentUser;
return db->add(Record({
Field("Id",db->newRecordId()),
Field("Username",currentUser["Username"]),
Field("BookNo",res["No"]),
Field("BookIndex",res["Index"]),
Field("Status","Pending"),
Field("Time",getTime()),
Field("Type","Returning")
}));
}
ErrorCode Server::acceptRequest(const User& currentUser,const Record& record){
Record res=record;
if (!db->findOne(res)) return requestNotFound;
if (res["Type"]!="Borrowing"&&res["Type"]!="Returning") return requestNotFound;
if (res["Status"]!="Pending") return requestAlreadyProcessed;
bool flag=res["Type"]=="Borrowing";
if (!db->objectExist(PracticalBook({
Field("No",res["BookNo"]),
Field("Index",res["BookIndex"]),
Field("Status",flag?"Accessible":"Borrowed")
})))
return flag?bookInaccessible:bookNotBorrowed;
ErrorCode ret=db->update(PracticalBook({
Field("No",res["BookNo"]),
Field("Index",res["BookIndex"]),
Field("Status",flag?"Borrowed":"Accessible")
}));
if (ret!=0) return ret;
if (!flag){
vector<Record> recs;
ret=db->search<Record>(CompleteMatchingSearch({
Field("Username",res["Username"]),
Field("BookNo",res["BookNo"]),
Field("BookIndex",res["BookIndex"]),
Field("Type","Borrowing"),
Field("Status","Accepted")
}),recs);
if (ret!=0) return ret;
if (int(recs.size())!=1) return unknownError;
string tmpId=(*recs.begin())["Id"];
ret=db->update(Record(set<Field>({
Field("Id",tmpId),
Field("Status","Returned")
})));
if (ret!=0) return ret;
}
return db->update(Record(set<Field>({
Field("Id",res["Id"]),
Field("Status","Accepted"),
Field("Remarks","Processing Time : "+getTime())
})));
}
ErrorCode Server::rejectRequest(const User& currentUser,const Record& record){
Record res=record;
if (!db->findOne(res)) return requestNotFound;
if (res["Type"]!="Borrowing"&&res["Type"]!="Returning") return requestNotFound;
if (res["Status"]!="Pending") return requestAlreadyProcessed;
bool flag=res["Type"]=="Borrowing";
return db->update(Record(set<Field>({
Field("Id",res["Id"]),
Field("Status","Rejected"),
Field("Remarks","Processing Time : "+getTime())
})));
}
ErrorCode Server::browseBook(const User& currentUser,Book& book){
if (!db->findOne(book)) return bookNotFound;
if (book["Status"]!="Accessible") return bookInaccessible;
return db->add(Record({
Field("Id",db->newRecordId()),
Field("Username",currentUser["Username"]),
Field("BookNo",book["No"]),
Field("Status","Meowed"),
Field("Time",getTime()),
Field("Type","Browsing")
}));
}
ErrorCode Server::previewBookContent(const User& currentUser,Book book,Content *ret){
if (!db->findOne(book)) return bookNotFound;
if (book["Status"]!="Accessible") return bookInaccessible;
db->add(Record({
Field("Id",db->newRecordId()),
Field("Username",currentUser["Username"]),
Field("BookNo",book["No"]),
Field("Status","Meowed"),
Field("Time",getTime()),
Field("Type","Previewing")
}));
string url=book["Url"];
string suffix=url.substr(url.find_last_of(".")+1);
if (suffix==url) suffix="";
bool found=0;
if (suffix=="txt"){
ret=new TxtContent(url,found);
}
else{
return unknownContentSuffix;
}
return found?noError:bookContentMissing;
}
//==============Gu Gu Gu!====================
ErrorCode searchInContent(const User ¤tUser,const Book &book,const string &keyWord){
return guGuGu;
}