#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()||se.isNew()||se.getUser().empty()) { jsonfail(err_need_login); break; } if(req.requestMethod!="POST") { jsonfail(err_method_not_supported); break; } if(req.post["booktype"].empty()) { jsonfail(err_missing_parameter); break; } postval(booktype); /// Connect 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; } /// Check if correct int countvalue; if(conn.exec(make_str("select count(book_type) from bs_booktype where book_type='", booktype, "'"), [&](MySQLResult& res) { res.stepRow([&](char** val,unsigned long* len) { countvalue=ParseInt(val[0]); }); })<0) { jsonfail(err_sql,"Step 2"); break; } if(countvalue!=1) { jsonfail(err_data,"value incorrect"); break; } /// Check ref int count_ref; if(conn.exec(make_str("select count(book_type) from bs_book where book_type='", booktype, "'"), [&](MySQLResult& res) { res.stepRow([&](char** val,unsigned long* len) { count_ref=ParseInt(val[0]); }); })<0) { jsonfail(err_sql,"Step 3"); break; } if(count_ref!=0) { jsonfail(err_data,"value reference count not equals 0"); break; } /// Do delete if(conn.exec(make_str("delete from bs_booktype where book_type='", booktype, "'"),nullptr)<0) { jsonfail(err_sql,"Step 4"); break; } if(conn.getAffectedRows()!=1) { jsonfail(err_sql_logic,"Affected rows not equals 1"); break; } /// Success j["success"]=1; }while(0); res.content.append(j.dump()); res.show(); return 0; }