mirror of
https://github.com/Expand-sys/CCash
synced 2026-03-22 12:37:08 +11:00
🔥racehorse: jsoncpp removed!
This commit is contained in:
parent
14618ac991
commit
f431d8bcda
7 changed files with 25 additions and 26 deletions
|
|
@ -17,10 +17,11 @@ add_executable(${PROJECT_NAME} main.cpp )
|
||||||
add_subdirectory(third_party/xxHash/cmake_unofficial third_party/xxHash/build EXCLUDE_FROM_ALL)
|
add_subdirectory(third_party/xxHash/cmake_unofficial third_party/xxHash/build EXCLUDE_FROM_ALL)
|
||||||
|
|
||||||
target_sources(${PROJECT_NAME} PRIVATE
|
target_sources(${PROJECT_NAME} PRIVATE
|
||||||
src/json_filter.cpp
|
|
||||||
src/bank_api.cpp
|
src/bank_api.cpp
|
||||||
|
src/bank_resp.cpp
|
||||||
src/bank.cpp
|
src/bank.cpp
|
||||||
src/change_flag.cpp
|
src/change_flag.cpp
|
||||||
|
src/json_filter.cpp
|
||||||
src/log.cpp
|
src/log.cpp
|
||||||
src/transaction.cpp
|
src/transaction.cpp
|
||||||
src/user_filter.cpp
|
src/user_filter.cpp
|
||||||
|
|
|
||||||
|
|
@ -2,16 +2,14 @@
|
||||||
#include <iostream> //temporary
|
#include <iostream> //temporary
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <shared_mutex>
|
#include <shared_mutex>
|
||||||
#include <drogon/HttpTypes.h>
|
|
||||||
#include <parallel-hashmap/parallel_hashmap/phmap.h>
|
#include <parallel-hashmap/parallel_hashmap/phmap.h>
|
||||||
|
#include "bank_resp.h"
|
||||||
#include "user.h"
|
#include "user.h"
|
||||||
|
|
||||||
#if (CONSERVATIVE_DISK_SAVE && MAX_LOG_SIZE < 0) && !MULTI_THREADED
|
#if (CONSERVATIVE_DISK_SAVE && MAX_LOG_SIZE < 0) && !MULTI_THREADED
|
||||||
#include "change_flag.h"
|
#include "change_flag.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
using BankResponse = std::pair<drogon::HttpStatusCode, Json::Value>;
|
|
||||||
|
|
||||||
class Bank
|
class Bank
|
||||||
{
|
{
|
||||||
#if MULTI_THREADED
|
#if MULTI_THREADED
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,7 @@ public:
|
||||||
void AdminAddUser(req_args) const;
|
void AdminAddUser(req_args) const;
|
||||||
void DelUser(req_args) const;
|
void DelUser(req_args) const;
|
||||||
void AdminDelUser(req_args) const;
|
void AdminDelUser(req_args) const;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
METHOD_LIST_BEGIN
|
METHOD_LIST_BEGIN
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <drogon/HttpFilter.h>
|
#include <drogon/HttpFilter.h>
|
||||||
|
#include "bank_resp.h"
|
||||||
|
|
||||||
using namespace drogon;
|
using namespace drogon;
|
||||||
|
|
||||||
|
|
|
||||||
38
src/bank.cpp
38
src/bank.cpp
|
|
@ -56,15 +56,15 @@ BankResponse Bank::GetBal(const std::string &name) const noexcept
|
||||||
{
|
{
|
||||||
uint32_t res = 0;
|
uint32_t res = 0;
|
||||||
users.if_contains(name, [&res](const User &u) { res = u.balance + 1; });
|
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 Bank::GetLogs(const std::string &name) noexcept
|
||||||
{
|
{
|
||||||
BankResponse res;
|
BankResponse res;
|
||||||
#if MAX_LOG_SIZE > 0
|
#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
|
#endif
|
||||||
return res;
|
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
|
//cant send money to self, from self or amount is 0
|
||||||
if (a_name == b_name)
|
if (a_name == b_name)
|
||||||
{
|
{
|
||||||
return {k400BadRequest, "Sender and Reciever names cannot match"};
|
return {k400BadRequest, "\"Sender and Reciever names cannot match\""};
|
||||||
}
|
}
|
||||||
|
|
||||||
//cant send 0
|
//cant send 0
|
||||||
if (!amount)
|
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
|
//as first modify_if checks a_name and grabs unique lock
|
||||||
if (!Contains(b_name))
|
if (!Contains(b_name))
|
||||||
{
|
{
|
||||||
return {k404NotFound, "Reciever does not exist"};
|
return {k404NotFound, "\"Reciever does not exist\""};
|
||||||
}
|
}
|
||||||
|
|
||||||
BankResponse state;
|
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 can afford it
|
||||||
if (a.balance < amount)
|
if (a.balance < amount)
|
||||||
{
|
{
|
||||||
state = {k400BadRequest, "Sender has insufficient funds"};
|
state = BankResponse(k400BadRequest, "\"Sender has insufficient funds\"");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -108,11 +108,11 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
|
||||||
#if MAX_LOG_SIZE > 0
|
#if MAX_LOG_SIZE > 0
|
||||||
a.log.AddTrans(Transaction(temp)); //about 40% of this function's cost
|
a.log.AddTrans(Transaction(temp)); //about 40% of this function's cost
|
||||||
#endif
|
#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)
|
if (state.first == k200OK)
|
||||||
{
|
{
|
||||||
|
|
@ -164,18 +164,18 @@ BankResponse Bank::SetBal(const std::string &name, uint32_t amount) noexcept
|
||||||
save_flag = true;
|
save_flag = true;
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
return {k200OK, "Balance set!"};
|
return {k200OK, "\"Balance set!\""};
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return {k404NotFound, "User not found"};
|
return {k404NotFound, "\"User not found\""};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
BankResponse Bank::ImpactBal(const std::string &name, int64_t amount) noexcept
|
BankResponse Bank::ImpactBal(const std::string &name, int64_t amount) noexcept
|
||||||
{
|
{
|
||||||
if (amount == 0)
|
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; }))
|
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;
|
save_flag = true;
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
return {k200OK, "Balance added!"};
|
return {k200OK, "\"Balance added!\""};
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return {k404NotFound, "User not found"};
|
return {k404NotFound, "\"User not found\""};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bool Bank::Contains(const std::string &name) const noexcept
|
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))
|
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<std::shared_mutex> lock{save_lock};
|
std::shared_lock<std::shared_mutex> lock{save_lock};
|
||||||
if (users.try_emplace_l(
|
if (users.try_emplace_l(
|
||||||
|
|
@ -219,11 +219,11 @@ BankResponse Bank::AddUser(std::string &&name, uint32_t init_bal, std::string &&
|
||||||
save_flag = true;
|
save_flag = true;
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
return {k200OK, "User added!"};
|
return {k200OK, "\"User added!\""};
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return {k409Conflict, "User already exists"};
|
return {k409Conflict, "\"User already exists\""};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
BankResponse Bank::DelUser(const std::string &name) noexcept
|
BankResponse Bank::DelUser(const std::string &name) noexcept
|
||||||
|
|
@ -250,11 +250,11 @@ BankResponse Bank::DelUser(const std::string &name) noexcept
|
||||||
save_flag = true;
|
save_flag = true;
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
return BankResponse(k200OK, "User deleted!");
|
return BankResponse{k200OK, "\"User deleted!\""};
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return BankResponse(k404NotFound, "User not found");
|
return BankResponse{k404NotFound, "\"User not found\""};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void Bank::Save()
|
void Bank::Save()
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ template <>
|
||||||
HttpResponsePtr drogon::toResponse(BankResponse &&data)
|
HttpResponsePtr drogon::toResponse(BankResponse &&data)
|
||||||
{
|
{
|
||||||
auto res = std::make_shared<HttpResponseImpl>(data.first, CT_APPLICATION_JSON);
|
auto res = std::make_shared<HttpResponseImpl>(data.first, CT_APPLICATION_JSON);
|
||||||
res->setJsonObject(std::move(data.second));
|
res->setBody(std::move(data.second));
|
||||||
auto &advices = HttpAppFrameworkImpl::instance().getResponseCreationAdvices();
|
auto &advices = HttpAppFrameworkImpl::instance().getResponseCreationAdvices();
|
||||||
if (!advices.empty())
|
if (!advices.empty())
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
#include "json_filter.h"
|
#include "json_filter.h"
|
||||||
|
|
||||||
using BankResponse = std::pair<drogon::HttpStatusCode, Json::Value>;
|
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
JsonFilter<true>::JsonFilter() {}
|
JsonFilter<true>::JsonFilter() {}
|
||||||
template <>
|
template <>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue