diff --git a/handler/CMakeLists.txt b/handler/CMakeLists.txt index 1d3296b..a660a29 100644 --- a/handler/CMakeLists.txt +++ b/handler/CMakeLists.txt @@ -8,10 +8,10 @@ set(HEADERS register.h login.h poll.h - listassets.h + assets.h addasset.h deleteasset.h - mycurrencies.h + currencies.h ) set(SOURCES @@ -21,10 +21,10 @@ set(SOURCES register.cpp login.cpp poll.cpp - listassets.cpp + assets.cpp addasset.cpp deleteasset.cpp - mycurrencies.cpp + currencies.cpp ) target_sources(${PROJECT_NAME} PRIVATE ${SOURCES}) diff --git a/handler/listassets.cpp b/handler/assets.cpp similarity index 88% rename from handler/listassets.cpp rename to handler/assets.cpp index 21a2825..1c89118 100644 --- a/handler/listassets.cpp +++ b/handler/assets.cpp @@ -1,18 +1,18 @@ //SPDX-FileCopyrightText: 2024 Yury Gubich //SPDX-License-Identifier: GPL-3.0-or-later -#include "listassets.h" +#include "assets.h" #include "server/server.h" #include "server/session.h" #include "database/exceptions.h" -Handler::ListAssets::ListAssets (const std::shared_ptr& server): - Handler("listAssets", Request::Method::get), +Handler::Assets::Assets (const std::shared_ptr& server): + Handler("assets", Request::Method::get), server(server) {} -void Handler::ListAssets::handle (Request& request) { +void Handler::Assets::handle (Request& request) { std::string access = request.getAuthorizationToken(); if (access.empty()) return error(request, Response::Status::unauthorized); diff --git a/handler/assets.h b/handler/assets.h new file mode 100644 index 0000000..a1e19e8 --- /dev/null +++ b/handler/assets.h @@ -0,0 +1,20 @@ +//SPDX-FileCopyrightText: 2024 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later + +#pragma once + +#include + +#include "handler.h" + +class Server; +namespace Handler { +class Assets : public Handler::Handler { +public: + Assets (const std::shared_ptr& server); + void handle (Request& request) override; + +private: + std::weak_ptr server; +}; +} diff --git a/handler/currencies.cpp b/handler/currencies.cpp new file mode 100644 index 0000000..1c43c2a --- /dev/null +++ b/handler/currencies.cpp @@ -0,0 +1,51 @@ +//SPDX-FileCopyrightText: 2024 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later + +#include "currencies.h" + +#include "server/server.h" +#include "server/session.h" +#include "database/exceptions.h" + +Handler::Currencies::Currencies (const std::shared_ptr& server): + Handler("currencies", Request::Method::get), + server(server) +{} + +void Handler::Currencies::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 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) { + 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); + } +} diff --git a/handler/currencies.h b/handler/currencies.h new file mode 100644 index 0000000..69c40c6 --- /dev/null +++ b/handler/currencies.h @@ -0,0 +1,21 @@ +//SPDX-FileCopyrightText: 2024 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later + +#pragma once + +#include + +#include "handler.h" + +class Server; +namespace Handler { +class Currencies : public Handler { +public: + Currencies(const std::shared_ptr& server); + + void handle (Request& request) override; + +private: + std::weak_ptr server; +}; +} diff --git a/handler/listassets.h b/handler/listassets.h index c806112..e69de29 100644 --- a/handler/listassets.h +++ b/handler/listassets.h @@ -1,20 +0,0 @@ -//SPDX-FileCopyrightText: 2024 Yury Gubich -//SPDX-License-Identifier: GPL-3.0-or-later - -#pragma once - -#include - -#include "handler.h" - -class Server; -namespace Handler { -class ListAssets : public Handler::Handler { -public: - ListAssets (const std::shared_ptr& server); - void handle (Request& request) override; - -private: - std::weak_ptr server; -}; -} diff --git a/handler/mycurrencies.cpp b/handler/mycurrencies.cpp index 549eef1..e69de29 100644 --- a/handler/mycurrencies.cpp +++ b/handler/mycurrencies.cpp @@ -1,51 +0,0 @@ -//SPDX-FileCopyrightText: 2024 Yury Gubich -//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): - 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 srv = server.lock(); - if (!srv) - return error(request, Response::Status::internalError); - - try { - Session& session = srv->getSession(access); - std::vector 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) { - 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); - } -} diff --git a/handler/mycurrencies.h b/handler/mycurrencies.h index 021f9db..e69de29 100644 --- a/handler/mycurrencies.h +++ b/handler/mycurrencies.h @@ -1,21 +0,0 @@ -//SPDX-FileCopyrightText: 2024 Yury Gubich -//SPDX-License-Identifier: GPL-3.0-or-later - -#pragma once - -#include - -#include "handler.h" - -class Server; -namespace Handler { -class MyCurrencies : public Handler { -public: - MyCurrencies(const std::shared_ptr& server); - - void handle (Request& request) override; - -private: - std::weak_ptr server; -}; -} diff --git a/run.sh.in b/run.sh.in index be5f016..d6eecee 100644 --- a/run.sh.in +++ b/run.sh.in @@ -17,7 +17,7 @@ start_service() { } if [ ! -d "/run/pica" ]; then - echo "reuired unix socket was not found, going to use sudo to create it" + echo "required unix socket was not found, going to use sudo to create it" sudo mkdir /run/pica sudo chown $USER:$USER /run/pica fi diff --git a/server/server.cpp b/server/server.cpp index 8bb9d1e..5964e5e 100644 --- a/server/server.cpp +++ b/server/server.cpp @@ -12,10 +12,10 @@ #include "handler/register.h" #include "handler/login.h" #include "handler/poll.h" -#include "handler/listassets.h" +#include "handler/assets.h" #include "handler/addasset.h" #include "handler/deleteasset.h" -#include "handler/mycurrencies.h" +#include "handler/currencies.h" #include "taskmanager/route.h" @@ -73,10 +73,10 @@ void Server::run (int socketDescriptor) { router->addRoute(std::make_unique(srv)); router->addRoute(std::make_unique(srv)); router->addRoute(std::make_unique(srv)); - router->addRoute(std::make_unique(srv)); + router->addRoute(std::make_unique(srv)); router->addRoute(std::make_unique(srv)); router->addRoute(std::make_unique(srv)); - router->addRoute(std::make_unique(srv)); + router->addRoute(std::make_unique(srv)); taskManager->start(); scheduler->start();