Skip to content

Commit 9d68bcc

Browse files
update
1 parent f2bfaa2 commit 9d68bcc

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

msg_controller.cc

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ void request_processing(const HttpRequestPtr &req, std::function<void(const Http
136136
}
137137
auto res = HttpResponse::newHttpResponse();
138138
res->setBody("Success");
139-
139+
}
140140
void info(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) {
141141
auto body = req->getBody();
142142
Json::Value req_json, res_json;
@@ -165,8 +165,12 @@ void info(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)
165165

166166
auto res = HttpResponse::newHttpResponse();
167167
res->addHeader("Access-Control-Allow-Origin", "*");
168-
who_send_me = req_json["person"].asString();
169-
auto output = writer.write(get_chat_info(me,who_send_me));
170-
res->setBody(output);
171-
callback(res);
168+
if(req_json["person"].asString()==""){
169+
res->setBody(writer.write(get_chat_info(me)));
170+
callback(res);
171+
}else{
172+
who_send_me = req_json["person"].asString();
173+
res->setBody(writer.write(get_chat_info(me,who_send_me)));
174+
callback(res);
175+
}
172176
}

mysql.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,36 @@ void sql_add(std::string username, std::string passwd, int avatar) {
230230

231231
// return result;
232232
// }
233+
Json::Value get_chat_info(std::string me){
234+
Json::Value json;
235+
try {
236+
sql::mysql::MySQL_Driver *driver;
237+
driver = sql::mysql::get_mysql_driver_instance();
238+
sql::Connection *con;
239+
con = driver->connect("tcp://8.130.48.157:3306", "root", "abc.123");
240+
con->setSchema("flypen");
241+
242+
std::string sql = "SELECT * FROM users WHERE username = ? LIMIT 1";
243+
sql::PreparedStatement *prepStmt = con->prepareStatement(sql);
244+
prepStmt->setString(1, me);
245+
246+
sql::ResultSet *res = prepStmt->executeQuery();
247+
if (res->next()) {
248+
Json::Value user;
249+
int avatar=res->getInt("avatar");
250+
std::string friends=res->getString("friends");
251+
std::string req=res->getString("req");
252+
user["avatar"] = avatar;
253+
user["friends"] = friends;
254+
user["req"] = req;
255+
256+
json[me] = user;
257+
}
258+
} catch (sql::SQLException &e) {
259+
std::cerr << "SQL Exception: " << e.what() << std::endl;
260+
}
261+
return json;
262+
}
233263
Json::Value get_chat_info(std::string me,std::string who_send_me){
234264
Json::Value json;
235265
try {

mysql.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ void sql_addrequest(std::string send,std::string receiver);
1212
void sql_process_request(std::string ,std::string,std::string);
1313
Json::Value sql_find_my_msg(std::string,std::string);
1414
Json::Value get_chat_info(std::string,std::string);
15+
Json::Value get_chat_info(std::string);
1516
//bool sql_check(std::string );
1617
#endif

user_controller.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ void Handle(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr
1717
Json::FastWriter writer;
1818
std::string msg = handler(req_json);
1919
res_json["msg"] = msg;
20-
if (msg=="Login Success")
20+
if (msg=="Login Success"){
2121
res_json["token"] = jwtGen(req_json);
22-
else
22+
res_json["username"] = req_json["username"].asString();
23+
}else
2324
res_json["token"] = "";
2425
auto output = writer.write(res_json);
2526
auto res = HttpResponse::newHttpResponse();

0 commit comments

Comments
 (0)