Add join
This commit is contained in:
parent
d97fb759ee
commit
849a52a230
|
@ -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
|
||||
|
|
122
join.cpp
Normal file
122
join.cpp
Normal file
|
@ -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;
|
||||
}
|
|
@ -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";
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
Reference in New Issue
Block a user