1
0
forked from blue/pica

debug, deleting assets

This commit is contained in:
Blue 2024-01-21 16:23:48 -03:00
parent 19d786631a
commit a2c2c2a883
Signed by untrusted user: 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 std::vector<Asset> listAssets(unsigned int owner) = 0;
virtual Asset addAsset(const Asset& asset) = 0;
virtual bool deleteAsset(unsigned int assetId, unsigned int actorId) = 0;
protected:
Interface(Type type);

View File

@ -56,6 +56,7 @@ CREATE TABLE IF NOT EXISTS currencies (
`value` DECIMAL (20, 5) NOT NULL,
`source` TEXT,
`description` TEXT,
`icon` VARCHAR(256),
INDEX manual_idx (manual)
);
@ -71,6 +72,7 @@ CREATE TABLE IF NOT EXISTS assets (
`balance` DECIMAL (20, 5) DEFAULT 0,
`type` INTEGER UNSIGNED NOT NULL,
`archived` BOOLEAN DEFAULT FALSE,
`created` TIMESTAMP DEFAULT UTC_TIMESTAMP(),
INDEX owner_idx (owner),
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* insertAsset = "INSERT INTO assets (`owner`, `currency`, `title`, `icon`, `archived`, `type`)"
" VALUES (?, ?, ?, ?, ?, 1)";
constexpr const char* removeAsset = "DELETE FROM assets where `id` = ? AND `owner` = ?";
static const std::filesystem::path buildSQLPath = "database";
@ -348,15 +349,29 @@ 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();
Statement add(con, insertAsset);
add.bind(&result.owner, MYSQL_TYPE_LONG, true);
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.archived, MYSQL_TYPE_TINY);
add.execute();
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;
std::vector<Asset> listAssets (unsigned int owner) override;
Asset addAsset (const Asset& asset) override;
bool deleteAsset(unsigned int assetId, unsigned int actorId) override;
private:
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;
}
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 execute();
unsigned int affectedRows();
std::vector<std::vector<std::any>> fetchResult();
private:

View File

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

View File

@ -68,8 +68,3 @@ void Handler::AddAsset::handle (Request& request) {
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);
virtual void handle (Request& request) override;
static void error (Request& request, Response::Status status);
private:
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() {}
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:
Handler(const std::string& path, Request::Method method);
protected:
static void error (Request& request, Response::Status status);
public:
virtual ~Handler();

View File

@ -49,8 +49,3 @@ void Handler::ListAssets::handle (Request& request) {
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);
void handle (Request& request) override;
static void error (Request& request, Response::Status status);
private:
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,
400,
401,
403,
404,
405,
409,
@ -19,6 +20,7 @@ constexpr std::array<std::string_view, static_cast<uint8_t>(Response::Status::__
"Status: 200 OK",
"Status: 400 Bad Request",
"Status: 401 Unauthorized",
"Status: 403 Forbidden",
"Status: 404 Not Found",
"Status: 405 Method Not Allowed",
"Status: 409 Conflict",

View File

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

View File

@ -14,6 +14,7 @@
#include "handler/poll.h"
#include "handler/listassets.h"
#include "handler/addasset.h"
#include "handler/deleteasset.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::ListAssets>(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();
scheduler->start();

View File

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

View File

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