142 lines
3.2 KiB
C++
142 lines
3.2 KiB
C++
#include "bs_util.h"
|
|
#include "MySQLTransaction.h"
|
|
using namespace std;
|
|
|
|
int main()
|
|
{
|
|
Request req;
|
|
Session se(req);
|
|
Response res;
|
|
json j;
|
|
|
|
auto jsonfail=[&](int errcode,const std::string& dtl="")
|
|
{
|
|
jsonfail_func(j,errcode,dtl);
|
|
};
|
|
|
|
do
|
|
{
|
|
if(!se.isReady())
|
|
{
|
|
jsonfail(err_session);
|
|
break;
|
|
}
|
|
|
|
if(se.getUser().empty())
|
|
{
|
|
jsonfail(err_need_login);
|
|
break;
|
|
}
|
|
|
|
if(req.requestMethod!="POST")
|
|
{
|
|
jsonfail(err_method_not_supported);
|
|
break;
|
|
}
|
|
|
|
postval(id);
|
|
postval(book_pos);
|
|
postval(bookobj_status);
|
|
|
|
int id_real=ParseInt(id);
|
|
if(id_real<0)
|
|
{
|
|
jsonfail(err_data,"Failed to parse id.");
|
|
break;
|
|
}
|
|
|
|
int bookobj_status_real=ParseInt(bookobj_status);
|
|
if(bookobj_status_real<0)
|
|
{
|
|
jsonfail(err_data,"Failed to parse obj status");
|
|
break;
|
|
}
|
|
|
|
startdb();
|
|
|
|
Transaction ts(conn);
|
|
|
|
/// Check Permission
|
|
int permission_level=-1;
|
|
if(conn.exec(make_str("select permission_level from bs_user where username='",
|
|
se.getUser(),
|
|
"'"),
|
|
SQLParseInt(permission_level))<0)
|
|
{
|
|
jsonfail(err_sql,"Step 1");
|
|
break;
|
|
}
|
|
|
|
if(permission_level<0 || permission_level>=3)
|
|
{
|
|
jsonfail(err_permission_denied);
|
|
break;
|
|
}
|
|
|
|
/// Check If this is the first book object
|
|
int count_val=-1;
|
|
if(conn.exec("select count(book_id) from bs_bookstatus",
|
|
SQLParseInt(count_val))<0)
|
|
{
|
|
jsonfail(err_sql,"Step 2");
|
|
break;
|
|
}
|
|
|
|
if(count_val<0)
|
|
{
|
|
jsonfail(err_general,"This error should not exist.");
|
|
break;
|
|
}
|
|
|
|
int current_maxbook_id=-1;
|
|
if(count_val==0)
|
|
{
|
|
current_maxbook_id=0;
|
|
}
|
|
else
|
|
{
|
|
/// Get Available BookObjID
|
|
|
|
if(conn.exec("select max(book_id) from bs_bookstatus",
|
|
SQLParseInt(current_maxbook_id))<0)
|
|
{
|
|
jsonfail(err_sql,"Step 3");
|
|
break;
|
|
}
|
|
|
|
if(current_maxbook_id<0)
|
|
{
|
|
jsonfail(err_data,"Failed to generate book id.");
|
|
break;
|
|
}
|
|
}
|
|
|
|
int nextbook_id=current_maxbook_id+1;
|
|
|
|
/// Insert the book.(TODO)
|
|
if(conn.exec(make_str("insert into bs_bookstatus values (",
|
|
nextbook_id,
|
|
",",
|
|
id_real,
|
|
",'",
|
|
book_pos,
|
|
"',",
|
|
bookobj_status_real,
|
|
")"),
|
|
nullptr)<0)
|
|
{
|
|
jsonfail(err_sql,"Step 4");
|
|
break;
|
|
}
|
|
|
|
ts.commit();
|
|
j["success"]=1;
|
|
}
|
|
while(0);
|
|
|
|
se.writeToResponse(res);
|
|
res.content.append(j.dump());
|
|
|
|
return 0;
|
|
}
|