From 3d3e0e176005a24a9768a23d87c6b2d6129589fa Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Sun, 11 Dec 2016 13:51:13 +0800 Subject: [PATCH] login to the service uses mysql now. --- main.cpp | 58 ++++++++++++++++++---------------------- src/MySqlConnector.h | 8 +++--- src/YourMoneroRequests.h | 28 ++++++++++++++++++- 3 files changed, 57 insertions(+), 37 deletions(-) diff --git a/main.cpp b/main.cpp index 40a2644..e706c09 100644 --- a/main.cpp +++ b/main.cpp @@ -15,38 +15,32 @@ using namespace restbed; int main() { - xmreg::MySqlAccounts xmr_accounts; - - //xmr_accounts.create_account("41vEA7Ye8Bpeda6g59v5t46koWrVn2PNgEKgzquJjmiKCFTsh9gajr8J3pad49rqu581TAtFGCH9CYTCkYrCpuWUG9GkgeB"); - - - string addr = "41vEA7Ye8Bpeda6g9v5t46koWrVn2PNgEKgluJjmiKCFTsh9gajr8J3pad49rqu581TAtFGCH9CYTCkYrCpuWUG9GkgeB"; - - xmreg::XmrAccount acc; - - bool r = xmr_accounts.select(addr, acc); - - if (r) - { - cout << "Account foudn: " << acc.id << endl; - - xmreg::XmrAccount new_acc = acc; - new_acc.total_received = 20e12; - - cout << xmr_accounts.update(acc, new_acc) << endl; - - } - else - { - cout << "Account does not exist" << endl; - cout << xmr_accounts.create(addr) << endl; - } - - - - - - +// xmreg::MySqlAccounts xmr_accounts; +// +// //xmr_accounts.create_account("41vEA7Ye8Bpeda6g59v5t46koWrVn2PNgEKgzquJjmiKCFTsh9gajr8J3pad49rqu581TAtFGCH9CYTCkYrCpuWUG9GkgeB"); +// +// +// string addr = "41vEA7Ye8Bpeda6g9v5t46koWrVn2PNgEKgluJjmiKCFTsh9gajr8J3pad49rqu581TAtFGCH9CYTCkYrCpuWUG9GkgeB"; +// +// xmreg::XmrAccount acc; +// +// bool r = xmr_accounts.select(addr, acc); +// +// if (r) +// { +// cout << "Account foudn: " << acc.id << endl; +// +// xmreg::XmrAccount new_acc = acc; +// new_acc.total_received = 20e12; +// +// cout << xmr_accounts.update(acc, new_acc) << endl; +// +// } +// else +// { +// cout << "Account does not exist" << endl; +// cout << xmr_accounts.create(addr) << endl; +// } xmreg::YourMoneroRequests::show_logs = true; diff --git a/src/MySqlConnector.h b/src/MySqlConnector.h index 8bd0163..64c29be 100644 --- a/src/MySqlConnector.h +++ b/src/MySqlConnector.h @@ -150,7 +150,7 @@ public: return false; } - bool + uint64_t create(const string& address) { Query query = conn.query(INSERT_STMT); @@ -161,15 +161,15 @@ public: SimpleResult sr = query.execute(address); if (sr.rows() == 1) - return true; + return sr.insert_id(); } catch (mysqlpp::Exception& e) { MYSQL_EXCEPTION_MSG(e); - return false; + return 0; } - return false; + return 0; } bool diff --git a/src/YourMoneroRequests.h b/src/YourMoneroRequests.h index 2035c8b..6fd85fc 100644 --- a/src/YourMoneroRequests.h +++ b/src/YourMoneroRequests.h @@ -88,7 +88,33 @@ public: if (show_logs) print_json_log("login request: ", j_request); - json j_response {{"new_address", true}}; + string xmr_address = j_request["address"]; + + // check if login address is new or existing + xmreg::MySqlAccounts xmr_accounts; + + xmreg::XmrAccount acc; + + uint64_t acc_id {0}; + + json j_response; + + if (xmr_accounts.select(xmr_address, acc)) + { + //cout << "Account found: " << acc.id << endl; + acc_id = acc.id; + j_response = {{"new_address", false}}; + } + else + { + //cout << "Account does not exist" << endl; + + if ((acc_id = xmr_accounts.create(xmr_address)) != 0) + { + //cout << "account created acc_id: " << acc_id << endl; + j_response = {{"new_address", true}}; + } + } string response_body = j_response.dump();