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: blue
GPG key ID: 9B203B252A63EE38
13 changed files with 172 additions and 6 deletions

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;