Small fixes, update asset method
This commit is contained in:
parent
a9f46b2ab0
commit
4914a467e5
13 changed files with 172 additions and 6 deletions
|
@ -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
|
||||
)
|
||||
|
||||
|
|
84
handler/updateasset.cpp
Normal file
84
handler/updateasset.cpp
Normal 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
20
handler/updateasset.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 UpdateAsset : public Handler {
|
||||
public:
|
||||
UpdateAsset (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