This repository has been archived on 2021-11-25. You can view files and clone it, but cannot push or open issues or pull requests.
DBHomework/removebook.cpp
2017-11-20 14:46:51 +08:00

99 lines
2.1 KiB
C++

#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.isNew()||se.getUser().empty())
{
jsonfail(err_need_login);
break;
}
if(req.requestMethod!="POST")
{
jsonfail(err_method_not_supported);
break;
}
if(req.post["book_key"].empty())
{
jsonfail(err_missing_parameter);
break;
}
/// Connect to DB
DBInfo db;
MySQLConn conn;
if(db.readConfig()<0)
{
jsonfail(err_config);
break;
}
if(db.connectProxy(conn)<0)
{
jsonfail(err_connect);
break;
}
/// Check Permission
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)
{
jsonfail(err_sql,"Step 1");
break;
}
if(permission_level>1)
{
/// Permission Denied.
jsonfail(err_permission_denied);
break;
}
/// TODO:
}while(0);
res.content.append(j.dump());
res.show();
return 0;
}