update delete and list transaction requests
This commit is contained in:
parent
e1d5b6c76c
commit
7c4adaf450
11 changed files with 277 additions and 4 deletions
|
@ -14,6 +14,9 @@ set(HEADERS
|
|||
updateasset.h
|
||||
currencies.h
|
||||
addtransaction.h
|
||||
transactions.h
|
||||
deletetransaction.h
|
||||
updatetransaction.h
|
||||
)
|
||||
|
||||
set(SOURCES
|
||||
|
@ -29,6 +32,9 @@ set(SOURCES
|
|||
updateasset.cpp
|
||||
currencies.cpp
|
||||
addtransaction.cpp
|
||||
transactions.cpp
|
||||
deletetransaction.cpp
|
||||
updatetransaction.cpp
|
||||
)
|
||||
|
||||
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})
|
||||
|
|
59
handler/deletetransaction.cpp
Normal file
59
handler/deletetransaction.cpp
Normal file
|
@ -0,0 +1,59 @@
|
|||
//SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
|
||||
//SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include "deletetransaction.h"
|
||||
|
||||
#include "server/server.h"
|
||||
#include "server/session.h"
|
||||
#include "database/exceptions.h"
|
||||
|
||||
Handler::DeleteTransaction::DeleteTransaction (const std::shared_ptr<Server>& server):
|
||||
Handler("deleteTransaction", Request::Method::post),
|
||||
server(server)
|
||||
{}
|
||||
|
||||
void Handler::DeleteTransaction::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 txnId;
|
||||
try {
|
||||
txnId = 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()->deleteTransaction(txnId, session.owner);
|
||||
if (!success)
|
||||
return error(request, Response::Status::forbidden);
|
||||
|
||||
Response& res = request.createResponse(Response::Status::ok);
|
||||
res.send();
|
||||
|
||||
session.transactionRemoved(txnId);
|
||||
|
||||
} 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);
|
||||
}
|
||||
}
|
21
handler/deletetransaction.h
Normal file
21
handler/deletetransaction.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 DeleteTransaction : public Handler {
|
||||
public:
|
||||
DeleteTransaction (const std::shared_ptr<Server>& server);
|
||||
|
||||
virtual void handle (Request& request) override;
|
||||
|
||||
private:
|
||||
std::weak_ptr<Server> server;
|
||||
};
|
||||
}
|
51
handler/transactions.cpp
Normal file
51
handler/transactions.cpp
Normal file
|
@ -0,0 +1,51 @@
|
|||
//SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
|
||||
//SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include "transactions.h"
|
||||
|
||||
#include "server/server.h"
|
||||
#include "server/session.h"
|
||||
#include "database/exceptions.h"
|
||||
|
||||
Handler::Transactions::Transactions (const std::shared_ptr<Server>& server):
|
||||
Handler("transactions", Request::Method::get),
|
||||
server(server)
|
||||
{}
|
||||
|
||||
void Handler::Transactions::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);
|
||||
|
||||
try {
|
||||
Session& session = srv->getSession(access);
|
||||
std::vector<DB::Transaction> transactions = srv->getDatabase()->listTransactions(session.owner);
|
||||
|
||||
nlohmann::json arr = nlohmann::json::array();
|
||||
for (const DB::Transaction& transaction : transactions)
|
||||
arr.push_back(transaction.toJSON());
|
||||
|
||||
nlohmann::json body = nlohmann::json::object();
|
||||
body["transactions"] = arr;
|
||||
|
||||
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 " << 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/transactions.h
Normal file
20
handler/transactions.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 Transactions : public Handler::Handler {
|
||||
public:
|
||||
Transactions (const std::shared_ptr<Server>& server);
|
||||
void handle (Request& request) override;
|
||||
|
||||
private:
|
||||
std::weak_ptr<Server> server;
|
||||
};
|
||||
}
|
84
handler/updatetransaction.cpp
Normal file
84
handler/updatetransaction.cpp
Normal file
|
@ -0,0 +1,84 @@
|
|||
//SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
|
||||
//SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include "updatetransaction.h"
|
||||
|
||||
#include <map>
|
||||
|
||||
#include "server/server.h"
|
||||
#include "server/session.h"
|
||||
#include "database/exceptions.h"
|
||||
|
||||
Handler::UpdateTransaction::UpdateTransaction (const std::shared_ptr<Server>& server):
|
||||
Handler("updateTransaction", Request::Method::post),
|
||||
server(server)
|
||||
{}
|
||||
|
||||
void Handler::UpdateTransaction::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::Transaction txn;
|
||||
txn.id = std::stoul(itr->second);
|
||||
|
||||
itr = form.find("asset");
|
||||
if (itr == form.end())
|
||||
return error(request, Response::Status::badRequest);
|
||||
|
||||
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;
|
||||
srv->getDatabase()->updateTransaction(txn);
|
||||
|
||||
Response& res = request.createResponse(Response::Status::ok);
|
||||
res.send();
|
||||
|
||||
session.transactionChanged(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/updatetransaction.h
Normal file
20
handler/updatetransaction.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 UpdateTransaction : public Handler {
|
||||
public:
|
||||
UpdateTransaction (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