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/old_src/doAddBook.cpp
2017-12-28 18:18:44 +08:00

122 lines
2.8 KiB
C++

#include "bs_util.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(book_isbn);
postval(book_type);
postval(book_name);
postval(book_author);
postval(book_publish);
postval(book_pubdate);
postval(book_status);
int book_status_real=ParseInt(book_status);
if(book_status_real<0)
{
jsonfail(err_parameter,"Failed to parse status");
break;
}
if(book_status_real<0||book_status_real>2)
{
jsonfail(err_parameter,"Invalid Status");
break;
}
startdb();
/// 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_data,"Failed to get permission level");
break;
}
if(permission_level>2)
{
jsonfail(err_permission_denied,"Permission Not Reach Required Level");
break;
}
int result_code=-1;
if(conn.exec(make_str("call newbook('",
book_isbn,
"','",
book_name,
"','",
book_type,
"','",
book_author,
"','",
book_publish,
"','",
book_pubdate,
"',",
book_status_real,
")"),
SQLParseInt(result_code))<0)
{
jsonfail(err_sql,"Procedure 1");
break;
}
if(result_code==1)
{
/// Failed
jsonfail(err_sql_logic,"SQL Operation Failed");
break;
}
j["success"]=1;
}
while(0);
se.writeToResponse(res);
res.content.append(j.dump());
return 0;
}