-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtreeGram.cpp
More file actions
executable file
·191 lines (166 loc) · 6.24 KB
/
treeGram.cpp
File metadata and controls
executable file
·191 lines (166 loc) · 6.24 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
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
#include "treeGram.h"
#include <algorithm>
#include <chrono> // std::chrono::system_clock
// #include <crow/middlewares/cors.h>
#include <cstddef>
#include <cstdlib>
#include <random>
treeGram::treeGram() {}
treeGram::~treeGram() {}
void treeGram::test() {
cout << goFast.size() << endl;
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::shuffle(std::begin(handels), std::end(handels),
std::default_random_engine(seed));
for (unsigned int i = 0; i < handels.size(); i++) {
cout << handels[i] << endl;
goFast.search(handels[i])->displayInfo();
}
}
void treeGram::Deploy() {
// Enable CORS
crow::SimpleApp app;
//
// // Customize CORS
// auto &cors = app.get_middleware<crow::CORSHandler>();
// // clang-format off
// cors
// .global()
// .headers("X-Custom-Header", "Upgrade-Insecure-Requests")
// .methods("POST"_method, "GET"_method)
// .prefix("/cors")
// .origin("json.almiraj.xyz")
// .prefix("/nocors")
// .ignore();
// // clang-format on
//
// CROW_ROUTE(app, "/")
// ([]() { return "Check Access-Control-Allow-Methods header"; });
//
// CROW_ROUTE(app, "/cors")
// ([]() { return "Check Access-Control-Allow-Origin header"; });
//
CROW_ROUTE(app, "/add_user")
.methods("POST"_method)([this](const crow::request &req) {
auto x = crow::json::load(req.body);
if (!x)
return "Check Access-Control-Allow-Origin header";
// return crow::response(400);
this->addUser(x["name"].s(), x["phone"].s(), x["handel"].s(),
x["age"].i());
cout << x["name"].s() << " " << x["phone"].s() << " " << x["handel"].s()
<< " " << x["age"].i() << endl;
std::ostringstream os;
os << x;
// return crow::response{os.str()};
return "Check Access-Control-Allow-Origin header";
});
CROW_ROUTE(app, "/add_post")
.methods("POST"_method)([this](const crow::request &req) {
auto x = crow::json::load(req.body);
if (!x)
return crow::response(400);
this->addPost(x["handel"].s(), x["content"].s());
cout << x["handel"].s() << " " << x["content"].s() << endl;
std::ostringstream os;
os << x;
return crow::response{os.str()};
});
CROW_ROUTE(app, "/add_like")
.methods("POST"_method)([this](const crow::request &req) {
auto x = crow::json::load(req.body);
if (!x)
return crow::response(400);
this->addLikes(x["handel"].s(), x["id"].i(), x["like?"].i());
cout << x["handel"].s() << " " << x["id"].s() << " " << x["like?"].i()
<< endl;
std::ostringstream os;
os << x;
return crow::response{os.str()};
});
// Route handler for posts
CROW_ROUTE(app, "/jsonfeed")
([this] {
const int n = 10; // Set the number of posts
std::vector<crow::json::wvalue> postsArray;
for (int i = 0; i < n; ++i) {
User *x = goFast.search(handels[i]);
Activity z = x->activites[(x->activites.getSize()!=0)? i%x->activites.getSize():0];
postsArray.push_back(crow::json::wvalue({{"id", (x->activites.getSize()!=0)? i%x->activites.getSize():0},
{"Handle", x->getHandel()},
{"User_Name", x->getName()},
{"Content", z.getContent()},
{"Photo", "NONE"},
{"likes", z.getLikes()},
{"views", z.getViews()},
{"Date", z.getDate()}}));
}
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::shuffle(std::begin(handels), std::end(handels),
std::default_random_engine(seed));
return crow::json::wvalue({{"Posts", postsArray}});
});
// Route handler for profile
CROW_ROUTE(app, "/jsonProfile/<string>")
([this](std::string Handle) {
User *x = goFast.search(Handle);
if (x == NULL)
return crow::json::wvalue();
std::vector<crow::json::wvalue> profilePostsArray;
for (size_t i = 0; i < x->activites.getSize(); i++) {
profilePostsArray.push_back(
crow::json::wvalue({{"id", i},
{"Content", x->activites[i].getContent()},
{"Photo", "NONE"},
{"likes", x->activites[i].getLikes()},
{"views", x->activites[i].getViews()},
{"Date", x->activites[i].getDate()}}));
}
return crow::json::wvalue({{"name", x->getName()},
{"phone", x->getPhone()},
{"handel", x->getHandel()},
{"age", x->getAge()},
{"activites", profilePostsArray}});
;
});
app.port(18080).server_name("CrowCpp").multithreaded().run();
}
bool treeGram::addUser(string name, string phone, string handel, int age) {
if (goFast.search(handel) == NULL) {
cout << "added User :" << handel << endl;
handels.push_back(handel);
goFast.insert(name, phone, handel, age);
return true;
}
return false;
}
bool treeGram::addPost(const string &handel, const string &content) {
User *temp = goFast.search(handel);
if (temp != NULL) {
cout << "added Post for User :" << handel << endl
<< "Content" << content << endl;
temp->addPost(content);
return true;
}
return false;
}
bool treeGram::addLikes(const string &handel, size_t post_num, size_t Likes) {
User *temp = goFast.search(handel);
if (temp != NULL) {
cout << "added likes for User :" << handel << endl
<< "post : " << post_num << " by " << Likes << endl;
temp->addLikes(post_num, Likes);
return true;
}
return false;
}
bool treeGram::addViews(const string &handel, size_t post_num, size_t Views) {
User *temp = goFast.search(handel);
if (temp != NULL) {
cout << "added views for User :" << handel << endl
<< "post : " << post_num << " by " << Views << endl;
temp->addViews(post_num, Views);
return true;
}
return false;
}