-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeleteNoteHandler.cpp
More file actions
75 lines (62 loc) · 2.08 KB
/
DeleteNoteHandler.cpp
File metadata and controls
75 lines (62 loc) · 2.08 KB
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
//
// Created by root on 5/31/17.
//
#include "DeleteNoteHandler.h"
using namespace std;
using namespace MyWeb;
using namespace rapidjson;
void deleteNotePost(Response& response, Request& request){
Session session;
Database *db = Database::getInstance();
Document d;
d.SetObject();
try{
if (!request.hasCookie("token") ||
!request.hasCookie("UID")){
throw IncorrectDataException();
}
SessionInfo *info = session.GetSessionInfo(request.getCookie("token"));
if (info == nullptr || info->UID != atoi(request.getCookie("UID").c_str())) {
throw IncorrectSessionException();
}
Document data;
String2Json(request.getRawBody(),data);
if(!data.HasMember("nid")){
throw IncorrectDataException();
}
int nid = data["nid"].GetInt();
if(!db->checkNoteOwner(nid,info->UID)){
throw IncorrectDataException();
}
db->deleteNote(nid);
d.AddMember("status",true,d.GetAllocator());
}catch(IncorrectSessionException const& e){
d.AddMember("status", false, d.GetAllocator());
Value message;
message.SetString(e.what(),d.GetAllocator());
d.AddMember("message", message, d.GetAllocator());
time_t t;
time(&t);
t -= 60*60*24;
response.setCookie("UID","",t);
response.setCookie("token","",t);
}catch(exception const& e){
d.AddMember("status", false, d.GetAllocator());
Value message;
message.SetString(e.what(),d.GetAllocator());
d.AddMember("message", message, d.GetAllocator());
}catch(...){
d.AddMember("status", false, d.GetAllocator());
Value message;
d.AddMember("message", "unknown error", d.GetAllocator());
}
string res;
Json2String(d, res);
response.setContentType("application/json");
response << res;
}
DeleteNoteHandler::DeleteNoteHandler(MyWeb::ServerHttp &server) {
server.addResource("^/delete/?$","POST",deleteNotePost);
}
DeleteNoteHandler::~DeleteNoteHandler() {
}