1
0
forked from blue/pica
pica/handler/deleteasset.cpp

60 lines
1.9 KiB
C++
Raw Normal View History

2024-01-21 19:23:48 +00:00
//SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
//SPDX-License-Identifier: GPL-3.0-or-later
#include "deleteasset.h"
#include "server/server.h"
#include "server/session.h"
#include "database/exceptions.h"
Handler::DeleteAsset::DeleteAsset (std::weak_ptr<Server> server):
Handler("deleteAsset", Request::Method::post),
server(server)
{}
void Handler::DeleteAsset::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 assetId;
try {
assetId = 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()->deleteAsset(assetId, session.owner);
if (!success)
return error(request, Response::Status::forbidden);
Response& res = request.createResponse(Response::Status::ok);
res.send();
session.assetRemoved(assetId);
} catch (const DB::NoSession& e) {
return error(request, Response::Status::unauthorized);
} catch (const std::exception& e) {
std::cerr << "Exception on poll:\n\t" << e.what() << std::endl;
return error(request, Response::Status::internalError);
} catch (...) {
std::cerr << "Unknown exception on poll" << std::endl;
return error(request, Response::Status::internalError);
}
}