currencies request
This commit is contained in:
parent
a2c2c2a883
commit
db37abacd2
@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
#include "schema/session.h"
|
#include "schema/session.h"
|
||||||
#include "schema/asset.h"
|
#include "schema/asset.h"
|
||||||
|
#include "schema/currency.h"
|
||||||
|
|
||||||
namespace DB {
|
namespace DB {
|
||||||
class Interface {
|
class Interface {
|
||||||
@ -41,13 +42,14 @@ public:
|
|||||||
virtual uint8_t getVersion() = 0;
|
virtual uint8_t getVersion() = 0;
|
||||||
virtual void setVersion(uint8_t version) = 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 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 createSession(const std::string& login, const std::string& access, const std::string& renew) = 0;
|
||||||
virtual Session findSession(const std::string& accessToken) = 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 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:
|
protected:
|
||||||
Interface(Type type);
|
Interface(Type type);
|
||||||
|
@ -22,10 +22,13 @@ constexpr const char* createSessionQuery = "INSERT INTO sessions (`owner`, `acce
|
|||||||
" SELECT accounts.id, ?, ?, true, ? FROM accounts WHERE accounts.login = ?"
|
" SELECT accounts.id, ?, ?, true, ? FROM accounts WHERE accounts.login = ?"
|
||||||
" RETURNING id, owner";
|
" RETURNING id, owner";
|
||||||
constexpr const char* selectSession = "SELECT id, owner, access, renew FROM sessions where access = ?";
|
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* selectAssets = "SELECT id, owner, currency, title, icon, color, archived FROM assets where owner = ?";
|
||||||
constexpr const char* insertAsset = "INSERT INTO assets (`owner`, `currency`, `title`, `icon`, `archived`, `type`)"
|
constexpr const char* insertAsset = "INSERT INTO assets (`owner`, `currency`, `title`, `icon`, `color`, `archived`, `type`)"
|
||||||
" VALUES (?, ?, ?, ?, ?, 1)";
|
" VALUES (?, ?, ?, ?, ?, ?, 1)";
|
||||||
constexpr const char* removeAsset = "DELETE FROM assets where `id` = ? AND `owner` = ?";
|
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";
|
static const std::filesystem::path buildSQLPath = "database";
|
||||||
|
|
||||||
@ -224,7 +227,7 @@ std::optional<std::string> DB::MySQL::getComment (std::string& string) {
|
|||||||
return std::nullopt;
|
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!
|
//TODO validate filed lengths!
|
||||||
MYSQL* con = &connection;
|
MYSQL* con = &connection;
|
||||||
MySQL::Transaction txn(con);
|
MySQL::Transaction txn(con);
|
||||||
@ -241,7 +244,7 @@ unsigned int DB::MySQL::registerAccount (const std::string& login, const std::st
|
|||||||
throw DuplicateLogin(dup.what());
|
throw DuplicateLogin(dup.what());
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int id = lastInsertedId();
|
uint32_t id = lastInsertedId();
|
||||||
static std::string defaultRole("default");
|
static std::string defaultRole("default");
|
||||||
|
|
||||||
Statement addRole(con, assignRoleQuery);
|
Statement addRole(con, assignRoleQuery);
|
||||||
@ -291,13 +294,13 @@ DB::Session DB::MySQL::createSession (const std::string& login, const std::strin
|
|||||||
if (result.empty())
|
if (result.empty())
|
||||||
throw std::runtime_error("Error returning ids after insertion in sessions table");
|
throw std::runtime_error("Error returning ids after insertion in sessions table");
|
||||||
|
|
||||||
res.id = std::any_cast<unsigned int>(result[0][0]);
|
res.id = std::any_cast<uint32_t>(result[0][0]);
|
||||||
res.owner = std::any_cast<unsigned int>(result[0][1]);
|
res.owner = std::any_cast<uint32_t>(result[0][1]);
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int DB::MySQL::lastInsertedId () {
|
uint32_t DB::MySQL::lastInsertedId () {
|
||||||
MYSQL* con = &connection;
|
MYSQL* con = &connection;
|
||||||
int result = mysql_query(con, lastIdQuery);
|
int result = mysql_query(con, lastIdQuery);
|
||||||
|
|
||||||
@ -329,7 +332,7 @@ DB::Session DB::MySQL::findSession (const std::string& accessToken) {
|
|||||||
return DB::Session(result[0]);
|
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;
|
MYSQL* con = &connection;
|
||||||
|
|
||||||
Statement st(con, selectAssets);
|
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.currency, MYSQL_TYPE_LONG, true);
|
||||||
add.bind(result.title.data(), MYSQL_TYPE_STRING);
|
add.bind(result.title.data(), MYSQL_TYPE_STRING);
|
||||||
add.bind(result.icon.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.bind(&result.archived, MYSQL_TYPE_TINY);
|
||||||
add.execute();
|
add.execute();
|
||||||
|
|
||||||
@ -362,10 +366,8 @@ DB::Asset DB::MySQL::addAsset(const Asset& asset) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DB::MySQL::deleteAsset(unsigned int assetId, unsigned int actorId) {
|
bool DB::MySQL::deleteAsset(uint32_t assetId, uint32_t actorId) {
|
||||||
MYSQL* con = &connection;
|
Statement del(&connection, removeAsset);
|
||||||
|
|
||||||
Statement del(con, removeAsset);
|
|
||||||
del.bind(&assetId, MYSQL_TYPE_LONG, true);
|
del.bind(&assetId, MYSQL_TYPE_LONG, true);
|
||||||
del.bind(&actorId, MYSQL_TYPE_LONG, true);
|
del.bind(&actorId, MYSQL_TYPE_LONG, true);
|
||||||
del.execute();
|
del.execute();
|
||||||
@ -375,3 +377,18 @@ bool DB::MySQL::deleteAsset(unsigned int assetId, unsigned int actorId) {
|
|||||||
|
|
||||||
return true;
|
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;
|
||||||
|
}
|
||||||
|
@ -17,7 +17,6 @@ class MySQL : public Interface {
|
|||||||
class Statement;
|
class Statement;
|
||||||
class Transaction;
|
class Transaction;
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
MySQL ();
|
MySQL ();
|
||||||
~MySQL () override;
|
~MySQL () override;
|
||||||
@ -31,18 +30,19 @@ public:
|
|||||||
uint8_t getVersion () override;
|
uint8_t getVersion () override;
|
||||||
void setVersion (uint8_t version) 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;
|
std::string getAccountHash (const std::string& login) override;
|
||||||
Session createSession (const std::string& login, const std::string& access, const std::string& renew) override;
|
Session createSession (const std::string& login, const std::string& access, const std::string& renew) override;
|
||||||
Session findSession (const std::string& accessToken) 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;
|
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:
|
private:
|
||||||
void executeFile (const std::filesystem::path& relativePath);
|
void executeFile (const std::filesystem::path& relativePath);
|
||||||
static std::optional<std::string> getComment (std::string& string);
|
static std::optional<std::string> getComment (std::string& string);
|
||||||
unsigned int lastInsertedId ();
|
uint32_t lastInsertedId ();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
MYSQL connection;
|
MYSQL connection;
|
||||||
|
@ -4,11 +4,13 @@
|
|||||||
set(HEADERS
|
set(HEADERS
|
||||||
session.h
|
session.h
|
||||||
asset.h
|
asset.h
|
||||||
|
currency.h
|
||||||
)
|
)
|
||||||
|
|
||||||
set(SOURCES
|
set(SOURCES
|
||||||
session.cpp
|
session.cpp
|
||||||
asset.cpp
|
asset.cpp
|
||||||
|
currency.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})
|
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})
|
||||||
|
@ -4,30 +4,33 @@
|
|||||||
#include "asset.h"
|
#include "asset.h"
|
||||||
|
|
||||||
DB::Asset::Asset ():
|
DB::Asset::Asset ():
|
||||||
id(),
|
id(0),
|
||||||
owner(),
|
owner(0),
|
||||||
currency(),
|
currency(0),
|
||||||
title(),
|
title(),
|
||||||
icon(),
|
icon(),
|
||||||
archived()
|
color(0),
|
||||||
|
archived(false)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
DB::Asset::Asset (const std::vector<std::any>& vec):
|
DB::Asset::Asset (const std::vector<std::any>& vec):
|
||||||
id(std::any_cast<unsigned int>(vec[0])),
|
id(std::any_cast<uint32_t>(vec[0])),
|
||||||
owner(std::any_cast<unsigned int>(vec[1])),
|
owner(std::any_cast<uint32_t>(vec[1])),
|
||||||
currency(std::any_cast<unsigned int>(vec[2])),
|
currency(std::any_cast<uint32_t>(vec[2])),
|
||||||
title(std::any_cast<const std::string&>(vec[3])),
|
title(std::any_cast<const std::string&>(vec[3])),
|
||||||
icon(std::any_cast<const std::string&>(vec[4])),
|
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) {
|
void DB::Asset::parse (const std::vector<std::any>& vec) {
|
||||||
id = std::any_cast<unsigned int>(vec[0]);
|
id = std::any_cast<uint32_t>(vec[0]);
|
||||||
owner = std::any_cast<unsigned int>(vec[1]);
|
owner = std::any_cast<uint32_t>(vec[1]);
|
||||||
currency = std::any_cast<unsigned int>(vec[2]);
|
currency = std::any_cast<uint32_t>(vec[2]);
|
||||||
title = std::any_cast<const std::string&>(vec[3]);
|
title = std::any_cast<const std::string&>(vec[3]);
|
||||||
icon = std::any_cast<const std::string&>(vec[4]);
|
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 {
|
nlohmann::json DB::Asset::toJSON () const {
|
||||||
@ -38,6 +41,7 @@ nlohmann::json DB::Asset::toJSON () const {
|
|||||||
//result["currency"] = currency;
|
//result["currency"] = currency;
|
||||||
result["title"] = title;
|
result["title"] = title;
|
||||||
result["icon"] = icon;
|
result["icon"] = icon;
|
||||||
|
result["color"] = color;
|
||||||
result["archived"] = archived;
|
result["archived"] = archived;
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
@ -20,12 +20,12 @@ public:
|
|||||||
nlohmann::json toJSON () const;
|
nlohmann::json toJSON () const;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
unsigned int id;
|
uint32_t id;
|
||||||
unsigned int owner;
|
uint32_t owner;
|
||||||
unsigned int currency;
|
uint32_t currency;
|
||||||
std::string title;
|
std::string title;
|
||||||
std::string icon;
|
std::string icon;
|
||||||
// `color` INTEGER UNSIGNED DEFAULT 0,
|
uint32_t color;
|
||||||
// `balance` DECIMAL (20, 5) DEFAULT 0,
|
// `balance` DECIMAL (20, 5) DEFAULT 0,
|
||||||
// `type` INTEGER UNSIGNED NOT NULL,
|
// `type` INTEGER UNSIGNED NOT NULL,
|
||||||
bool archived;
|
bool archived;
|
||||||
|
40
database/schema/currency.cpp
Normal file
40
database/schema/currency.cpp
Normal 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;
|
||||||
|
}
|
35
database/schema/currency.h
Normal file
35
database/schema/currency.h
Normal 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;
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
@ -10,7 +10,8 @@ set(HEADERS
|
|||||||
poll.h
|
poll.h
|
||||||
listassets.h
|
listassets.h
|
||||||
addasset.h
|
addasset.h
|
||||||
deleteasset.cpp
|
deleteasset.h
|
||||||
|
mycurrencies.h
|
||||||
)
|
)
|
||||||
|
|
||||||
set(SOURCES
|
set(SOURCES
|
||||||
@ -23,6 +24,7 @@ set(SOURCES
|
|||||||
listassets.cpp
|
listassets.cpp
|
||||||
addasset.cpp
|
addasset.cpp
|
||||||
deleteasset.cpp
|
deleteasset.cpp
|
||||||
|
mycurrencies.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})
|
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
#include "server/session.h"
|
#include "server/session.h"
|
||||||
#include "database/exceptions.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),
|
Handler("addAsset", Request::Method::post),
|
||||||
server(server)
|
server(server)
|
||||||
{}
|
{}
|
||||||
@ -32,7 +32,7 @@ void Handler::AddAsset::handle (Request& request) {
|
|||||||
return error(request, Response::Status::badRequest);
|
return error(request, Response::Status::badRequest);
|
||||||
|
|
||||||
DB::Asset asset;
|
DB::Asset asset;
|
||||||
asset.currency = std::stoi(itr->second);
|
asset.currency = std::stoul(itr->second);
|
||||||
//TODO validate the currency
|
//TODO validate the currency
|
||||||
|
|
||||||
itr = form.find("title");
|
itr = form.find("title");
|
||||||
@ -47,6 +47,14 @@ void Handler::AddAsset::handle (Request& request) {
|
|||||||
|
|
||||||
asset.icon = itr->second;
|
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 {
|
try {
|
||||||
Session& session = srv->getSession(access);
|
Session& session = srv->getSession(access);
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ class Server;
|
|||||||
namespace Handler {
|
namespace Handler {
|
||||||
class AddAsset : public Handler {
|
class AddAsset : public Handler {
|
||||||
public:
|
public:
|
||||||
AddAsset (std::weak_ptr<Server> server);
|
AddAsset (const std::shared_ptr<Server>& server);
|
||||||
virtual void handle (Request& request) override;
|
virtual void handle (Request& request) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
#include "server/session.h"
|
#include "server/session.h"
|
||||||
#include "database/exceptions.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),
|
Handler("deleteAsset", Request::Method::post),
|
||||||
server(server)
|
server(server)
|
||||||
{}
|
{}
|
||||||
|
@ -11,7 +11,7 @@ class Server;
|
|||||||
namespace Handler {
|
namespace Handler {
|
||||||
class DeleteAsset : public Handler {
|
class DeleteAsset : public Handler {
|
||||||
public:
|
public:
|
||||||
DeleteAsset (std::weak_ptr<Server> server);
|
DeleteAsset (const std::shared_ptr<Server>& server);
|
||||||
|
|
||||||
virtual void handle (Request& request) override;
|
virtual void handle (Request& request) override;
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
#include "server/session.h"
|
#include "server/session.h"
|
||||||
#include "database/exceptions.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),
|
Handler("listAssets", Request::Method::get),
|
||||||
server(server)
|
server(server)
|
||||||
{}
|
{}
|
||||||
|
@ -11,7 +11,7 @@ class Server;
|
|||||||
namespace Handler {
|
namespace Handler {
|
||||||
class ListAssets : public Handler::Handler {
|
class ListAssets : public Handler::Handler {
|
||||||
public:
|
public:
|
||||||
ListAssets (std::weak_ptr<Server> server);
|
ListAssets (const std::shared_ptr<Server>& server);
|
||||||
void handle (Request& request) override;
|
void handle (Request& request) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
#include "server/server.h"
|
#include "server/server.h"
|
||||||
#include "database/exceptions.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),
|
Handler("login", Request::Method::post),
|
||||||
server(server)
|
server(server)
|
||||||
{}
|
{}
|
||||||
|
@ -12,7 +12,7 @@ namespace Handler {
|
|||||||
|
|
||||||
class Login : public Handler {
|
class Login : public Handler {
|
||||||
public:
|
public:
|
||||||
Login(std::weak_ptr<Server> server);
|
Login(const std::shared_ptr<Server>& server);
|
||||||
void handle(Request& request) override;
|
void handle(Request& request) override;
|
||||||
|
|
||||||
enum class Result {
|
enum class Result {
|
||||||
|
51
handler/mycurrencies.cpp
Normal file
51
handler/mycurrencies.cpp
Normal 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
21
handler/mycurrencies.h
Normal 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;
|
||||||
|
};
|
||||||
|
}
|
@ -8,7 +8,7 @@
|
|||||||
#include "request/redirect.h"
|
#include "request/redirect.h"
|
||||||
#include "database/exceptions.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),
|
Handler("poll", Request::Method::get),
|
||||||
server(server)
|
server(server)
|
||||||
{}
|
{}
|
||||||
|
@ -14,7 +14,7 @@ namespace Handler {
|
|||||||
|
|
||||||
class Poll : public Handler {
|
class Poll : public Handler {
|
||||||
public:
|
public:
|
||||||
Poll (std::weak_ptr<Server> server);
|
Poll (const std::shared_ptr<Server>& server);
|
||||||
void handle (Request& request) override;
|
void handle (Request& request) override;
|
||||||
|
|
||||||
enum class Result {
|
enum class Result {
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
#include "server/server.h"
|
#include "server/server.h"
|
||||||
#include "database/exceptions.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),
|
Handler("register", Request::Method::post),
|
||||||
server(server)
|
server(server)
|
||||||
{}
|
{}
|
||||||
|
@ -12,7 +12,7 @@ namespace Handler {
|
|||||||
|
|
||||||
class Register : public Handler {
|
class Register : public Handler {
|
||||||
public:
|
public:
|
||||||
Register(std::weak_ptr<Server> server);
|
Register(const std::shared_ptr<Server>& server);
|
||||||
void handle(Request& request) override;
|
void handle(Request& request) override;
|
||||||
|
|
||||||
enum class Result {
|
enum class Result {
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
#include "handler/listassets.h"
|
#include "handler/listassets.h"
|
||||||
#include "handler/addasset.h"
|
#include "handler/addasset.h"
|
||||||
#include "handler/deleteasset.h"
|
#include "handler/deleteasset.h"
|
||||||
|
#include "handler/mycurrencies.h"
|
||||||
|
|
||||||
#include "taskmanager/route.h"
|
#include "taskmanager/route.h"
|
||||||
|
|
||||||
@ -65,14 +66,17 @@ Server::Server ():
|
|||||||
Server::~Server () {}
|
Server::~Server () {}
|
||||||
|
|
||||||
void Server::run (int socketDescriptor) {
|
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::Info>());
|
||||||
router->addRoute(std::make_unique<Handler::Env>());
|
router->addRoute(std::make_unique<Handler::Env>());
|
||||||
router->addRoute(std::make_unique<Handler::Register>(shared_from_this()));
|
router->addRoute(std::make_unique<Handler::Register>(srv));
|
||||||
router->addRoute(std::make_unique<Handler::Login>(shared_from_this()));
|
router->addRoute(std::make_unique<Handler::Login>(srv));
|
||||||
router->addRoute(std::make_unique<Handler::Poll>(shared_from_this()));
|
router->addRoute(std::make_unique<Handler::Poll>(srv));
|
||||||
router->addRoute(std::make_unique<Handler::ListAssets>(shared_from_this()));
|
router->addRoute(std::make_unique<Handler::ListAssets>(srv));
|
||||||
router->addRoute(std::make_unique<Handler::AddAsset>(shared_from_this()));
|
router->addRoute(std::make_unique<Handler::AddAsset>(srv));
|
||||||
router->addRoute(std::make_unique<Handler::DeleteAsset>(shared_from_this()));
|
router->addRoute(std::make_unique<Handler::DeleteAsset>(srv));
|
||||||
|
router->addRoute(std::make_unique<Handler::MyCurrencies>(srv));
|
||||||
|
|
||||||
taskManager->start();
|
taskManager->start();
|
||||||
scheduler->start();
|
scheduler->start();
|
||||||
|
Loading…
Reference in New Issue
Block a user