schema directory for all datastructures of database, add-list assets requests, not tested
This commit is contained in:
parent
d33ec5def8
commit
4df8d4319e
@ -19,3 +19,4 @@ target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})
|
|||||||
|
|
||||||
add_subdirectory(mysql)
|
add_subdirectory(mysql)
|
||||||
add_subdirectory(migrations)
|
add_subdirectory(migrations)
|
||||||
|
add_subdirectory(schema)
|
||||||
|
@ -9,26 +9,10 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
|
#include "schema/session.h"
|
||||||
|
#include "schema/asset.h"
|
||||||
|
|
||||||
namespace DB {
|
namespace DB {
|
||||||
struct Session {
|
|
||||||
unsigned int id;
|
|
||||||
unsigned int owner;
|
|
||||||
std::string accessToken;
|
|
||||||
std::string renewToken;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Asset {
|
|
||||||
unsigned int id;
|
|
||||||
unsigned int owner;
|
|
||||||
unsigned int currency;
|
|
||||||
std::string title;
|
|
||||||
std::string icon;
|
|
||||||
// `color` INTEGER UNSIGNED DEFAULT 0,
|
|
||||||
// `balance` DECIMAL (20, 5) DEFAULT 0,
|
|
||||||
// `type` INTEGER UNSIGNED NOT NULL,
|
|
||||||
bool archived;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Interface {
|
class Interface {
|
||||||
public:
|
public:
|
||||||
enum class Type {
|
enum class Type {
|
||||||
@ -62,6 +46,7 @@ public:
|
|||||||
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(unsigned int owner) = 0;
|
||||||
|
virtual Asset addAsset(const Asset& asset) = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Interface(Type type);
|
Interface(Type type);
|
||||||
|
@ -21,8 +21,10 @@ constexpr const char* selectHash = "SELECT password FROM accounts where login =
|
|||||||
constexpr const char* createSessionQuery = "INSERT INTO sessions (`owner`, `access`, `renew`, `persist`, `device`)"
|
constexpr const char* createSessionQuery = "INSERT INTO sessions (`owner`, `access`, `renew`, `persist`, `device`)"
|
||||||
" 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, 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, archived FROM assets where owner = ?";
|
||||||
|
constexpr const char* insertAsset = "INSERT INTO assets (`owner`, `currency`, `title`, `icon`, `archived`)"
|
||||||
|
" VALUES (?, ?, ?, ?, ?)";
|
||||||
|
|
||||||
static const std::filesystem::path buildSQLPath = "database";
|
static const std::filesystem::path buildSQLPath = "database";
|
||||||
|
|
||||||
@ -323,36 +325,38 @@ DB::Session DB::MySQL::findSession(const std::string& accessToken) {
|
|||||||
if (result.empty())
|
if (result.empty())
|
||||||
throw NoSession("Couldn't find session with token " + a);
|
throw NoSession("Couldn't find session with token " + a);
|
||||||
|
|
||||||
DB::Session res;
|
return DB::Session(result[0]);
|
||||||
res.id = std::any_cast<unsigned int>(result[0][0]);
|
|
||||||
res.owner = std::any_cast<unsigned int>(result[0][1]);
|
|
||||||
res.renewToken = std::any_cast<const std::string&>(result[0][2]);
|
|
||||||
res.accessToken = a;
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<DB::Asset> DB::MySQL::listAssets (unsigned int owner) {
|
std::vector<DB::Asset> DB::MySQL::listAssets (unsigned int owner) {
|
||||||
MYSQL* con = &connection;
|
MYSQL* con = &connection;
|
||||||
|
|
||||||
Statement st(con, selectSession);
|
Statement st(con, selectAssets);
|
||||||
st.bind(&owner, MYSQL_TYPE_LONG, true);
|
st.bind(&owner, MYSQL_TYPE_LONG, true);
|
||||||
st.execute();
|
st.execute();
|
||||||
std::vector<std::vector<std::any>> res = st.fetchResult();
|
std::vector<std::vector<std::any>> res = st.fetchResult();
|
||||||
|
|
||||||
std::size_t size = res.size();
|
std::size_t size = res.size();
|
||||||
std::vector<DB::Asset> result(size);
|
std::vector<DB::Asset> result(size);
|
||||||
for (std::size_t i = 0; i < size; ++i) {
|
for (std::size_t i = 0; i < size; ++i)
|
||||||
const std::vector<std::any>& proto = res[i];
|
result[i].parse(res[i]);
|
||||||
DB::Asset& asset = result[i];
|
|
||||||
asset.id = std::any_cast<unsigned int>(proto[0]);
|
|
||||||
asset.owner = std::any_cast<unsigned int>(proto[1]);
|
|
||||||
asset.currency = std::any_cast<unsigned int>(proto[2]);
|
|
||||||
asset.title = std::any_cast<const std::string&>(proto[3]);
|
|
||||||
asset.icon = std::any_cast<const std::string&>(proto[4]);
|
|
||||||
asset.archived = std::any_cast<bool>(proto[5]); //TODO
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DB::Asset DB::MySQL::addAsset(const Asset& asset) {
|
||||||
|
MYSQL* con = &connection;
|
||||||
|
Asset result = asset;
|
||||||
|
|
||||||
|
Statement session(con, insertAsset);
|
||||||
|
session.bind(&result.owner, MYSQL_TYPE_LONG, true);
|
||||||
|
session.bind(&result.currency, MYSQL_TYPE_LONG, true);
|
||||||
|
session.bind(result.title.data(), MYSQL_TYPE_STRING);
|
||||||
|
session.bind(result.icon.data(), MYSQL_TYPE_STRING);
|
||||||
|
session.bind(&result.archived, MYSQL_TYPE_TINY);
|
||||||
|
session.execute();
|
||||||
|
|
||||||
|
result.id = lastInsertedId();
|
||||||
|
|
||||||
|
return asset;
|
||||||
|
}
|
||||||
|
@ -36,6 +36,7 @@ public:
|
|||||||
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 (unsigned int owner) override;
|
||||||
|
Asset addAsset (const Asset& asset) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void executeFile (const std::filesystem::path& relativePath);
|
void executeFile (const std::filesystem::path& relativePath);
|
||||||
|
14
database/schema/CMakeLists.txt
Normal file
14
database/schema/CMakeLists.txt
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
|
||||||
|
#SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
|
set(HEADERS
|
||||||
|
session.h
|
||||||
|
asset.h
|
||||||
|
)
|
||||||
|
|
||||||
|
set(SOURCES
|
||||||
|
session.cpp
|
||||||
|
asset.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})
|
44
database/schema/asset.cpp
Normal file
44
database/schema/asset.cpp
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
//SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
|
||||||
|
//SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
|
#include "asset.h"
|
||||||
|
|
||||||
|
DB::Asset::Asset ():
|
||||||
|
id(),
|
||||||
|
owner(),
|
||||||
|
currency(),
|
||||||
|
title(),
|
||||||
|
icon(),
|
||||||
|
archived()
|
||||||
|
{}
|
||||||
|
|
||||||
|
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])),
|
||||||
|
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]))
|
||||||
|
{}
|
||||||
|
|
||||||
|
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]);
|
||||||
|
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]);
|
||||||
|
}
|
||||||
|
|
||||||
|
nlohmann::json DB::Asset::toJSON () const {
|
||||||
|
nlohmann::json result = nlohmann::json::object();
|
||||||
|
|
||||||
|
result["id"] = id;
|
||||||
|
//result["owner"] = owner;
|
||||||
|
//result["currency"] = currency;
|
||||||
|
result["title"] = title;
|
||||||
|
result["icon"] = icon;
|
||||||
|
result["archived"] = archived;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
33
database/schema/asset.h
Normal file
33
database/schema/asset.h
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
//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 Asset {
|
||||||
|
public:
|
||||||
|
Asset ();
|
||||||
|
Asset (const std::vector<std::any>& vec);
|
||||||
|
|
||||||
|
void parse (const std::vector<std::any>& vec);
|
||||||
|
nlohmann::json toJSON () const;
|
||||||
|
|
||||||
|
public:
|
||||||
|
unsigned int id;
|
||||||
|
unsigned int owner;
|
||||||
|
unsigned int currency;
|
||||||
|
std::string title;
|
||||||
|
std::string icon;
|
||||||
|
// `color` INTEGER UNSIGNED DEFAULT 0,
|
||||||
|
// `balance` DECIMAL (20, 5) DEFAULT 0,
|
||||||
|
// `type` INTEGER UNSIGNED NOT NULL,
|
||||||
|
bool archived;
|
||||||
|
};
|
||||||
|
}
|
18
database/schema/session.cpp
Normal file
18
database/schema/session.cpp
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
//SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
|
||||||
|
//SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
|
#include "session.h"
|
||||||
|
|
||||||
|
DB::Session::Session ():
|
||||||
|
id(),
|
||||||
|
owner(),
|
||||||
|
accessToken(),
|
||||||
|
renewToken()
|
||||||
|
{}
|
||||||
|
|
||||||
|
DB::Session::Session (const std::vector<std::any>& vec):
|
||||||
|
id(std::any_cast<unsigned int>(vec[0])),
|
||||||
|
owner(std::any_cast<unsigned int>(vec[1])),
|
||||||
|
accessToken(std::any_cast<const std::string&>(vec[2])),
|
||||||
|
renewToken(std::any_cast<const std::string&>(vec[3]))
|
||||||
|
{}
|
23
database/schema/session.h
Normal file
23
database/schema/session.h
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
//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>
|
||||||
|
|
||||||
|
namespace DB {
|
||||||
|
class Session {
|
||||||
|
public:
|
||||||
|
Session ();
|
||||||
|
Session (const std::vector<std::any>& vec);
|
||||||
|
|
||||||
|
public:
|
||||||
|
unsigned int id;
|
||||||
|
unsigned int owner;
|
||||||
|
std::string accessToken;
|
||||||
|
std::string renewToken;
|
||||||
|
};
|
||||||
|
}
|
@ -9,6 +9,7 @@ set(HEADERS
|
|||||||
login.h
|
login.h
|
||||||
poll.h
|
poll.h
|
||||||
listassets.h
|
listassets.h
|
||||||
|
addasset.h
|
||||||
)
|
)
|
||||||
|
|
||||||
set(SOURCES
|
set(SOURCES
|
||||||
@ -19,6 +20,7 @@ set(SOURCES
|
|||||||
login.cpp
|
login.cpp
|
||||||
poll.cpp
|
poll.cpp
|
||||||
listassets.cpp
|
listassets.cpp
|
||||||
|
addasset.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})
|
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})
|
||||||
|
77
handler/addasset.cpp
Normal file
77
handler/addasset.cpp
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
//SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
|
||||||
|
//SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
|
#include "addasset.h"
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
#include "server/server.h"
|
||||||
|
#include "server/session.h"
|
||||||
|
#include "database/exceptions.h"
|
||||||
|
|
||||||
|
Handler::AddAsset::AddAsset (std::weak_ptr<Server> server):
|
||||||
|
Handler("addAsset", Request::Method::post),
|
||||||
|
server(server)
|
||||||
|
{}
|
||||||
|
|
||||||
|
void Handler::AddAsset::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);
|
||||||
|
|
||||||
|
std::map form = request.getForm();
|
||||||
|
std::map<std::string, std::string>::const_iterator itr = form.find("currency");
|
||||||
|
if (itr == form.end())
|
||||||
|
return error(request, Response::Status::badRequest);
|
||||||
|
|
||||||
|
DB::Asset asset;
|
||||||
|
asset.currency = std::stoi(itr->second);
|
||||||
|
//TODO validate the currency
|
||||||
|
|
||||||
|
itr = form.find("title");
|
||||||
|
if (itr == form.end())
|
||||||
|
return error(request, Response::Status::badRequest);
|
||||||
|
|
||||||
|
asset.title = itr->second;
|
||||||
|
|
||||||
|
itr = form.find("icon");
|
||||||
|
if (itr == form.end())
|
||||||
|
return error(request, Response::Status::badRequest);
|
||||||
|
|
||||||
|
asset.icon = itr->second;
|
||||||
|
|
||||||
|
try {
|
||||||
|
Session& session = srv->getSession(access);
|
||||||
|
|
||||||
|
asset.owner = session.owner;
|
||||||
|
asset = srv->getDatabase()->addAsset(asset);
|
||||||
|
|
||||||
|
nlohmann::json body = nlohmann::json::object();
|
||||||
|
body["asset"] = asset.toJSON();
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Handler::AddAsset::error (Request& request, Response::Status status) {
|
||||||
|
Response& res = request.createResponse(status);
|
||||||
|
res.send();
|
||||||
|
}
|
21
handler/addasset.h
Normal file
21
handler/addasset.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 AddAsset : public Handler {
|
||||||
|
public:
|
||||||
|
AddAsset (std::weak_ptr<Server> server);
|
||||||
|
virtual void handle (Request& request) override;
|
||||||
|
|
||||||
|
static void error (Request& request, Response::Status status);
|
||||||
|
private:
|
||||||
|
std::weak_ptr<Server> server;
|
||||||
|
};
|
||||||
|
}
|
@ -26,7 +26,18 @@ void Handler::ListAssets::handle (Request& request) {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
Session& session = srv->getSession(access);
|
Session& session = srv->getSession(access);
|
||||||
|
std::vector<DB::Asset> assets = srv->getDatabase()->listAssets(session.owner);
|
||||||
|
|
||||||
|
nlohmann::json arr = nlohmann::json::array();
|
||||||
|
for (const DB::Asset& asset : assets)
|
||||||
|
arr.push_back(asset.toJSON());
|
||||||
|
|
||||||
|
nlohmann::json body = nlohmann::json::object();
|
||||||
|
body["assets"] = arr;
|
||||||
|
|
||||||
|
Response& res = request.createResponse(Response::Status::ok);
|
||||||
|
res.setBody(body);
|
||||||
|
res.send();
|
||||||
|
|
||||||
} catch (const DB::NoSession& e) {
|
} catch (const DB::NoSession& e) {
|
||||||
return error(request, Response::Status::unauthorized);
|
return error(request, Response::Status::unauthorized);
|
||||||
|
@ -12,6 +12,8 @@
|
|||||||
#include "handler/register.h"
|
#include "handler/register.h"
|
||||||
#include "handler/login.h"
|
#include "handler/login.h"
|
||||||
#include "handler/poll.h"
|
#include "handler/poll.h"
|
||||||
|
#include "handler/listassets.h"
|
||||||
|
#include "handler/addasset.h"
|
||||||
|
|
||||||
#include "taskmanager/route.h"
|
#include "taskmanager/route.h"
|
||||||
|
|
||||||
@ -67,6 +69,8 @@ void Server::run(int socketDescriptor) {
|
|||||||
router->addRoute(std::make_unique<Handler::Register>(shared_from_this()));
|
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::Login>(shared_from_this()));
|
||||||
router->addRoute(std::make_unique<Handler::Poll>(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()));
|
||||||
|
|
||||||
taskManager->start();
|
taskManager->start();
|
||||||
scheduler->start();
|
scheduler->start();
|
||||||
@ -197,3 +201,8 @@ Session& Server::getSession (const std::string& accessToken) {
|
|||||||
);
|
);
|
||||||
return *session.get();
|
return *session.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
DB::Resource Server::getDatabase () {
|
||||||
|
return pool->request();
|
||||||
|
}
|
||||||
|
@ -39,6 +39,7 @@ public:
|
|||||||
bool validatePassword(const std::string& login, const std::string& password);
|
bool validatePassword(const std::string& login, const std::string& password);
|
||||||
Session& openSession(const std::string& login);
|
Session& openSession(const std::string& login);
|
||||||
Session& getSession(const std::string& accessToken);
|
Session& getSession(const std::string& accessToken);
|
||||||
|
DB::Resource getDatabase();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void handleRequest(std::unique_ptr<Request> request);
|
void handleRequest(std::unique_ptr<Request> request);
|
||||||
|
Loading…
Reference in New Issue
Block a user