-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelete.cpp
85 lines (76 loc) · 1.97 KB
/
delete.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <fstream>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fastcgi++/request.hpp>
#include <fastcgi++/manager.hpp>
#include <fastcgi++/http.hpp>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include "connector.hpp"
void error_log(const char* msg)
{
using namespace std;
using namespace boost;
static ofstream error;
if(!error.is_open())
{
error.open("/tmp/errlog", ios_base::out | ios_base::app);
error.imbue(locale(error.getloc(), new posix_time::time_facet()));
}
error << '[' << posix_time::second_clock::local_time() << "] " << msg << endl;
}
class DeleteBook : public Connector
{
inline void sendError(const std::string& errorMsg)
{
out << "{ \"success\" : 0, \"message\" : \"" + errorMsg + "\" }" << std::endl;
}
inline void sendSuccess()
{
out << "{ \"success\" : 1 }" << std::endl;
}
bool response()
{
out << "Content-Type: application/json; charset=ISO-8859-1\r\n\r\n";
std::map<std::string, std::string> parameters;
for (Fastcgipp::Http::Environment<char>::Posts::const_iterator it = environment().posts.begin(); it != environment().posts.end(); ++it)
{
parameters[it->first] = it->second.value;
}
if (parameters.find("id") == parameters.end())
{
sendError("Missing id");
}
else
{
sql::Statement* stmt = con->createStatement();
try
{
stmt->execute("DELETE FROM book WHERE id = " + parameters["id"]);
sendSuccess();
} catch (sql::SQLException& e)
{
sendError(e.what());
}
delete stmt;
}
return true;
}
};
int main()
{
try
{
Fastcgipp::Manager<DeleteBook> fcgi;
fcgi.handler();
}
catch (std::exception& e)
{
error_log(e.what());
}
return 0;
}