1
0
forked from blue/pica

currencies request

This commit is contained in:
Blue 2024-01-22 15:21:55 -03:00
parent a2c2c2a883
commit db37abacd2
Signed by untrusted user: blue
GPG Key ID: 9B203B252A63EE38
24 changed files with 243 additions and 57 deletions

View File

@ -11,6 +11,7 @@
#include "schema/session.h"
#include "schema/asset.h"
#include "schema/currency.h"
namespace DB {
class Interface {
@ -41,13 +42,14 @@ public:
virtual uint8_t getVersion() = 0;
virtual void setVersion(uint8_t version) = 0;
virtual unsigned int registerAccount(const std::string& login, const std::string& hash) = 0;
virtual uint32_t registerAccount(const std::string& login, const std::string& hash) = 0;
virtual std::string getAccountHash(const std::string& login) = 0;
virtual Session createSession(const std::string& login, const std::string& access, const std::string& renew) = 0;
virtual Session findSession(const std::string& accessToken) = 0;
virtual std::vector<Asset> listAssets(unsigned int owner) = 0;
virtual std::vector<Asset> listAssets(uint32_t owner) = 0;
virtual Asset addAsset(const Asset& asset) = 0;
virtual bool deleteAsset(unsigned int assetId, unsigned int actorId) = 0;
virtual bool deleteAsset(uint32_t assetId, uint32_t actorId) = 0;
virtual std::vector<Currency> listUsedCurrencies(uint32_t owner) = 0;
protected:
Interface(Type type);

View File

@ -22,10 +22,13 @@ constexpr const char* createSessionQuery = "INSERT INTO sessions (`owner`, `acce
" SELECT accounts.id, ?, ?, true, ? FROM accounts WHERE accounts.login = ?"
" RETURNING id, owner";
constexpr const char* selectSession = "SELECT id, owner, access, renew FROM sessions where access = ?";
constexpr const char* selectAssets = "SELECT id, owner, currency, title, icon, archived FROM assets where owner = ?";
constexpr const char* insertAsset = "INSERT INTO assets (`owner`, `currency`, `title`, `icon`, `archived`, `type`)"
" VALUES (?, ?, ?, ?, ?, 1)";
constexpr const char* selectAssets = "SELECT id, owner, currency, title, icon, color, archived FROM assets where owner = ?";
constexpr const char* insertAsset = "INSERT INTO assets (`owner`, `currency`, `title`, `icon`, `color`, `archived`, `type`)"
" VALUES (?, ?, ?, ?, ?, ?, 1)";
constexpr const char* removeAsset = "DELETE FROM assets where `id` = ? AND `owner` = ?";
constexpr const char* selectUsedCurrencies = "SELECT c.id, c.code, c.title, c.manual, c.icon FROM currencies c"
"JOIN assets a ON c.id = a.currency"
"WHERE a.owner = ?";
static const std::filesystem::path buildSQLPath = "database";
@ -224,7 +227,7 @@ std::optional<std::string> DB::MySQL::getComment (std::string& string) {
return std::nullopt;
}
unsigned int DB::MySQL::registerAccount (const std::string& login, const std::string& hash) {
uint32_t DB::MySQL::registerAccount (const std::string& login, const std::string& hash) {
//TODO validate filed lengths!
MYSQL* con = &connection;
MySQL::Transaction txn(con);
@ -241,7 +244,7 @@ unsigned int DB::MySQL::registerAccount (const std::string& login, const std::st
throw DuplicateLogin(dup.what());
}
unsigned int id = lastInsertedId();
uint32_t id = lastInsertedId();
static std::string defaultRole("default");
Statement addRole(con, assignRoleQuery);
@ -291,13 +294,13 @@ DB::Session DB::MySQL::createSession (const std::string& login, const std::strin
if (result.empty())
throw std::runtime_error("Error returning ids after insertion in sessions table");
res.id = std::any_cast<unsigned int>(result[0][0]);
res.owner = std::any_cast<unsigned int>(result[0][1]);
res.id = std::any_cast<uint32_t>(result[0][0]);
res.owner = std::any_cast<uint32_t>(result[0][1]);
return res;
}
unsigned int DB::MySQL::lastInsertedId () {
uint32_t DB::MySQL::lastInsertedId () {
MYSQL* con = &connection;
int result = mysql_query(con, lastIdQuery);
@ -329,7 +332,7 @@ DB::Session DB::MySQL::findSession (const std::string& accessToken) {
return DB::Session(result[0]);
}
std::vector<DB::Asset> DB::MySQL::listAssets (unsigned int owner) {
std::vector<DB::Asset> DB::MySQL::listAssets (uint32_t owner) {
MYSQL* con = &connection;
Statement st(con, selectAssets);
@ -354,6 +357,7 @@ DB::Asset DB::MySQL::addAsset(const Asset& asset) {
add.bind(&result.currency, MYSQL_TYPE_LONG, true);
add.bind(result.title.data(), MYSQL_TYPE_STRING);
add.bind(result.icon.data(), MYSQL_TYPE_STRING);
add.bind(&result.color, MYSQL_TYPE_LONG, true);
add.bind(&result.archived, MYSQL_TYPE_TINY);
add.execute();
@ -362,10 +366,8 @@ DB::Asset DB::MySQL::addAsset(const Asset& asset) {
return result;
}
bool DB::MySQL::deleteAsset(unsigned int assetId, unsigned int actorId) {
MYSQL* con = &connection;
Statement del(con, removeAsset);
bool DB::MySQL::deleteAsset(uint32_t assetId, uint32_t actorId) {
Statement del(&connection, removeAsset);
del.bind(&assetId, MYSQL_TYPE_LONG, true);
del.bind(&actorId, MYSQL_TYPE_LONG, true);
del.execute();
@ -375,3 +377,18 @@ bool DB::MySQL::deleteAsset(unsigned int assetId, unsigned int actorId) {
return true;
}
std::vector<DB::Currency> DB::MySQL::listUsedCurrencies(uint32_t owner) {
Statement list(&connection, removeAsset);
list.bind(&owner, MYSQL_TYPE_LONG, true);
list.execute();
std::vector<std::vector<std::any>> res = list.fetchResult();
std::size_t size = res.size();
std::vector<DB::Currency> result(size);
for (std::size_t i = 0; i < size; ++i)
result[i].parse(res[i]);
return result;
}

View File

@ -17,7 +17,6 @@ class MySQL : public Interface {
class Statement;
class Transaction;
public:
MySQL ();
~MySQL () override;
@ -31,18 +30,19 @@ public:
uint8_t getVersion () override;
void setVersion (uint8_t version) override;
unsigned int registerAccount (const std::string& login, const std::string& hash) override;
uint32_t registerAccount (const std::string& login, const std::string& hash) override;
std::string getAccountHash (const std::string& login) override;
Session createSession (const std::string& login, const std::string& access, const std::string& renew) override;
Session findSession (const std::string& accessToken) override;
std::vector<Asset> listAssets (unsigned int owner) override;
std::vector<Asset> listAssets (uint32_t owner) override;
Asset addAsset (const Asset& asset) override;
bool deleteAsset(unsigned int assetId, unsigned int actorId) override;
bool deleteAsset(uint32_t assetId, uint32_t actorId) override;
std::vector<Currency> listUsedCurrencies(uint32_t owner) override;
private:
void executeFile (const std::filesystem::path& relativePath);
static std::optional<std::string> getComment (std::string& string);
unsigned int lastInsertedId ();
uint32_t lastInsertedId ();
protected:
MYSQL connection;

View File

@ -4,11 +4,13 @@
set(HEADERS
session.h
asset.h
currency.h
)
set(SOURCES
session.cpp
asset.cpp
currency.cpp
)
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})

View File

@ -4,30 +4,33 @@
#include "asset.h"
DB::Asset::Asset ():
id(),
owner(),
currency(),
id(0),
owner(0),
currency(0),
title(),
icon(),
archived()
color(0),
archived(false)
{}
DB::Asset::Asset (const std::vector<std::any>& vec):
id(std::any_cast<unsigned int>(vec[0])),
owner(std::any_cast<unsigned int>(vec[1])),
currency(std::any_cast<unsigned int>(vec[2])),
id(std::any_cast<uint32_t>(vec[0])),
owner(std::any_cast<uint32_t>(vec[1])),
currency(std::any_cast<uint32_t>(vec[2])),
title(std::any_cast<const std::string&>(vec[3])),
icon(std::any_cast<const std::string&>(vec[4])),
archived(std::any_cast<uint8_t>(vec[5]))
color(std::any_cast<uint32_t>(vec[5])),
archived(std::any_cast<uint8_t>(vec[6]))
{}
void DB::Asset::parse (const std::vector<std::any>& vec) {
id = std::any_cast<unsigned int>(vec[0]);
owner = std::any_cast<unsigned int>(vec[1]);
currency = std::any_cast<unsigned int>(vec[2]);
id = std::any_cast<uint32_t>(vec[0]);
owner = std::any_cast<uint32_t>(vec[1]);
currency = std::any_cast<uint32_t>(vec[2]);
title = std::any_cast<const std::string&>(vec[3]);
icon = std::any_cast<const std::string&>(vec[4]);
archived = std::any_cast<uint8_t>(vec[5]);
color = std::any_cast<uint32_t>(vec[5]);
archived = std::any_cast<uint8_t>(vec[6]);
}
nlohmann::json DB::Asset::toJSON () const {
@ -38,6 +41,7 @@ nlohmann::json DB::Asset::toJSON () const {
//result["currency"] = currency;
result["title"] = title;
result["icon"] = icon;
result["color"] = color;
result["archived"] = archived;
return result;

View File

@ -20,12 +20,12 @@ public:
nlohmann::json toJSON () const;
public:
unsigned int id;
unsigned int owner;
unsigned int currency;
uint32_t id;
uint32_t owner;
uint32_t currency;
std::string title;
std::string icon;
// `color` INTEGER UNSIGNED DEFAULT 0,
uint32_t color;
// `balance` DECIMAL (20, 5) DEFAULT 0,
// `type` INTEGER UNSIGNED NOT NULL,
bool archived;

View File

@ -0,0 +1,40 @@
//SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
//SPDX-License-Identifier: GPL-3.0-or-later
#include "currency.h"
DB::Currency::Currency ():
id(0),
code(),
title(),
manual(false),
icon()
{}
DB::Currency::Currency (const std::vector<std::any>& vec):
id(std::any_cast<uint32_t>(vec[0])),
code(std::any_cast<const std::string&>(vec[1])),
title(std::any_cast<const std::string&>(vec[2])),
manual(std::any_cast<uint8_t>(vec[3])),
icon(std::any_cast<const std::string&>(vec[4]))
{}
void DB::Currency::parse (const std::vector<std::any>& vec) {
id = std::any_cast<uint32_t>(vec[0]);
code = std::any_cast<const std::string&>(vec[1]);
title = std::any_cast<const std::string&>(vec[2]);
manual = std::any_cast<uint8_t>(vec[3]);
icon = std::any_cast<const std::string&>(vec[4]);
}
nlohmann::json DB::Currency::toJSON () const {
nlohmann::json result = nlohmann::json::object();
result["id"] = id;
result["code"] = code;
result["title"] = title;
result["manual"] = manual;
result["icon"] = icon;
return result;
}

View File

@ -0,0 +1,35 @@
//SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
//SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <string>
#include <vector>
#include <any>
#include <cstdint>
#include <nlohmann/json.hpp>
namespace DB {
class Currency {
public:
Currency ();
Currency (const std::vector<std::any>& vec);
void parse (const std::vector<std::any>& vec);
nlohmann::json toJSON () const;
public:
uint32_t id;
std::string code;
std::string title;
bool manual;
// `added` TIMESTAMP DEFAULT UTC_TIMESTAMP(),
// `type` INTEGER UNSIGNED NOT NULL,
// `value` DECIMAL (20, 5) NOT NULL,
// `source` TEXT,
// `description` TEXT,
std::string icon;
};
}

View File

@ -10,7 +10,8 @@ set(HEADERS
poll.h
listassets.h
addasset.h
deleteasset.cpp
deleteasset.h
mycurrencies.h
)
set(SOURCES
@ -23,6 +24,7 @@ set(SOURCES
listassets.cpp
addasset.cpp
deleteasset.cpp
mycurrencies.cpp
)
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})

View File

@ -9,7 +9,7 @@
#include "server/session.h"
#include "database/exceptions.h"
Handler::AddAsset::AddAsset (std::weak_ptr<Server> server):
Handler::AddAsset::AddAsset (const std::shared_ptr<Server>& server):
Handler("addAsset", Request::Method::post),
server(server)
{}
@ -32,7 +32,7 @@ void Handler::AddAsset::handle (Request& request) {
return error(request, Response::Status::badRequest);
DB::Asset asset;
asset.currency = std::stoi(itr->second);
asset.currency = std::stoul(itr->second);
//TODO validate the currency
itr = form.find("title");
@ -47,6 +47,14 @@ void Handler::AddAsset::handle (Request& request) {
asset.icon = itr->second;
try {
itr = form.find("color");
if (itr != form.end())
asset.color = std::stoul(itr->second);
} catch (const std::exception& e) {
std::cerr << "Insignificant error parsing color during asset addition: " << e.what() << std::endl;
}
try {
Session& session = srv->getSession(access);

View File

@ -11,7 +11,7 @@ class Server;
namespace Handler {
class AddAsset : public Handler {
public:
AddAsset (std::weak_ptr<Server> server);
AddAsset (const std::shared_ptr<Server>& server);
virtual void handle (Request& request) override;
private:

View File

@ -7,7 +7,7 @@
#include "server/session.h"
#include "database/exceptions.h"
Handler::DeleteAsset::DeleteAsset (std::weak_ptr<Server> server):
Handler::DeleteAsset::DeleteAsset (const std::shared_ptr<Server>& server):
Handler("deleteAsset", Request::Method::post),
server(server)
{}

View File

@ -11,7 +11,7 @@ class Server;
namespace Handler {
class DeleteAsset : public Handler {
public:
DeleteAsset (std::weak_ptr<Server> server);
DeleteAsset (const std::shared_ptr<Server>& server);
virtual void handle (Request& request) override;

View File

@ -7,7 +7,7 @@
#include "server/session.h"
#include "database/exceptions.h"
Handler::ListAssets::ListAssets (std::weak_ptr<Server> server):
Handler::ListAssets::ListAssets (const std::shared_ptr<Server>& server):
Handler("listAssets", Request::Method::get),
server(server)
{}

View File

@ -11,7 +11,7 @@ class Server;
namespace Handler {
class ListAssets : public Handler::Handler {
public:
ListAssets (std::weak_ptr<Server> server);
ListAssets (const std::shared_ptr<Server>& server);
void handle (Request& request) override;
private:

View File

@ -6,7 +6,7 @@
#include "server/server.h"
#include "database/exceptions.h"
Handler::Login::Login(std::weak_ptr<Server> server):
Handler::Login::Login(const std::shared_ptr<Server>& server):
Handler("login", Request::Method::post),
server(server)
{}

View File

@ -12,7 +12,7 @@ namespace Handler {
class Login : public Handler {
public:
Login(std::weak_ptr<Server> server);
Login(const std::shared_ptr<Server>& server);
void handle(Request& request) override;
enum class Result {

51
handler/mycurrencies.cpp Normal file
View File

@ -0,0 +1,51 @@
//SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
//SPDX-License-Identifier: GPL-3.0-or-later
#include "mycurrencies.h"
#include "server/server.h"
#include "server/session.h"
#include "database/exceptions.h"
Handler::MyCurrencies::MyCurrencies (const std::shared_ptr<Server>& server):
Handler("myCurrencies", Request::Method::get),
server(server)
{}
void Handler::MyCurrencies::handle (Request& request) {
std::string access = request.getAuthorizationToken();
if (access.empty())
return error(request, Response::Status::unauthorized);
if (access.size() != 32)
return error(request, Response::Status::badRequest);
std::shared_ptr<Server> srv = server.lock();
if (!srv)
return error(request, Response::Status::internalError);
try {
Session& session = srv->getSession(access);
std::vector<DB::Currency> cur = srv->getDatabase()->listUsedCurrencies(session.owner);
nlohmann::json arr = nlohmann::json::array();
for (const DB::Currency& c : cur)
arr.push_back(c.toJSON());
nlohmann::json body = nlohmann::json::object();
body["currencies"] = arr;
Response& res = request.createResponse(Response::Status::ok);
res.setBody(body);
res.send();
} catch (const DB::NoSession& e) {
return error(request, Response::Status::unauthorized);
} catch (const std::exception& e) {
std::cerr << "Exception on poll:\n\t" << e.what() << std::endl;
return error(request, Response::Status::internalError);
} catch (...) {
std::cerr << "Unknown exception on poll" << std::endl;
return error(request, Response::Status::internalError);
}
}

21
handler/mycurrencies.h Normal file
View File

@ -0,0 +1,21 @@
//SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
//SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <memory>
#include "handler.h"
class Server;
namespace Handler {
class MyCurrencies : public Handler {
public:
MyCurrencies(const std::shared_ptr<Server>& server);
void handle (Request& request) override;
private:
std::weak_ptr<Server> server;
};
}

View File

@ -8,7 +8,7 @@
#include "request/redirect.h"
#include "database/exceptions.h"
Handler::Poll::Poll (std::weak_ptr<Server> server):
Handler::Poll::Poll (const std::shared_ptr<Server>& server):
Handler("poll", Request::Method::get),
server(server)
{}

View File

@ -14,7 +14,7 @@ namespace Handler {
class Poll : public Handler {
public:
Poll (std::weak_ptr<Server> server);
Poll (const std::shared_ptr<Server>& server);
void handle (Request& request) override;
enum class Result {

View File

@ -6,7 +6,7 @@
#include "server/server.h"
#include "database/exceptions.h"
Handler::Register::Register(std::weak_ptr<Server> server):
Handler::Register::Register(const std::shared_ptr<Server>& server):
Handler("register", Request::Method::post),
server(server)
{}

View File

@ -12,7 +12,7 @@ namespace Handler {
class Register : public Handler {
public:
Register(std::weak_ptr<Server> server);
Register(const std::shared_ptr<Server>& server);
void handle(Request& request) override;
enum class Result {

View File

@ -15,6 +15,7 @@
#include "handler/listassets.h"
#include "handler/addasset.h"
#include "handler/deleteasset.h"
#include "handler/mycurrencies.h"
#include "taskmanager/route.h"
@ -65,14 +66,17 @@ Server::Server ():
Server::~Server () {}
void Server::run (int socketDescriptor) {
std::shared_ptr<Server> srv = shared_from_this();
router->addRoute(std::make_unique<Handler::Info>());
router->addRoute(std::make_unique<Handler::Env>());
router->addRoute(std::make_unique<Handler::Register>(shared_from_this()));
router->addRoute(std::make_unique<Handler::Login>(shared_from_this()));
router->addRoute(std::make_unique<Handler::Poll>(shared_from_this()));
router->addRoute(std::make_unique<Handler::ListAssets>(shared_from_this()));
router->addRoute(std::make_unique<Handler::AddAsset>(shared_from_this()));
router->addRoute(std::make_unique<Handler::DeleteAsset>(shared_from_this()));
router->addRoute(std::make_unique<Handler::Register>(srv));
router->addRoute(std::make_unique<Handler::Login>(srv));
router->addRoute(std::make_unique<Handler::Poll>(srv));
router->addRoute(std::make_unique<Handler::ListAssets>(srv));
router->addRoute(std::make_unique<Handler::AddAsset>(srv));
router->addRoute(std::make_unique<Handler::DeleteAsset>(srv));
router->addRoute(std::make_unique<Handler::MyCurrencies>(srv));
taskManager->start();
scheduler->start();