Add editbooktype

This commit is contained in:
Kirigaya Kazuto 2017-11-20 09:08:11 +08:00
parent 2a31a8676f
commit 17f00f981b

View File

@ -0,0 +1,175 @@
#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_old"].empty() ||
req.post["booktype_new"].empty())
{
jsonfail(err_missing_parameter);
break;
}
postval(booktype_old);
postval(booktype_new);
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;
}
int count_old;
if(conn.exec(make_str("select count(book_type) from bs_booktype where book_type='",
booktype_old,
"'"),
[&](MySQLResult& res)
{
res.stepRow([&](char** val,unsigned long* len)
{
count_old=ParseInt(val[0]);
});
})<0)
{
jsonfail(err_sql,"Step 2");
break;
}
if(count_old!=1)
{
jsonfail(err_data,"old value incorrect");
break;
}
int count_new;
if(conn.exec(make_str("select count(book_type) from bs_booktype where booktype='",
booktype_new,
"'"),
[&](MySQLResult& res)
{
res.stepRow([&](char** val,unsigned long* len)
{
count_new=ParseInt(val[0]);
});
})<0)
{
jsonfail(err_sql,"Step 3");
break;
}
if(count_new!=0)
{
jsonfail(err_data,"new value conflict");
break;
}
int count_old_ref;
if(conn.exec(make_str("select count(book_type) from bs_book where book_type='",
booktype_old,
"'"),
[&](MySQLResult& res)
{
res.stepRow([&](char** val,unsigned long* len)
{
count_old_ref=ParseInt(val[0]);
});
})<0)
{
jsonfail(err_sql,"Step 4");
break;
}
if(count_old_ref!=0)
{
jsonfail(err_data,"old value reference count not equals 0");
break;
}
/// do update
if(conn.exec(make_str("update bs_booktype set book_type='",
booktype_new,
"' where book_type='",
booktype_old,
"'"),nullptr)<0)
{
jsonfail(err_sql,"Step 5");
break;
}
if(conn.getAffectedRows()!=1)
{
jsonfail(err_sql_logic,"Affected rows not equals 1");
break;
}
}while(0);
res.content.append(j.dump());
res.show();
return 0;
}