1
0
forked from blue/pica

Small fixes, update asset method

This commit is contained in:
Blue 2024-04-07 20:03:10 -03:00
parent a9f46b2ab0
commit 4914a467e5
Signed by untrusted user: blue
GPG Key ID: 9B203B252A63EE38
13 changed files with 172 additions and 6 deletions

View File

@ -48,6 +48,7 @@ public:
virtual Session findSession(const std::string& accessToken) = 0;
virtual std::vector<Asset> listAssets(uint32_t owner) = 0;
virtual Asset addAsset(const Asset& asset) = 0;
virtual void updateAsset(const Asset& asset) = 0;
virtual bool deleteAsset(uint32_t assetId, uint32_t actorId) = 0;
virtual std::vector<Currency> listUsedCurrencies(uint32_t owner) = 0;

View File

@ -25,8 +25,10 @@ constexpr const char* selectSession = "SELECT id, owner, access, renew FROM sess
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* updateAssetQuery = "UPDATE assets SET `owner` = ?, `currency` = ?, `title` = ?, `icon` = ?, `color` = ?, `archived` = ?"
" WHERE `id` = ?";
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"
constexpr const char* selectUsedCurrencies = "SELECT DISTINCT c.id, c.code, c.title, c.manual, c.icon FROM currencies c"
" JOIN assets a ON c.id = a.currency"
" WHERE a.owner = ?";
@ -366,6 +368,21 @@ DB::Asset DB::MySQL::addAsset(const Asset& asset) {
return result;
}
void DB::MySQL::updateAsset(const Asset& asset) {
MYSQL* con = &connection;
Asset result = asset;
Statement update(con, updateAssetQuery);
update.bind(&result.owner, MYSQL_TYPE_LONG, true);
update.bind(&result.currency, MYSQL_TYPE_LONG, true);
update.bind(result.title.data(), MYSQL_TYPE_STRING);
update.bind(result.icon.data(), MYSQL_TYPE_STRING);
update.bind(&result.color, MYSQL_TYPE_LONG, true);
update.bind(&result.archived, MYSQL_TYPE_TINY);
update.bind(&result.id, MYSQL_TYPE_LONG, true);
update.execute();
}
bool DB::MySQL::deleteAsset(uint32_t assetId, uint32_t actorId) {
Statement del(&connection, removeAsset);
del.bind(&assetId, MYSQL_TYPE_LONG, true);

View File

@ -36,6 +36,7 @@ public:
Session findSession (const std::string& accessToken) override;
std::vector<Asset> listAssets (uint32_t owner) override;
Asset addAsset (const Asset& asset) override;
void updateAsset (const Asset& asset) override;
bool deleteAsset(uint32_t assetId, uint32_t actorId) override;
std::vector<Currency> listUsedCurrencies(uint32_t owner) override;

View File

@ -11,6 +11,7 @@ set(HEADERS
assets.h
addasset.h
deleteasset.h
updateasset.h
currencies.h
)
@ -24,6 +25,7 @@ set(SOURCES
assets.cpp
addasset.cpp
deleteasset.cpp
updateasset.cpp
currencies.cpp
)

View File

View File

84
handler/updateasset.cpp Normal file
View File

@ -0,0 +1,84 @@
//SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
//SPDX-License-Identifier: GPL-3.0-or-later
#include "updateasset.h"
#include <map>
#include "server/server.h"
#include "server/session.h"
#include "database/exceptions.h"
Handler::UpdateAsset::UpdateAsset (const std::shared_ptr<Server>& server):
Handler("updateAsset", Request::Method::post),
server(server)
{}
void Handler::UpdateAsset::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);
DB::Asset asset;
asset.id = std::stoul(itr->second);
itr = form.find("currency");
if (itr == form.end())
return error(request, Response::Status::badRequest);
asset.currency = std::stoul(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 {
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);
asset.owner = session.owner;
srv->getDatabase()->updateAsset(asset);
Response& res = request.createResponse(Response::Status::ok);
res.send();
session.assetChanged(asset);
} catch (const DB::NoSession& e) {
return error(request, Response::Status::unauthorized);
} catch (const std::exception& e) {
std::cerr << "Exception on " << path << ":\n\t" << e.what() << std::endl;
return error(request, Response::Status::internalError);
} catch (...) {
std::cerr << "Unknown exception on " << path << std::endl;
return error(request, Response::Status::internalError);
}
}

20
handler/updateasset.h Normal file
View File

@ -0,0 +1,20 @@
//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 UpdateAsset : public Handler {
public:
UpdateAsset (const std::shared_ptr<Server>& server);
virtual void handle (Request& request) override;
private:
std::weak_ptr<Server> server;
};
}

View File

@ -25,4 +25,4 @@ fi
start_service "mariadb"
start_service "httpd"
./@PROJECT_NAME@
$(dirname "$0")/@PROJECT_NAME@

View File

@ -16,6 +16,7 @@
#include "handler/addasset.h"
#include "handler/deleteasset.h"
#include "handler/currencies.h"
#include "handler/updateasset.h"
#include "taskmanager/route.h"
@ -77,6 +78,7 @@ void Server::run (int socketDescriptor) {
router->addRoute(std::make_unique<Handler::AddAsset>(srv));
router->addRoute(std::make_unique<Handler::DeleteAsset>(srv));
router->addRoute(std::make_unique<Handler::Currencies>(srv));
router->addRoute(std::make_unique<Handler::UpdateAsset>(srv));
taskManager->start();
scheduler->start();

View File

@ -110,17 +110,53 @@ void Session::assetAdded (const DB::Asset& asset) {
checkUpdates();
}
void Session::assetChanged (const DB::Asset& asset) {
std::lock_guard lock(mtx);
std::map<std::string, nlohmann::json>& assets = cache["assets"];
auto itr = assets.find("changed");
if (itr == assets.end())
itr = assets.emplace("changed", nlohmann::json::array()).first;
removeByID(itr->second, asset.id);
itr->second.push_back(asset.toJSON());
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;
auto itr = assets.find("added");
if (itr != assets.end())
removeByID(itr->second, assetId);
else {
itr = assets.find("removed");
if (itr == assets.end())
itr = assets.emplace("removed", nlohmann::json::array()).first;
itr->second.push_back(assetId);
}
itr = assets.find("changed");
if (itr != assets.end())
removeByID(itr->second, assetId);
addedItr->second.push_back(assetId);
checkUpdates();
}
void Session::removeByID(nlohmann::json& array, unsigned int id) {
array.erase(
std::remove_if(
array.begin(),
array.end(),
[id](const nlohmann::json& item) {
return item["id"].get<unsigned int>() == id;
}
),
array.end()
);
}
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 assetChanged (const DB::Asset& asset);
void assetRemoved (unsigned int assetId);
private:
@ -45,6 +46,8 @@ private:
void sendUpdates (std::unique_ptr<Request> request);
void checkUpdates ();
void static removeByID (nlohmann::json& array, unsigned int id);
private:
std::weak_ptr<TM::Scheduler> scheduler;
std::string access;