This repository has been archived on 2021-11-25. You can view files and clone it, but cannot push or open issues/pull-requests.
DBHomework/old_src/doListBook.cpp

89 lines
2.0 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(startid);
postval(amount);
int startid_real=ParseInt(startid);
if(startid_real<0)
{
jsonfail(err_parameter,"Failed to parse startid");
break;
}
int amount_real=ParseInt(amount);
if(amount_real<0)
{
jsonfail(err_parameter,"Failed to parse amount");
break;
}
/// Limit to 50.
if(amount_real>50) amount_real=50;
startdb();
if(conn.exec(make_str("select class_id,name,book_type from bs_book where class_id>=",
startid_real,
" order by class_id limit ",
amount_real),
[&](MySQLResult& res)
{
res.stepRow([&](char** val,unsigned long* len)
{
json s;
s["class_id"]=string(val[0]);
s["book_name"]=string(val[1]);
s["book_type"]=string(val[2]);
j["result"].push_back(s);
});
})<0)
{
jsonfail(err_sql,"Step 1");
break;
}
j["success"]=1;
}
while(0);
se.writeToResponse(res);
res.content.append(j.dump());
return 0;
}