2024-01-22 18:21:55 +00:00
|
|
|
//SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
|
|
|
|
//SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
#include "mycurrencies.h"
|
|
|
|
|
|
|
|
#include "server/server.h"
|
|
|
|
#include "server/session.h"
|
|
|
|
#include "database/exceptions.h"
|
|
|
|
|
|
|
|
Handler::MyCurrencies::MyCurrencies (const std::shared_ptr<Server>& server):
|
|
|
|
Handler("myCurrencies", Request::Method::get),
|
|
|
|
server(server)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void Handler::MyCurrencies::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);
|
|
|
|
std::vector<DB::Currency> cur = srv->getDatabase()->listUsedCurrencies(session.owner);
|
|
|
|
|
|
|
|
nlohmann::json arr = nlohmann::json::array();
|
|
|
|
for (const DB::Currency& c : cur)
|
|
|
|
arr.push_back(c.toJSON());
|
|
|
|
|
|
|
|
nlohmann::json body = nlohmann::json::object();
|
|
|
|
body["currencies"] = arr;
|
|
|
|
|
|
|
|
Response& res = request.createResponse(Response::Status::ok);
|
|
|
|
res.setBody(body);
|
|
|
|
res.send();
|
|
|
|
|
|
|
|
} catch (const DB::NoSession& e) {
|
|
|
|
return error(request, Response::Status::unauthorized);
|
|
|
|
} catch (const std::exception& e) {
|
2024-03-28 23:20:21 +00:00
|
|
|
std::cerr << "Exception on " << path << ":\n\t" << e.what() << std::endl;
|
2024-01-22 18:21:55 +00:00
|
|
|
return error(request, Response::Status::internalError);
|
|
|
|
} catch (...) {
|
2024-03-28 23:20:21 +00:00
|
|
|
std::cerr << "Unknown exception on " << path << std::endl;
|
2024-01-22 18:21:55 +00:00
|
|
|
return error(request, Response::Status::internalError);
|
|
|
|
}
|
|
|
|
}
|