1
0
forked from blue/pica

renamed methods

This commit is contained in:
Blue 2024-03-29 18:57:45 -03:00
parent 07003c2fe6
commit a9f46b2ab0
Signed by untrusted user: blue
GPG Key ID: 9B203B252A63EE38
10 changed files with 105 additions and 105 deletions

View File

@ -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})

View File

@ -1,18 +1,18 @@
//SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
//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>& server):
Handler("listAssets", Request::Method::get),
Handler::Assets::Assets (const std::shared_ptr<Server>& 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);

20
handler/assets.h Normal file
View File

@ -0,0 +1,20 @@
//SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
//SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <memory>
#include "handler.h"
class Server;
namespace Handler {
class Assets : public Handler::Handler {
public:
Assets (const std::shared_ptr<Server>& server);
void handle (Request& request) override;
private:
std::weak_ptr<Server> server;
};
}

51
handler/currencies.cpp Normal file
View File

@ -0,0 +1,51 @@
//SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
//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>& 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<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) {
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);
}
}

21
handler/currencies.h Normal file
View File

@ -0,0 +1,21 @@
//SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
//SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <memory>
#include "handler.h"
class Server;
namespace Handler {
class Currencies : public Handler {
public:
Currencies(const std::shared_ptr<Server>& server);
void handle (Request& request) override;
private:
std::weak_ptr<Server> server;
};
}

View File

@ -1,20 +0,0 @@
//SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
//SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <memory>
#include "handler.h"
class Server;
namespace Handler {
class ListAssets : public Handler::Handler {
public:
ListAssets (const std::shared_ptr<Server>& server);
void handle (Request& request) override;
private:
std::weak_ptr<Server> server;
};
}

View File

@ -1,51 +0,0 @@
//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) {
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);
}
}

View File

@ -1,21 +0,0 @@
//SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
//SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <memory>
#include "handler.h"
class Server;
namespace Handler {
class MyCurrencies : public Handler {
public:
MyCurrencies(const std::shared_ptr<Server>& server);
void handle (Request& request) override;
private:
std::weak_ptr<Server> server;
};
}

View File

@ -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

View File

@ -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<Handler::Register>(srv));
router->addRoute(std::make_unique<Handler::Login>(srv));
router->addRoute(std::make_unique<Handler::Poll>(srv));
router->addRoute(std::make_unique<Handler::ListAssets>(srv));
router->addRoute(std::make_unique<Handler::Assets>(srv));
router->addRoute(std::make_unique<Handler::AddAsset>(srv));
router->addRoute(std::make_unique<Handler::DeleteAsset>(srv));
router->addRoute(std::make_unique<Handler::MyCurrencies>(srv));
router->addRoute(std::make_unique<Handler::Currencies>(srv));
taskManager->start();
scheduler->start();