2017-11-19 15:43:47 +08:00
|
|
|
#include "Session.h"
|
|
|
|
#include "Util.h"
|
|
|
|
#include "json.hpp"
|
|
|
|
using namespace std;
|
2017-11-19 23:28:27 +08:00
|
|
|
using json=nlohmann::json;
|
|
|
|
|
|
|
|
#define postval(NAME) string NAME=req.post[#NAME]
|
2017-11-19 15:43:47 +08:00
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
Request req;
|
|
|
|
Session se(req);
|
|
|
|
Response res;
|
2017-11-19 23:28:27 +08:00
|
|
|
json j;
|
|
|
|
|
|
|
|
auto jsonfail=[&](int errcode,const std::string& errmsg)
|
|
|
|
{
|
|
|
|
j["success"]=0;
|
|
|
|
j["errcode"]=errcode;
|
|
|
|
j["errmsg"]=errmsg;
|
|
|
|
};
|
|
|
|
|
2017-11-20 08:15:49 +08:00
|
|
|
do
|
2017-11-19 23:28:27 +08:00
|
|
|
{
|
2017-11-20 08:15:49 +08:00
|
|
|
if(se.isNew()||se.getUser().empty())
|
|
|
|
{
|
|
|
|
jsonfail(1,"Please Login First");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(req.requestMethod!="POST")
|
|
|
|
{
|
|
|
|
jsonfail(2,"Unsupported request method");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(req.post["booktype"].empty())
|
|
|
|
{
|
|
|
|
jsonfail(3,"Missing Parameters");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
postval(booktype);
|
|
|
|
|
|
|
|
DBInfo db;
|
|
|
|
MySQLConn conn;
|
|
|
|
if(db.readConfig()<0)
|
|
|
|
{
|
|
|
|
jsonfail(4,"Failed to read config");
|
|
|
|
break;
|
|
|
|
}
|
2017-11-19 23:28:27 +08:00
|
|
|
|
|
|
|
if(db.connectProxy(conn)<0)
|
|
|
|
{
|
|
|
|
jsonfail(5,"Failed to connect DB");
|
2017-11-20 08:15:49 +08:00
|
|
|
break;
|
2017-11-19 23:28:27 +08:00
|
|
|
}
|
2017-11-20 08:15:49 +08:00
|
|
|
|
|
|
|
int permission_level;
|
|
|
|
if(conn.exec(make_str("select permission_level from bs_user where username='",
|
|
|
|
se.getUser(),
|
|
|
|
"'"),
|
|
|
|
[&](MySQLResult& res)
|
|
|
|
{
|
|
|
|
res.stepRow([&](char** val,unsigned long* len)
|
|
|
|
{
|
|
|
|
permission_level=ParseInt(val[0]);
|
|
|
|
});
|
|
|
|
})<0)
|
2017-11-19 23:28:27 +08:00
|
|
|
{
|
2017-11-20 08:15:49 +08:00
|
|
|
jsonfail(6,"Failed to execute SQL");
|
|
|
|
break;
|
2017-11-19 23:28:27 +08:00
|
|
|
}
|
2017-11-19 15:43:47 +08:00
|
|
|
|
2017-11-20 08:15:49 +08:00
|
|
|
if(permission_level>1)
|
2017-11-19 23:28:27 +08:00
|
|
|
{
|
2017-11-20 08:15:49 +08:00
|
|
|
/// Permission Denied.
|
|
|
|
jsonfail(7,"Permission denied");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(conn.exec(make_str("insert into bs_booktype values ('",
|
|
|
|
booktype,
|
|
|
|
"')"),nullptr)<0)
|
|
|
|
{
|
|
|
|
jsonfail(8,"Failed to execute SQL");
|
|
|
|
break;
|
2017-11-19 23:28:27 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-11-20 08:15:49 +08:00
|
|
|
if(conn.getAffectedRows()!=1)
|
2017-11-19 23:28:27 +08:00
|
|
|
{
|
2017-11-20 08:15:49 +08:00
|
|
|
jsonfail(9,"insert affects not equals 1");
|
|
|
|
break;
|
2017-11-19 23:28:27 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-11-20 08:15:49 +08:00
|
|
|
j["success"]=1;
|
2017-11-19 23:28:27 +08:00
|
|
|
}
|
|
|
|
}
|
2017-11-20 08:15:49 +08:00
|
|
|
|
|
|
|
}while(0);
|
|
|
|
|
|
|
|
res.content.append(j.dump());
|
|
|
|
res.show();
|
2017-11-19 15:43:47 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|