//SPDX-FileCopyrightText: 2024 Yury Gubich //SPDX-License-Identifier: GPL-3.0-or-later #include "transactions.h" #include "server/server.h" #include "server/session.h" #include "database/exceptions.h" Handler::Transactions::Transactions (const std::shared_ptr& server): Handler("transactions", Request::Method::get), server(server) {} void Handler::Transactions::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 srv = server.lock(); if (!srv) return error(request, Response::Status::internalError); try { Session& session = srv->getSession(access); std::vector transactions = srv->getDatabase()->listTransactions(session.owner); nlohmann::json arr = nlohmann::json::array(); for (const DB::Transaction& transaction : transactions) arr.push_back(transaction.toJSON()); nlohmann::json body = nlohmann::json::object(); body["transactions"] = 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) { 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); } }