-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFetchNoteHandler.cpp
More file actions
103 lines (90 loc) · 3.37 KB
/
FetchNoteHandler.cpp
File metadata and controls
103 lines (90 loc) · 3.37 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
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
//
// Created by root on 5/30/17.
//
#include "FetchNoteHandler.h"
#include <rapidjson/document.h>
using namespace std;
using namespace MyWeb;
using namespace rapidjson;
void fetchNotePost(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();
Value noteList(kArrayType);
if(nid){
note_ptr note = db->getNote(nid);
Value noteValue(kObjectType);
Value title;
title.SetString(note->title.c_str(),d.GetAllocator());
Value content;
content.SetString(note->content.c_str(),d.GetAllocator());
noteValue.AddMember("nid",note->nid,d.GetAllocator());
noteValue.AddMember("title",title,d.GetAllocator());
noteValue.AddMember("content",content,d.GetAllocator());
noteList.PushBack(noteValue,d.GetAllocator());
}else{
note_ptr* notes = nullptr;
int count = db->getAllNote(info->UID,¬es);
for(int i = 0;i<count;i++){
Value noteValue(kObjectType);
Value title;
title.SetString(notes[i]->title.c_str(),d.GetAllocator());
Value content;
content.SetString(notes[i]->content.c_str(),d.GetAllocator());
noteValue.AddMember("nid",notes[i]->nid,d.GetAllocator());
noteValue.AddMember("title",title,d.GetAllocator());
noteValue.AddMember("content",content,d.GetAllocator());
noteList.PushBack(noteValue,d.GetAllocator());
}
if(notes){
delete[] notes;
}
}
d.AddMember("status", true, d.GetAllocator());
d.AddMember("notes",noteList,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;
}
FetchNoteHandler::FetchNoteHandler(MyWeb::ServerHttp &server) {
server.addResource("^/fetch/?$","POST",fetchNotePost);
}
FetchNoteHandler::~FetchNoteHandler() {
}