2024-01-11 21:33:46 +00:00
|
|
|
//SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
|
|
|
|
//SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
#include "listassets.h"
|
|
|
|
|
|
|
|
#include "server/server.h"
|
|
|
|
#include "server/session.h"
|
|
|
|
#include "database/exceptions.h"
|
|
|
|
|
|
|
|
Handler::ListAssets::ListAssets (std::weak_ptr<Server> server):
|
|
|
|
Handler("listAssets", Request::Method::get),
|
|
|
|
server(server)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void Handler::ListAssets::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);
|
2024-01-12 23:39:41 +00:00
|
|
|
std::vector<DB::Asset> assets = srv->getDatabase()->listAssets(session.owner);
|
2024-01-11 21:33:46 +00:00
|
|
|
|
2024-01-12 23:39:41 +00:00
|
|
|
nlohmann::json arr = nlohmann::json::array();
|
|
|
|
for (const DB::Asset& asset : assets)
|
|
|
|
arr.push_back(asset.toJSON());
|
|
|
|
|
|
|
|
nlohmann::json body = nlohmann::json::object();
|
|
|
|
body["assets"] = arr;
|
|
|
|
|
|
|
|
Response& res = request.createResponse(Response::Status::ok);
|
|
|
|
res.setBody(body);
|
|
|
|
res.send();
|
2024-01-11 21:33:46 +00:00
|
|
|
|
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Handler::ListAssets::error (Request& request, Response::Status status) {
|
|
|
|
Response& res = request.createResponse(status);
|
|
|
|
res.send();
|
|
|
|
}
|