mirror of
https://github.com/Expand-sys/CCash
synced 2025-12-17 00:22:14 +11:00
🔥🐎 cleaned up
This commit is contained in:
parent
58e00e3b75
commit
e8941d60cd
5 changed files with 21 additions and 35 deletions
|
|
@ -4,7 +4,6 @@
|
||||||
#include <shared_mutex>
|
#include <shared_mutex>
|
||||||
#include <drogon/HttpTypes.h>
|
#include <drogon/HttpTypes.h>
|
||||||
#include <parallel-hashmap/parallel_hashmap/phmap.h>
|
#include <parallel-hashmap/parallel_hashmap/phmap.h>
|
||||||
#include "error_responses.hpp" //temporary
|
|
||||||
#include "user.h"
|
#include "user.h"
|
||||||
|
|
||||||
#if CONSERVATIVE_DISK_SAVE && MAX_LOG_SIZE < 0
|
#if CONSERVATIVE_DISK_SAVE && MAX_LOG_SIZE < 0
|
||||||
|
|
|
||||||
|
|
@ -28,34 +28,34 @@ public:
|
||||||
void AddUser(req_args) const;
|
void AddUser(req_args) const;
|
||||||
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 std::string &name) const;
|
void AdminDelUser(req_args) const;
|
||||||
void Contains(req_args, const std::string &name) const;
|
void Contains(req_args, const std::string &name) const;
|
||||||
void AdminVerifyPass(req_args);
|
void AdminVerifyPass(req_args);
|
||||||
|
|
||||||
METHOD_LIST_BEGIN
|
METHOD_LIST_BEGIN
|
||||||
|
|
||||||
//Usage
|
//Usage
|
||||||
METHOD_ADD(api::GetBal, "/v1/user/bal?name={name}", Get, Options); //done
|
METHOD_ADD(api::GetBal, "/v1/user/bal?name={name}", Get, Options);
|
||||||
METHOD_ADD(api::GetLog, "/v1/user/log", Get, Options, "UserFilter"); //done (could be optimized further)
|
METHOD_ADD(api::GetLog, "/v1/user/log", Get, Options, "UserFilter"); //(could be optimized further)
|
||||||
METHOD_ADD(api::SendFunds, "/v1/user/transfer", Post, Options, "UserFilter"); //done
|
METHOD_ADD(api::SendFunds, "/v1/user/transfer", Post, Options, "UserFilter");
|
||||||
METHOD_ADD(api::VerifyPassword, "/v1/user/verify_password", Get, Options, "UserFilter"); //done
|
METHOD_ADD(api::VerifyPassword, "/v1/user/verify_password", Get, Options, "UserFilter");
|
||||||
|
|
||||||
//Meta Usage
|
//Meta Usage
|
||||||
METHOD_ADD(api::ChangePassword, "/v1/user/change_password", Patch, Options, "UserFilter"); //done
|
METHOD_ADD(api::ChangePassword, "/v1/user/change_password", Patch, Options, "UserFilter");
|
||||||
METHOD_ADD(api::SetBal, "/v1/admin/bal", Patch, Options, "AdminFilter"); //done
|
METHOD_ADD(api::SetBal, "/v1/admin/bal", Patch, Options, "AdminFilter");
|
||||||
|
|
||||||
//System Usage
|
//System Usage
|
||||||
METHOD_ADD(api::Help, "/v1/help", Get, Options); //done
|
METHOD_ADD(api::Help, "/v1/help", Get, Options);
|
||||||
METHOD_ADD(api::Ping, "/v1/ping", Get, Options); //done
|
METHOD_ADD(api::Ping, "/v1/ping", Get, Options);
|
||||||
METHOD_ADD(api::Close, "/v1/admin/shutdown", Post, Options, "AdminFilter"); //done
|
METHOD_ADD(api::Close, "/v1/admin/shutdown", Post, Options, "AdminFilter");
|
||||||
METHOD_ADD(api::Contains, "/v1/user/exists?name={name}", Get, Options); //done
|
METHOD_ADD(api::Contains, "/v1/user/exists?name={name}", Get, Options);
|
||||||
METHOD_ADD(api::AdminVerifyPass, "/v1/admin/verify_password", Get, Options, "AdminFilter"); //done
|
METHOD_ADD(api::AdminVerifyPass, "/v1/admin/verify_password", Get, Options, "AdminFilter");
|
||||||
|
|
||||||
//User Managment
|
//User Managment
|
||||||
METHOD_ADD(api::AddUser, "/v1/user/register", Post, Options); //done
|
METHOD_ADD(api::AddUser, "/v1/user/register", Post, Options);
|
||||||
METHOD_ADD(api::AdminAddUser, "/v1/admin/user/register", Post, Options, "AdminFilter"); //done
|
METHOD_ADD(api::AdminAddUser, "/v1/admin/user/register", Post, Options, "AdminFilter");
|
||||||
METHOD_ADD(api::DelUser, "/v1/delete", Delete, Options, "UserFilter"); //done
|
METHOD_ADD(api::DelUser, "/v1/delete", Delete, Options, "UserFilter");
|
||||||
METHOD_ADD(api::AdminDelUser, "/v1/admin/delete", Delete, Options, "AdminFilter"); //done
|
METHOD_ADD(api::AdminDelUser, "/v1/admin/delete", Delete, Options, "AdminFilter");
|
||||||
|
|
||||||
METHOD_LIST_END
|
METHOD_LIST_END
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -1,10 +0,0 @@
|
||||||
#pragma once
|
|
||||||
enum ErrorResponse
|
|
||||||
{
|
|
||||||
UserNotFound = -1, //404
|
|
||||||
WrongPassword = -2, //401
|
|
||||||
InvalidRequest = -3, //400
|
|
||||||
NameTooLong = -4, //400
|
|
||||||
UserAlreadyExists = -5, //409
|
|
||||||
InsufficientFunds = -6, //400
|
|
||||||
};
|
|
||||||
10
src/bank.cpp
10
src/bank.cpp
|
|
@ -22,11 +22,11 @@ bool Bank::GetChangeState() const noexcept { return save_flag.GetChangeState();
|
||||||
|
|
||||||
BankResponse Bank::GetBal(const std::string &name) const noexcept
|
BankResponse Bank::GetBal(const std::string &name) const noexcept
|
||||||
{
|
{
|
||||||
int_fast64_t res = -1;
|
uint32_t res = 0;
|
||||||
users.if_contains(name, [&res](const User &u) {
|
users.if_contains(name, [&res](const User &u) {
|
||||||
res = u.balance;
|
res = u.balance + 1;
|
||||||
});
|
});
|
||||||
return res < 0 ? BankResponse(k404NotFound, "User not found") : BankResponse(k200OK, res);
|
return res > 0 ? BankResponse(k200OK, res - 1) : BankResponse(k404NotFound, "User not found");
|
||||||
}
|
}
|
||||||
BankResponse Bank::GetLogs(const std::string &name) noexcept
|
BankResponse Bank::GetLogs(const std::string &name) noexcept
|
||||||
{
|
{
|
||||||
|
|
@ -154,7 +154,7 @@ BankResponse Bank::AddUser(const std::string &name, std::string &&init_pass) noe
|
||||||
|
|
||||||
std::shared_lock<std::shared_mutex> lock{size_l};
|
std::shared_lock<std::shared_mutex> lock{size_l};
|
||||||
return (users.try_emplace_l(
|
return (users.try_emplace_l(
|
||||||
name, [](User &) {}, std::move(init_pass)))
|
std::move(name), [](User &) {}, std::move(init_pass)))
|
||||||
? BankResponse(k200OK, "User added!")
|
? BankResponse(k200OK, "User added!")
|
||||||
: BankResponse(k409Conflict, "User already exists");
|
: BankResponse(k409Conflict, "User already exists");
|
||||||
}
|
}
|
||||||
|
|
@ -167,7 +167,7 @@ BankResponse Bank::AdminAddUser(std::string &&name, uint32_t init_bal, std::stri
|
||||||
|
|
||||||
std::shared_lock<std::shared_mutex> lock{size_l};
|
std::shared_lock<std::shared_mutex> lock{size_l};
|
||||||
return (users.try_emplace_l(
|
return (users.try_emplace_l(
|
||||||
name, [](User &) {}, init_bal, std::move(init_pass)))
|
std::move(name), [](User &) {}, init_bal, std::move(init_pass)))
|
||||||
? BankResponse(k200OK, "User added!")
|
? BankResponse(k200OK, "User added!")
|
||||||
: BankResponse(k409Conflict, "User already exists");
|
: BankResponse(k409Conflict, "User already exists");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,5 @@
|
||||||
#include "bank_api.h"
|
#include "bank_api.h"
|
||||||
|
|
||||||
#define JSON(V) callback(HttpResponse::newHttpJsonResponse(JsonCast(V))); //temporary
|
|
||||||
#define PASS_HEADER req->getHeader("Password") //temporary
|
|
||||||
|
|
||||||
#define GEN_BODY \
|
#define GEN_BODY \
|
||||||
const auto temp_req = req->getJsonObject(); \
|
const auto temp_req = req->getJsonObject(); \
|
||||||
const auto body = temp_req ? *temp_req : Json::Value();
|
const auto body = temp_req ? *temp_req : Json::Value();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue