Add transaction handler, delete transaction and list transactions queries, session methods

This commit is contained in:
Blue 2024-04-15 19:13:22 -03:00
parent c2d4bf5ccb
commit e1d5b6c76c
Signed by: blue
GPG key ID: 9B203B252A63EE38
9 changed files with 195 additions and 4 deletions

View file

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

View file

@ -144,6 +144,53 @@ void Session::assetRemoved (unsigned int assetId) {
checkUpdates();
}
void Session::transactionAdded(const DB::Transaction& txn) {
std::lock_guard lock(mtx);
std::map<std::string, nlohmann::json>& txns = cache["transactions"];
auto itr = txns.find("changed");
if (itr == txns.end())
itr = txns.emplace("changed", nlohmann::json::array()).first;
removeByID(itr->second, txn.id);
itr->second.push_back(txn.toJSON());
checkUpdates();
}
void Session::transactionChanged(const DB::Transaction& txn) {
std::lock_guard lock(mtx);
std::map<std::string, nlohmann::json>& txns = cache["transactions"];
auto itr = txns.find("changed");
if (itr == txns.end())
itr = txns.emplace("changed", nlohmann::json::array()).first;
removeByID(itr->second, txn.id);
itr->second.push_back(txn.toJSON());
checkUpdates();
}
void Session::transactionRemoved(unsigned int txnId) {
std::lock_guard lock(mtx);
std::map<std::string, nlohmann::json>& txns = cache["transactions"];
auto itr = txns.find("added");
if (itr != txns.end())
removeByID(itr->second, txnId);
else {
itr = txns.find("removed");
if (itr == txns.end())
itr = txns.emplace("removed", nlohmann::json::array()).first;
itr->second.push_back(txnId);
}
itr = txns.find("changed");
if (itr != txns.end())
removeByID(itr->second, txnId);
checkUpdates();
}
void Session::removeByID(nlohmann::json& array, unsigned int id) {
array.erase(
std::remove_if(

View file

@ -13,6 +13,7 @@
#include "taskmanager/scheduler.h"
#include "database/schema/asset.h"
#include "database/schema/transaction.h"
class Session : public Accepting {
public:
@ -41,6 +42,10 @@ public:
void assetChanged (const DB::Asset& asset);
void assetRemoved (unsigned int assetId);
void transactionAdded(const DB::Transaction& txn);
void transactionChanged(const DB::Transaction& txn);
void transactionRemoved(unsigned int txnId);
private:
void onTimeout ();
void sendUpdates (std::unique_ptr<Request> request);