|
10 | 10 | #include "../include/mysql++.h" |
11 | 11 | #include "../include/StreamResponse.h" |
12 | 12 | #include "../include/options.h" |
| 13 | +#include "../include/row.h" |
13 | 14 | //#include <tchar.h> |
14 | 15 |
|
15 | 16 | using namespace std; |
@@ -124,11 +125,57 @@ class MyController : public WebController |
124 | 125 | } |
125 | 126 | }; |
126 | 127 |
|
| 128 | + void login(Request &request,StreamResponse &response) { |
| 129 | + string body = request.getData(); |
| 130 | + cout << "body = " << body << endl; |
| 131 | + Json::Reader reader; |
| 132 | + Json::Value value; |
| 133 | + |
| 134 | + if (reader.parse(body,value)) { |
| 135 | + string username = value["username"].asString(); |
| 136 | + string passwd = value["passwd"].asString(); |
| 137 | + |
| 138 | + cout << "username = " << username << endl; |
| 139 | + cout << "passwd = " << passwd << endl; |
| 140 | + |
| 141 | + Connection conn = Connection(false); |
| 142 | + conn.set_option(new SetCharsetNameOption("utf8")); |
| 143 | + conn.connect("cpp","127.0.0.1","root","root"); |
| 144 | + |
| 145 | + string sql = "select count(*) from t_user where username = '" + username + "' and passwd = '" + passwd + "'"; |
| 146 | + cout << "sql = " << sql << endl; |
| 147 | + Query query = conn.query(); |
| 148 | + query << sql; |
| 149 | + StoreQueryResult result = query.store(); |
| 150 | + //mysqlpp::StoreQueryResult::list_type::size_type r = result.num_rows(); |
| 151 | + // Row row = result.fetch_row(); |
| 152 | + // Row::const_reference count = row.at(0); |
| 153 | + //cout << "count = " << count << endl; |
| 154 | + //string field_name = result.field_name(0); |
| 155 | + //cout << "field_name = " << field_name << endl; |
| 156 | + // Row row = result.fetch_row(); |
| 157 | + // cout << row.begin() << endl; |
| 158 | + StoreQueryResult::const_iterator it = result.begin(); |
| 159 | + |
| 160 | + Row row = *it; |
| 161 | + if (it != result.end()) { |
| 162 | + if (row[0]) { |
| 163 | + response << "0" << endl; |
| 164 | + } else { |
| 165 | + response << "1" << endl; |
| 166 | + } |
| 167 | + //cout << "cout = " << row[0] << endl; |
| 168 | + } |
| 169 | + // cout << "count = " << *it[0] << endl; |
| 170 | + } |
| 171 | + } |
| 172 | + |
127 | 173 | void setup() |
128 | 174 | { |
129 | 175 | // addRoute("GET", "/hello", MyController, hello); |
130 | 176 | addRoute("POST","/hello",MyController,hello); |
131 | 177 | addRoute("POST","/reg",MyController,reg); |
| 178 | + addRoute("POST","/login",MyController,login); |
132 | 179 | } |
133 | 180 |
|
134 | 181 |
|
|
0 commit comments