From 849a52a230a539cec7f780004a361745b471e3a3 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Mon, 20 Nov 2017 09:37:43 +0800 Subject: [PATCH] Add join --- design.txt | 4 +- join.cpp | 122 +++++++++++++++++++++++++++++++++++++++++++++++++++ jsonfail.cpp | 2 + jsonfail.h | 1 + register.cpp | 0 5 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 join.cpp delete mode 100644 register.cpp diff --git a/design.txt b/design.txt index c15866a..94d18eb 100644 --- a/design.txt +++ b/design.txt @@ -155,7 +155,7 @@ 当新的类型名称发生冲突 副作用: - 改变图书种类会引起所有与原种类相关图书的信息变更 + 改变图书种类会引起所有与原种类相关图书的信息变更(未实现) http://booksys.com/api/v1/removebooktype POST POST: @@ -164,7 +164,7 @@ 错误: 当任一图书定义使用此定义时将不能删除图书种类 - http://booksys.com/api/v1/register POST + http://booksys.com/api/v1/join POST POST: account pass diff --git a/join.cpp b/join.cpp new file mode 100644 index 0000000..983698b --- /dev/null +++ b/join.cpp @@ -0,0 +1,122 @@ +#include "Session.h" +#include "Util.h" +#include "json.hpp" +#include "jsonfail.h" +using namespace std; +using json=nlohmann::json; + +#define postval(NAME) string NAME=req.post[#NAME] + +int main() +{ + Request req; + Session se(req); + Response res; + json j; + + auto jsonfail=[&](int errcode,const string& detail="") + { + j["success"]=0; + j["errcode"]=errcode; + j["errmsg"]=string(GetErrMsg(errcode))+" : "+detail; + }; + + do + { + if(!se.isReady()) + { + jsonfail(err_session); + break; + } + if(!se.getUser().empty()) + { + /// Logged in. Redirect user to dashboard. + j["success"]=2; + j["next_url"]="/booksys/dashboard.html"; + break; + } + + if(req.requestMethod!="POST") + { + jsonfail(err_method_not_supported); + break; + } + + if(req.post["account"].empty() || + req.post["pass"].empty() || + req.post["nickname"].empty()) + { + jsonfail(err_missing_parameter); + break; + } + + postval(account); + postval(pass); + postval(nickname); + + /// Connect DB + DBInfo db; + MySQLConn conn; + if(db.readConfig()<0) + { + jsonfail(err_config); + break; + } + + if(db.connectProxy(conn)<0) + { + jsonfail(err_connect); + break; + } + + /// Check username conflict + int count_val; + if(conn.exec(make_str("select count(username) from bs_user where username='", + account, + "'"), + [&](MySQLResult& res) + { + res.stepRow([&](char** val,unsigned long* len) + { + count_val=ParseInt(val[0]); + }); + })<0) + { + jsonfail(err_sql); + break; + } + + if(count_val!=0) + { + jsonfail(err_data,"username conflict"); + break; + } + + /// Do insert + if(conn.exec(make_str("insert into bs_user values ('", + account, + "','", + pass, + "','", + nickname, + "',3,2"),nullptr)<0) + { + jsonfail(err_sql); + break; + } + + if(conn.getAffectedRows()!=1) + { + jsonfail(err_sql_logic,"Affected rows not equals 1"); + break; + } + + /// Redirect user to enable page. + j["success"]=1; + j["next_url"]="/booksys/enable.html"; + }while(0); + + res.content.append(j.dump()); + res.show(); + return 0; +} diff --git a/jsonfail.cpp b/jsonfail.cpp index 4e604a4..9604ef6 100644 --- a/jsonfail.cpp +++ b/jsonfail.cpp @@ -22,6 +22,8 @@ const char* GetErrMsg(int errcode) return "Permission denied"; case err_data: return "Data Error"; + case err_session: + return "Session Error"; default: return "General error"; } diff --git a/jsonfail.h b/jsonfail.h index 9b21119..f2205e7 100644 --- a/jsonfail.h +++ b/jsonfail.h @@ -9,5 +9,6 @@ #define err_sql_logic 7 #define err_permission_denied 8 #define err_data 9 +#define err_session 10 const char* GetErrMsg(int errcode); diff --git a/register.cpp b/register.cpp deleted file mode 100644 index e69de29..0000000