Update DBHomework.

And I'm gonna to sleep now.
This commit is contained in:
Kirigaya Kazuto 2017-11-19 23:28:27 +08:00
parent 4d0aad04ff
commit d4ee2b3edb
6 changed files with 299 additions and 44 deletions

View File

@ -14,11 +14,11 @@
"MySQLWrapper.h" "MySQLWrapper.h"
"json.hpp" "json.hpp"
1510879577 d:\codeblocks_codes\dbhomework\mysqlwrapper\mysqlwrapper.h 1511076615 d:\codeblocks_codes\dbhomework\mysqlwrapper\mysqlwrapper.h
<functional> <functional>
<string> <string>
1510879577 source:d:\codeblocks_codes\dbhomework\mysqlwrapper\mysqlwrapper.cpp 1511076563 source:d:\codeblocks_codes\dbhomework\mysqlwrapper\mysqlwrapper.cpp
"MySQLWrapper.h" "MySQLWrapper.h"
"MySQLInclude.h" "MySQLInclude.h"
<string> <string>
@ -191,7 +191,7 @@
1510881771 source:d:\codeblocks_codes\dbhomework\httpwrapper\sessionmanager.cpp 1510881771 source:d:\codeblocks_codes\dbhomework\httpwrapper\sessionmanager.cpp
"SessionManager.h" "SessionManager.h"
1510901514 source:d:\codeblocks_codes\dbhomework\httpwrapper\session.cpp 1511102541 source:d:\codeblocks_codes\dbhomework\httpwrapper\session.cpp
"Session.h" "Session.h"
"Singleton.hpp" "Singleton.hpp"
"Util.h" "Util.h"
@ -200,7 +200,7 @@
<cstdlib> <cstdlib>
<cstring> <cstring>
1510901603 d:\codeblocks_codes\dbhomework\httpwrapper\session.h 1511102538 d:\codeblocks_codes\dbhomework\httpwrapper\session.h
"Request.h" "Request.h"
"Response.h" "Response.h"
<string> <string>
@ -224,3 +224,8 @@
"Util.h" "Util.h"
"json.hpp" "json.hpp"
1511102500 source:d:\codeblocks_codes\dbhomework\addbooktype.cpp
"Session.h"
"Util.h"
"json.hpp"

View File

@ -2,19 +2,24 @@
<CodeBlocks_layout_file> <CodeBlocks_layout_file>
<FileVersion major="1" minor="0" /> <FileVersion major="1" minor="0" />
<ActiveTarget name="Release" /> <ActiveTarget name="Release" />
<File name="HTTPWrapper\Request.h" open="1" top="0" tabpos="9" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> <File name="HTTPWrapper\Request.h" open="1" top="0" tabpos="10" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor> <Cursor>
<Cursor1 position="159" topLine="0" /> <Cursor1 position="159" topLine="0" />
</Cursor> </Cursor>
</File> </File>
<File name="HTTPWrapper\Session.h" open="1" top="0" tabpos="5" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> <File name="HTTPWrapper\Singleton.hpp" open="1" top="0" tabpos="5" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor> <Cursor>
<Cursor1 position="562" topLine="7" /> <Cursor1 position="21" topLine="27" />
</Cursor> </Cursor>
</File> </File>
<File name="HTTPWrapper\Response.cpp" open="1" top="0" tabpos="11" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> <File name="HTTPWrapper\Util.h" open="1" top="0" tabpos="8" split="0" active="1" splitpos="0" zoom_1="1" zoom_2="0">
<Cursor> <Cursor>
<Cursor1 position="560" topLine="30" /> <Cursor1 position="867" topLine="12" />
</Cursor>
</File>
<File name="HTTPWrapper\Response.h" open="1" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="76" topLine="0" />
</Cursor> </Cursor>
</File> </File>
<File name="MySQLWrapper\MySQLInclude.h" open="0" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> <File name="MySQLWrapper\MySQLInclude.h" open="0" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
@ -22,32 +27,47 @@
<Cursor1 position="66" topLine="0" /> <Cursor1 position="66" topLine="0" />
</Cursor> </Cursor>
</File> </File>
<File name="HTTPWrapper\Util.h" open="1" top="0" tabpos="7" split="0" active="1" splitpos="0" zoom_1="1" zoom_2="0"> <File name="MySQLWrapper\MySQLWrapper.h" open="1" top="0" tabpos="13" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor> <Cursor>
<Cursor1 position="867" topLine="12" /> <Cursor1 position="562" topLine="20" />
</Cursor> </Cursor>
</File> </File>
<File name="HTTPWrapper\Request.cpp" open="1" top="0" tabpos="10" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> <File name="addbooktype.cpp" open="1" top="1" tabpos="3" split="0" active="1" splitpos="0" zoom_1="3" zoom_2="0">
<Cursor>
<Cursor1 position="983" topLine="32" />
</Cursor>
</File>
<File name="HTTPWrapper\Request.cpp" open="1" top="0" tabpos="11" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor> <Cursor>
<Cursor1 position="3128" topLine="92" /> <Cursor1 position="3128" topLine="92" />
</Cursor> </Cursor>
</File> </File>
<File name="HTTPWrapper\Singleton.hpp" open="1" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> <File name="HTTPWrapper\Response.cpp" open="1" top="0" tabpos="12" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor> <Cursor>
<Cursor1 position="21" topLine="27" /> <Cursor1 position="560" topLine="30" />
</Cursor> </Cursor>
</File> </File>
<File name="HTTPWrapper\Util.cpp" open="1" top="0" tabpos="8" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> <File name="HTTPWrapper\Session.h" open="1" top="0" tabpos="6" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="579" topLine="7" />
</Cursor>
</File>
<File name="MySQLWrapper\MySQLWrapper.cpp" open="1" top="0" tabpos="14" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="2643" topLine="96" />
</Cursor>
</File>
<File name="HTTPWrapper\Util.cpp" open="1" top="0" tabpos="9" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor> <Cursor>
<Cursor1 position="3294" topLine="132" /> <Cursor1 position="3294" topLine="132" />
</Cursor> </Cursor>
</File> </File>
<File name="addbooktype.cpp" open="1" top="1" tabpos="14" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> <File name="HTTPWrapper\Session.cpp" open="1" top="0" tabpos="7" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor> <Cursor>
<Cursor1 position="192" topLine="0" /> <Cursor1 position="2514" topLine="117" />
</Cursor> </Cursor>
</File> </File>
<File name="HTTPWrapper\CookieVec.cpp" open="1" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> <File name="HTTPWrapper\CookieVec.cpp" open="1" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor> <Cursor>
<Cursor1 position="960" topLine="0" /> <Cursor1 position="960" topLine="0" />
</Cursor> </Cursor>
@ -57,24 +77,4 @@
<Cursor1 position="2013" topLine="10" /> <Cursor1 position="2013" topLine="10" />
</Cursor> </Cursor>
</File> </File>
<File name="HTTPWrapper\Response.h" open="1" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="76" topLine="0" />
</Cursor>
</File>
<File name="HTTPWrapper\Session.cpp" open="1" top="0" tabpos="6" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="2717" topLine="117" />
</Cursor>
</File>
<File name="MySQLWrapper\MySQLWrapper.cpp" open="1" top="0" tabpos="13" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="2643" topLine="96" />
</Cursor>
</File>
<File name="MySQLWrapper\MySQLWrapper.h" open="1" top="0" tabpos="12" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="562" topLine="20" />
</Cursor>
</File>
</CodeBlocks_layout_file> </CodeBlocks_layout_file>

@ -1 +1 @@
Subproject commit d6d82ae0d96c0c0c0ef800dbe6aa6871170b6236 Subproject commit d4ce5626c4b6eb6e36b59c0811733c8ddf4001ae

View File

@ -0,0 +1,167 @@
#include "Session.h"
#include "Util.h"
#include "json.hpp"
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 std::string& errmsg)
{
j["success"]=0;
j["errcode"]=errcode;
j["errmsg"]=errmsg;
};
if(se.isNew()||se.getUser().empty())
{
jsonfail(1,"Please Login First");
}
if(req.requestMethod=="POST")
{
if(req.post["isbn"].empty() ||
req.post["bookname"].empty() ||
req.post["booktype"].empty() ||
req.post["author"].empty() ||
req.post["publisher"].empty() ||
req.post["pubdate"].empty() ||
req.post["status"].empty() )
{
jsonfail(3,"Missing parameter");
}
else
{
postval(isbn);
postval(bookname);
postval(booktype);
postval(author);
postval(publisher);
postval(pubdate);
postval(status);
DBInfo db;
if(db.readConfig()<0)
{
jsonfail(5,"Failed to read config");
}
else
{
MySQLConn conn;
if(db.connectProxy(conn)<0)
{
jsonfail(6,"Failed to connect DB");
}
else
{
int counter;
if(conn.exec("select count(class_id) from bs_book",[&](MySQLResult& res)
{
res.stepRow([&](char** val,unsigned long* len)
{
counter=ParseInt(val[0]);
});
})<0)
{
jsonfail(7,"Failed to execute SQL at Step 1");
}
else
{
if(counter==0)
{
/// First book, class_id = 1, goto step 3 directly.
if(conn.exec(make_str("insert into bs_book values (1,'",
isbn,
"','",
bookname,
"','",
booktype,
"','",
author,
"','",
publisher,
"','",
pubdate,
"',2)"),nullptr)<0)
{
jsonfail(9,"Failed to execute SQL at step 3");
}
else
{
if(conn.getAffectedRows()!=1)
{
jsonfail(10,"Failed to execute SQL at step 4");
}
else
{
j["success"]=1;
}
}
}
else
{
/// Try to find the max class id.
int maxId;
if(conn.exec("select max(class_id) from bs_book ",[&](MySQLResult& res)
{
res.stepRow([&](char** val,unsigned long* len)
{
maxId=ParseInt(val[0]);
});
})<0)
{
jsonfail(8,"Failed to execute SQL at step 2");
}
/// Give new book.class_id as maxId+1
if(conn.exec(make_str("insert into bs_book values (",
maxId+1,
",'",
isbn,
"','",
bookname,
"','",
booktype,
"','",
author,
"','",
publisher,
"','",
pubdate,
"',2)"),nullptr)<0)
{
jsonfail(9,"Failed to execute SQL at step 3");
}
else
{
if(conn.getAffectedRows()!=1)
{
jsonfail(10,"Failed to execute SQL at step 4");
}
else
{
j["success"]=1;
}
}
}
}
}
}
}
}
else
{
jsonfail(2,"Unsupported request method.");
}
res.content.append(j.dump());
res.show();
return 0;
}

View File

@ -2,14 +2,97 @@
#include "Util.h" #include "Util.h"
#include "json.hpp" #include "json.hpp"
using namespace std; using namespace std;
using json = nlohmann::json; using json=nlohmann::json;
#define postval(NAME) string NAME=req.post[#NAME]
int main() int main()
{ {
Request req; Request req;
Session se(req); Session se(req);
Response res; Response res;
json j;
auto jsonfail=[&](int errcode,const std::string& errmsg)
{
j["success"]=0;
j["errcode"]=errcode;
j["errmsg"]=errmsg;
};
if(se.isNew()||se.getUser().empty())
{
jsonfail(1,"Please Login First");
}
DBInfo db;
MySQLConn conn;
if(db.readConfig()<0)
{
jsonfail(4,"Failed to read config");
}
else
{
if(db.connectProxy(conn)<0)
{
jsonfail(5,"Failed to connect DB");
}
else
{
if(conn.exec(make_str("select permission_level from bs_user where username='",
se.getUser(),
"'"),/// TODO
))
}
if(req.requestMethod!="POST")
{
jsonfail(2,"Unsupported request method");
}
else
{
if(req.post["booktype"].empty())
{
jsonfail(3,"Missing Parameters");
}
else
{
postval(booktype);
DBInfo db;
if(db.readConfig()<0)
{
jsonfail(4,"Failed to read config");
}
else
{
MySQLConn conn;
if(db.connectProxy(conn)<0)
{
jsonfail(5,"Failed to connect DB");
}
else
{
if(conn.exec(make_str("insert into bs_booktype values ('",
booktype,
"')"),nullptr)<0)
{
jsonfail(6,"Failed to execute SQL");
}
else
{
if(conn.getAffectedRows()!=1)
{
jsonfail(7,"insert affects not equals 1");
}
else
{
j["success"]=1;
}
}
}
}
}
}
return 0; return 0;
} }

View File

@ -64,7 +64,7 @@ create table bs_booktype
); );
create table bs_book create table bs_book
( (
class_id varchar(20) primary key, class_id integer primary key,
isbn varchar(13) unique, isbn varchar(13) unique,
name varchar(20), name varchar(20),
book_type varchar(10) references bs_booktype(book_type), book_type varchar(10) references bs_booktype(book_type),
@ -75,15 +75,15 @@ create table bs_book
); );
create table bs_bookstatus create table bs_bookstatus
( (
book_id varchar(25) primary key, book_id integer primary key,
class_id varchar(20) references bs_book(class_id), class_id integer references bs_book(class_id),
position varchar(30), position varchar(30),
status integer references bs_meta_bookstatus(status) status integer references bs_meta_bookstatus(status)
); );
create table bs_borrow create table bs_borrow
( (
username varchar(10) references bs_user(username), username varchar(10) references bs_user(username),
book_id varchar(25) references bs_bookstatus(book_id), book_id integer references bs_bookstatus(book_id),
borrow_time date not null, borrow_time date not null,
return_deadline date not null, return_deadline date not null,
return_time date return_time date