#include "Session.h"
#include "Util.h"
#include "json.hpp"
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 std::string& errmsg)
j["success"]=0;
j["errcode"]=errcode;
j["errmsg"]=errmsg;
};
if(se.isNew()||se.getUser().empty())
jsonfail(1,"Please Login First");
}
DBInfo db;
MySQLConn conn;
if(db.readConfig()<0)
jsonfail(4,"Failed to read config");
else
if(db.connectProxy(conn)<0)
jsonfail(5,"Failed to connect DB");
if(conn.exec(make_str("select permission_level from bs_user where username='",
se.getUser(),
"'"),/// TODO
))
if(req.requestMethod!="POST")
jsonfail(2,"Unsupported request method");
if(req.post["booktype"].empty())
jsonfail(3,"Missing Parameters");
postval(booktype);
if(conn.exec(make_str("insert into bs_booktype values ('",
booktype,
"')"),nullptr)<0)
jsonfail(6,"Failed to execute SQL");
if(conn.getAffectedRows()!=1)
jsonfail(7,"insert affects not equals 1");
j["success"]=1;
return 0;