diff --git a/CMakeLists.txt b/CMakeLists.txt index 3b97205..2838621 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,10 +17,11 @@ add_executable(${PROJECT_NAME} main.cpp ) add_subdirectory(third_party/xxHash/cmake_unofficial third_party/xxHash/build EXCLUDE_FROM_ALL) target_sources(${PROJECT_NAME} PRIVATE - src/json_filter.cpp src/bank_api.cpp + src/bank_resp.cpp src/bank.cpp src/change_flag.cpp + src/json_filter.cpp src/log.cpp src/transaction.cpp src/user_filter.cpp diff --git a/include/bank.h b/include/bank.h index 2357fd2..f01bdde 100644 --- a/include/bank.h +++ b/include/bank.h @@ -2,16 +2,14 @@ #include //temporary #include #include -#include #include +#include "bank_resp.h" #include "user.h" #if (CONSERVATIVE_DISK_SAVE && MAX_LOG_SIZE < 0) && !MULTI_THREADED #include "change_flag.h" #endif -using BankResponse = std::pair; - class Bank { #if MULTI_THREADED diff --git a/include/bank_api.h b/include/bank_api.h index 063be5a..c1e540d 100644 --- a/include/bank_api.h +++ b/include/bank_api.h @@ -37,6 +37,7 @@ public: void AdminAddUser(req_args) const; void DelUser(req_args) const; void AdminDelUser(req_args) const; + #endif METHOD_LIST_BEGIN diff --git a/include/json_filter.h b/include/json_filter.h index a6038ee..ffa9a05 100644 --- a/include/json_filter.h +++ b/include/json_filter.h @@ -1,5 +1,6 @@ #pragma once #include +#include "bank_resp.h" using namespace drogon; diff --git a/src/bank.cpp b/src/bank.cpp index 4556a2c..a15d792 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -56,15 +56,15 @@ BankResponse Bank::GetBal(const std::string &name) const noexcept { uint32_t res = 0; users.if_contains(name, [&res](const User &u) { res = u.balance + 1; }); - return res ? BankResponse(k200OK, res - 1) : BankResponse(k404NotFound, "User not found"); + return res ? BankResponse(k200OK, std::to_string(res - 1)) : BankResponse(k404NotFound, "\"User not found\""); } BankResponse Bank::GetLogs(const std::string &name) noexcept { BankResponse res; #if MAX_LOG_SIZE > 0 - if (!users.modify_if(name, [&res](User &u) { res = {k200OK, u.log.GetLog()}; })) + if (!users.modify_if(name, [&res](User &u) { res = BankResponse(k200OK, u.log.GetLog().toStyledString()); })) { - return BankResponse(k404NotFound, "User not found"); + return BankResponse(k404NotFound, "\"User not found\""); } #endif return res; @@ -74,19 +74,19 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam //cant send money to self, from self or amount is 0 if (a_name == b_name) { - return {k400BadRequest, "Sender and Reciever names cannot match"}; + return {k400BadRequest, "\"Sender and Reciever names cannot match\""}; } //cant send 0 if (!amount) { - return {k400BadRequest, "Amount being sent cannot be 0"}; + return {k400BadRequest, "\"Amount being sent cannot be 0\""}; } //as first modify_if checks a_name and grabs unique lock if (!Contains(b_name)) { - return {k404NotFound, "Reciever does not exist"}; + return {k404NotFound, "\"Reciever does not exist\""}; } BankResponse state; @@ -100,7 +100,7 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam //if A can afford it if (a.balance < amount) { - state = {k400BadRequest, "Sender has insufficient funds"}; + state = BankResponse(k400BadRequest, "\"Sender has insufficient funds\""); } else { @@ -108,11 +108,11 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam #if MAX_LOG_SIZE > 0 a.log.AddTrans(Transaction(temp)); //about 40% of this function's cost #endif - state = {k200OK, "Transfer successful!"}; + state = BankResponse(k200OK, "\"Transfer successful!\""); } })) { - return {k404NotFound, "Sender does not exist"}; + return {k404NotFound, "\"Sender does not exist\""}; } if (state.first == k200OK) { @@ -164,18 +164,18 @@ BankResponse Bank::SetBal(const std::string &name, uint32_t amount) noexcept save_flag = true; #endif #endif - return {k200OK, "Balance set!"}; + return {k200OK, "\"Balance set!\""}; } else { - return {k404NotFound, "User not found"}; + return {k404NotFound, "\"User not found\""}; } } BankResponse Bank::ImpactBal(const std::string &name, int64_t amount) noexcept { if (amount == 0) { - return {k400BadRequest, "Amount cannot be 0"}; + return {k400BadRequest, "\"Amount cannot be 0\""}; } if (users.modify_if(name, [amount](User &u) { u.balance += amount; })) { @@ -186,11 +186,11 @@ BankResponse Bank::ImpactBal(const std::string &name, int64_t amount) noexcept save_flag = true; #endif #endif - return {k200OK, "Balance added!"}; + return {k200OK, "\"Balance added!\""}; } else { - return {k404NotFound, "User not found"}; + return {k404NotFound, "\"User not found\""}; } } bool Bank::Contains(const std::string &name) const noexcept @@ -206,7 +206,7 @@ BankResponse Bank::AddUser(std::string &&name, uint32_t init_bal, std::string && { if (!ValidUsrname(name)) { - return {k400BadRequest, "Invalid Name, breaks size and/or character restrictions"}; + return {k400BadRequest, "\"Invalid Name, breaks size and/or character restrictions\""}; } std::shared_lock lock{save_lock}; if (users.try_emplace_l( @@ -219,11 +219,11 @@ BankResponse Bank::AddUser(std::string &&name, uint32_t init_bal, std::string && save_flag = true; #endif #endif - return {k200OK, "User added!"}; + return {k200OK, "\"User added!\""}; } else { - return {k409Conflict, "User already exists"}; + return {k409Conflict, "\"User already exists\""}; } } BankResponse Bank::DelUser(const std::string &name) noexcept @@ -250,11 +250,11 @@ BankResponse Bank::DelUser(const std::string &name) noexcept save_flag = true; #endif #endif - return BankResponse(k200OK, "User deleted!"); + return BankResponse{k200OK, "\"User deleted!\""}; } else { - return BankResponse(k404NotFound, "User not found"); + return BankResponse{k404NotFound, "\"User not found\""}; } } void Bank::Save() diff --git a/src/bank_api.cpp b/src/bank_api.cpp index 0d7bfd9..cbe862c 100644 --- a/src/bank_api.cpp +++ b/src/bank_api.cpp @@ -24,7 +24,7 @@ template <> HttpResponsePtr drogon::toResponse(BankResponse &&data) { auto res = std::make_shared(data.first, CT_APPLICATION_JSON); - res->setJsonObject(std::move(data.second)); + res->setBody(std::move(data.second)); auto &advices = HttpAppFrameworkImpl::instance().getResponseCreationAdvices(); if (!advices.empty()) { diff --git a/src/json_filter.cpp b/src/json_filter.cpp index dd0e161..efa01ff 100644 --- a/src/json_filter.cpp +++ b/src/json_filter.cpp @@ -1,7 +1,5 @@ #include "json_filter.h" -using BankResponse = std::pair; - template <> JsonFilter::JsonFilter() {} template <>