Add transaction handler, delete transaction and list transactions queries, session methods
This commit is contained in:
parent
c2d4bf5ccb
commit
e1d5b6c76c
9 changed files with 195 additions and 4 deletions
|
@ -13,6 +13,7 @@ set(HEADERS
|
|||
deleteasset.h
|
||||
updateasset.h
|
||||
currencies.h
|
||||
addtransaction.h
|
||||
)
|
||||
|
||||
set(SOURCES
|
||||
|
@ -27,6 +28,7 @@ set(SOURCES
|
|||
deleteasset.cpp
|
||||
updateasset.cpp
|
||||
currencies.cpp
|
||||
addtransaction.cpp
|
||||
)
|
||||
|
||||
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})
|
||||
|
|
78
handler/addtransaction.cpp
Normal file
78
handler/addtransaction.cpp
Normal file
|
@ -0,0 +1,78 @@
|
|||
//SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
|
||||
//SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include "addtransaction.h"
|
||||
|
||||
#include <map>
|
||||
|
||||
#include "server/server.h"
|
||||
#include "server/session.h"
|
||||
#include "database/exceptions.h"
|
||||
|
||||
Handler::AddTransaction::AddTransaction (const std::shared_ptr<Server>& server):
|
||||
Handler("addTransaction", Request::Method::post),
|
||||
server(server)
|
||||
{}
|
||||
|
||||
void Handler::AddTransaction::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("asset");
|
||||
if (itr == form.end())
|
||||
return error(request, Response::Status::badRequest);
|
||||
|
||||
DB::Transaction txn;
|
||||
txn.asset = std::stoul(itr->second);
|
||||
//TODO validate the asset
|
||||
|
||||
itr = form.find("value");
|
||||
if (itr == form.end())
|
||||
return error(request, Response::Status::badRequest);
|
||||
|
||||
txn.value = std::stod(itr->second);
|
||||
|
||||
itr = form.find("performed");
|
||||
if (itr == form.end())
|
||||
return error(request, Response::Status::badRequest);
|
||||
|
||||
txn.performed = std::stoul(itr->second);
|
||||
|
||||
itr = form.find("notes");
|
||||
if (itr != form.end())
|
||||
txn.notes = itr->second;
|
||||
|
||||
itr = form.find("parent");
|
||||
if (itr != form.end())
|
||||
txn.parent = std::stoul(itr->second);
|
||||
|
||||
try {
|
||||
Session& session = srv->getSession(access);
|
||||
|
||||
txn.initiator = session.owner;
|
||||
txn = srv->getDatabase()->addTransaction(txn);
|
||||
|
||||
Response& res = request.createResponse(Response::Status::ok);
|
||||
res.send();
|
||||
|
||||
session.transactionAdded(txn);
|
||||
|
||||
} 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/addtransaction.h
Normal file
20
handler/addtransaction.h
Normal 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 AddTransaction : public Handler {
|
||||
public:
|
||||
AddTransaction (const std::shared_ptr<Server>& server);
|
||||
virtual void handle (Request& request) override;
|
||||
|
||||
private:
|
||||
std::weak_ptr<Server> server;
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue