debug, deleting assets

This commit is contained in:
Blue 2024-01-21 16:23:48 -03:00
parent 19d786631a
commit a2c2c2a883
Signed by: blue
GPG Key ID: 9B203B252A63EE38
20 changed files with 139 additions and 21 deletions

View File

@ -47,6 +47,7 @@ public:
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; virtual Asset addAsset(const Asset& asset) = 0;
virtual bool deleteAsset(unsigned int assetId, unsigned int actorId) = 0;
protected: protected:
Interface(Type type); Interface(Type type);

View File

@ -56,6 +56,7 @@ CREATE TABLE IF NOT EXISTS currencies (
`value` DECIMAL (20, 5) NOT NULL, `value` DECIMAL (20, 5) NOT NULL,
`source` TEXT, `source` TEXT,
`description` TEXT, `description` TEXT,
`icon` VARCHAR(256),
INDEX manual_idx (manual) INDEX manual_idx (manual)
); );
@ -71,6 +72,7 @@ CREATE TABLE IF NOT EXISTS assets (
`balance` DECIMAL (20, 5) DEFAULT 0, `balance` DECIMAL (20, 5) DEFAULT 0,
`type` INTEGER UNSIGNED NOT NULL, `type` INTEGER UNSIGNED NOT NULL,
`archived` BOOLEAN DEFAULT FALSE, `archived` BOOLEAN DEFAULT FALSE,
`created` TIMESTAMP DEFAULT UTC_TIMESTAMP(),
INDEX owner_idx (owner), INDEX owner_idx (owner),
INDEX archived_idx (archived), INDEX archived_idx (archived),

View File

@ -25,6 +25,7 @@ constexpr const char* selectSession = "SELECT id, owner, access, renew FROM sess
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`, `type`)" constexpr const char* insertAsset = "INSERT INTO assets (`owner`, `currency`, `title`, `icon`, `archived`, `type`)"
" VALUES (?, ?, ?, ?, ?, 1)"; " VALUES (?, ?, ?, ?, ?, 1)";
constexpr const char* removeAsset = "DELETE FROM assets where `id` = ? AND `owner` = ?";
static const std::filesystem::path buildSQLPath = "database"; static const std::filesystem::path buildSQLPath = "database";
@ -348,15 +349,29 @@ DB::Asset DB::MySQL::addAsset(const Asset& asset) {
MYSQL* con = &connection; MYSQL* con = &connection;
Asset result = asset; Asset result = asset;
Statement session(con, insertAsset); Statement add(con, insertAsset);
session.bind(&result.owner, MYSQL_TYPE_LONG, true); add.bind(&result.owner, MYSQL_TYPE_LONG, true);
session.bind(&result.currency, MYSQL_TYPE_LONG, true); add.bind(&result.currency, MYSQL_TYPE_LONG, true);
session.bind(result.title.data(), MYSQL_TYPE_STRING); add.bind(result.title.data(), MYSQL_TYPE_STRING);
session.bind(result.icon.data(), MYSQL_TYPE_STRING); add.bind(result.icon.data(), MYSQL_TYPE_STRING);
session.bind(&result.archived, MYSQL_TYPE_TINY); add.bind(&result.archived, MYSQL_TYPE_TINY);
session.execute(); add.execute();
result.id = lastInsertedId(); result.id = lastInsertedId();
return asset; return result;
}
bool DB::MySQL::deleteAsset(unsigned int assetId, unsigned int actorId) {
MYSQL* con = &connection;
Statement del(con, removeAsset);
del.bind(&assetId, MYSQL_TYPE_LONG, true);
del.bind(&actorId, MYSQL_TYPE_LONG, true);
del.execute();
if (del.affectedRows() == 0)
return false;
return true;
} }

View File

@ -37,6 +37,7 @@ public:
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; Asset addAsset (const Asset& asset) override;
bool deleteAsset(unsigned int assetId, unsigned int actorId) override;
private: private:
void executeFile (const std::filesystem::path& relativePath); void executeFile (const std::filesystem::path& relativePath);

View File

@ -150,3 +150,6 @@ std::vector<std::vector<std::any>> DB::MySQL::Statement::fetchResult() {
return result; return result;
} }
unsigned int DB::MySQL::Statement::affectedRows () {
return mysql_stmt_affected_rows(stmt.get());
}

View File

@ -22,6 +22,7 @@ public:
void bind(void* value, enum_field_types type, bool usigned = false); void bind(void* value, enum_field_types type, bool usigned = false);
void execute(); void execute();
unsigned int affectedRows();
std::vector<std::vector<std::any>> fetchResult(); std::vector<std::vector<std::any>> fetchResult();
private: private:

View File

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

View File

@ -68,8 +68,3 @@ void Handler::AddAsset::handle (Request& request) {
return error(request, Response::Status::internalError); return error(request, Response::Status::internalError);
} }
} }
void Handler::AddAsset::error (Request& request, Response::Status status) {
Response& res = request.createResponse(status);
res.send();
}

View File

@ -14,7 +14,6 @@ public:
AddAsset (std::weak_ptr<Server> server); AddAsset (std::weak_ptr<Server> server);
virtual void handle (Request& request) override; virtual void handle (Request& request) override;
static void error (Request& request, Response::Status status);
private: private:
std::weak_ptr<Server> server; std::weak_ptr<Server> server;
}; };

59
handler/deleteasset.cpp Normal file
View File

@ -0,0 +1,59 @@
//SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
//SPDX-License-Identifier: GPL-3.0-or-later
#include "deleteasset.h"
#include "server/server.h"
#include "server/session.h"
#include "database/exceptions.h"
Handler::DeleteAsset::DeleteAsset (std::weak_ptr<Server> server):
Handler("deleteAsset", Request::Method::post),
server(server)
{}
void Handler::DeleteAsset::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("id");
if (itr == form.end())
return error(request, Response::Status::badRequest);
unsigned int assetId;
try {
assetId = std::stoul(itr->second);
} catch (const std::exception& e) {
return error(request, Response::Status::badRequest);
}
try {
Session& session = srv->getSession(access);
bool success = srv->getDatabase()->deleteAsset(assetId, session.owner);
if (!success)
return error(request, Response::Status::forbidden);
Response& res = request.createResponse(Response::Status::ok);
res.send();
session.assetRemoved(assetId);
} 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/deleteasset.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 DeleteAsset : public Handler {
public:
DeleteAsset (std::weak_ptr<Server> server);
virtual void handle (Request& request) override;
private:
std::weak_ptr<Server> server;
};
}

View File

@ -9,3 +9,8 @@ Handler::Handler::Handler(const std::string& path, Request::Method method):
{} {}
Handler::Handler::~Handler() {} Handler::Handler::~Handler() {}
void Handler::Handler::error (Request& request, Response::Status status) {
Response& res = request.createResponse(status);
res.send();
}

View File

@ -15,6 +15,9 @@ class Handler {
protected: protected:
Handler(const std::string& path, Request::Method method); Handler(const std::string& path, Request::Method method);
protected:
static void error (Request& request, Response::Status status);
public: public:
virtual ~Handler(); virtual ~Handler();

View File

@ -49,8 +49,3 @@ void Handler::ListAssets::handle (Request& request) {
return error(request, Response::Status::internalError); return error(request, Response::Status::internalError);
} }
} }
void Handler::ListAssets::error (Request& request, Response::Status status) {
Response& res = request.createResponse(status);
res.send();
}

View File

@ -14,7 +14,6 @@ public:
ListAssets (std::weak_ptr<Server> server); ListAssets (std::weak_ptr<Server> server);
void handle (Request& request) override; void handle (Request& request) override;
static void error (Request& request, Response::Status status);
private: private:
std::weak_ptr<Server> server; std::weak_ptr<Server> server;
}; };

View File

@ -9,6 +9,7 @@ constexpr std::array<uint16_t, static_cast<uint8_t>(Response::Status::__size)> s
200, 200,
400, 400,
401, 401,
403,
404, 404,
405, 405,
409, 409,
@ -19,6 +20,7 @@ constexpr std::array<std::string_view, static_cast<uint8_t>(Response::Status::__
"Status: 200 OK", "Status: 200 OK",
"Status: 400 Bad Request", "Status: 400 Bad Request",
"Status: 401 Unauthorized", "Status: 401 Unauthorized",
"Status: 403 Forbidden",
"Status: 404 Not Found", "Status: 404 Not Found",
"Status: 405 Method Not Allowed", "Status: 405 Method Not Allowed",
"Status: 409 Conflict", "Status: 409 Conflict",

View File

@ -20,6 +20,7 @@ public:
ok, ok,
badRequest, badRequest,
unauthorized, unauthorized,
forbidden,
notFound, notFound,
methodNotAllowed, methodNotAllowed,
conflict, conflict,

View File

@ -14,6 +14,7 @@
#include "handler/poll.h" #include "handler/poll.h"
#include "handler/listassets.h" #include "handler/listassets.h"
#include "handler/addasset.h" #include "handler/addasset.h"
#include "handler/deleteasset.h"
#include "taskmanager/route.h" #include "taskmanager/route.h"
@ -71,6 +72,7 @@ void Server::run (int socketDescriptor) {
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::ListAssets>(shared_from_this()));
router->addRoute(std::make_unique<Handler::AddAsset>(shared_from_this())); router->addRoute(std::make_unique<Handler::AddAsset>(shared_from_this()));
router->addRoute(std::make_unique<Handler::DeleteAsset>(shared_from_this()));
taskManager->start(); taskManager->start();
scheduler->start(); scheduler->start();

View File

@ -101,7 +101,7 @@ void Session::onTimeout () {
void Session::assetAdded (const DB::Asset& asset) { void Session::assetAdded (const DB::Asset& asset) {
std::lock_guard lock(mtx); std::lock_guard lock(mtx);
auto assets = cache["assets"]; std::map<std::string, nlohmann::json>& assets = cache["assets"];
auto addedItr = assets.find("added"); auto addedItr = assets.find("added");
if (addedItr == assets.end()) if (addedItr == assets.end())
addedItr = assets.emplace("added", nlohmann::json::array()).first; addedItr = assets.emplace("added", nlohmann::json::array()).first;
@ -110,6 +110,17 @@ void Session::assetAdded (const DB::Asset& asset) {
checkUpdates(); checkUpdates();
} }
void Session::assetRemoved (unsigned int assetId) {
std::lock_guard lock(mtx);
std::map<std::string, nlohmann::json>& assets = cache["assets"];
auto addedItr = assets.find("removed");
if (addedItr == assets.end())
addedItr = assets.emplace("removed", nlohmann::json::array()).first;
addedItr->second.push_back(assetId);
checkUpdates();
}
void Session::checkUpdates () { void Session::checkUpdates () {
std::shared_ptr<TM::Scheduler> sch = scheduler.lock(); std::shared_ptr<TM::Scheduler> sch = scheduler.lock();
if (polling) { if (polling) {

View File

@ -38,6 +38,7 @@ public:
const unsigned int owner; const unsigned int owner;
void assetAdded (const DB::Asset& asset); void assetAdded (const DB::Asset& asset);
void assetRemoved (unsigned int assetId);
private: private:
void onTimeout (); void onTimeout ();