125 lines
2.8 KiB
C++
125 lines
2.8 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);
|
|
int id_real=ParseInt(id);
|
|
if(id_real<0)
|
|
{
|
|
jsonfail(err_parameter,"Failed to parse id");
|
|
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)
|
|
{
|
|
jsonfail(err_general,"Failed to check Permission");
|
|
break;
|
|
}
|
|
|
|
if(permission_level>=3)
|
|
{
|
|
jsonfail(err_permission_denied);
|
|
break;
|
|
}
|
|
|
|
/// Check if the book info can be removed.
|
|
/// count_val: Count of books with class_id={ID} and status is borrowed (0)
|
|
int count_val=-1;
|
|
if(conn.exec(make_str("select count(book_id) from bs_bookstatus where class_id=",
|
|
id_real,
|
|
" and status=0"),
|
|
SQLParseInt(count_val))<0)
|
|
{
|
|
jsonfail(err_sql,"Step 2");
|
|
break;
|
|
}
|
|
|
|
if(count_val<0)
|
|
{
|
|
jsonfail(err_data,"Failed to get book id data");
|
|
break;
|
|
}
|
|
|
|
if(count_val!=0)
|
|
{
|
|
jsonfail(err_data,make_str("Cannot remove book. Still ",count_val," books borrowed."));
|
|
break;
|
|
}
|
|
|
|
if(conn.exec(make_str("delete from bs_borrow where book_id in (select book_id from bs_bookstatus where class_id=",id_real),nullptr)<0)
|
|
{
|
|
jsonfail(err_sql,"Step 3");
|
|
}
|
|
|
|
if(conn.exec(make_str("delete from bs_bookstatus where class_id=",id_real),nullptr)<0)
|
|
{
|
|
jsonfail(err_sql,"Step 4");
|
|
break;
|
|
}
|
|
|
|
if(conn.exec(make_str("delete from bs_book where class_id=",id_real),nullptr)<0)
|
|
{
|
|
jsonfail(err_sql,"Step 5");
|
|
break;
|
|
}
|
|
|
|
ts.commit();
|
|
j["success"]=1;
|
|
}
|
|
while(0);
|
|
|
|
se.writeToResponse(res);
|
|
res.content.append(j.dump());
|
|
|
|
return 0;
|
|
}
|
|
|
|
|