schema directory for all datastructures of database, add-list assets requests, not tested
This commit is contained in:
parent
d33ec5def8
commit
4df8d4319e
15 changed files with 306 additions and 62 deletions
|
@ -9,6 +9,7 @@ set(HEADERS
|
|||
login.h
|
||||
poll.h
|
||||
listassets.h
|
||||
addasset.h
|
||||
)
|
||||
|
||||
set(SOURCES
|
||||
|
@ -19,6 +20,7 @@ set(SOURCES
|
|||
login.cpp
|
||||
poll.cpp
|
||||
listassets.cpp
|
||||
addasset.cpp
|
||||
)
|
||||
|
||||
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})
|
||||
|
|
77
handler/addasset.cpp
Normal file
77
handler/addasset.cpp
Normal file
|
@ -0,0 +1,77 @@
|
|||
//SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
|
||||
//SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include "addasset.h"
|
||||
|
||||
#include <map>
|
||||
|
||||
#include "server/server.h"
|
||||
#include "server/session.h"
|
||||
#include "database/exceptions.h"
|
||||
|
||||
Handler::AddAsset::AddAsset (std::weak_ptr<Server> server):
|
||||
Handler("addAsset", Request::Method::post),
|
||||
server(server)
|
||||
{}
|
||||
|
||||
void Handler::AddAsset::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);
|
||||
|
||||
std::map form = request.getForm();
|
||||
std::map<std::string, std::string>::const_iterator itr = form.find("currency");
|
||||
if (itr == form.end())
|
||||
return error(request, Response::Status::badRequest);
|
||||
|
||||
DB::Asset asset;
|
||||
asset.currency = std::stoi(itr->second);
|
||||
//TODO validate the currency
|
||||
|
||||
itr = form.find("title");
|
||||
if (itr == form.end())
|
||||
return error(request, Response::Status::badRequest);
|
||||
|
||||
asset.title = itr->second;
|
||||
|
||||
itr = form.find("icon");
|
||||
if (itr == form.end())
|
||||
return error(request, Response::Status::badRequest);
|
||||
|
||||
asset.icon = itr->second;
|
||||
|
||||
try {
|
||||
Session& session = srv->getSession(access);
|
||||
|
||||
asset.owner = session.owner;
|
||||
asset = srv->getDatabase()->addAsset(asset);
|
||||
|
||||
nlohmann::json body = nlohmann::json::object();
|
||||
body["asset"] = asset.toJSON();
|
||||
|
||||
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 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::AddAsset::error (Request& request, Response::Status status) {
|
||||
Response& res = request.createResponse(status);
|
||||
res.send();
|
||||
}
|
21
handler/addasset.h
Normal file
21
handler/addasset.h
Normal 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 AddAsset : public Handler {
|
||||
public:
|
||||
AddAsset (std::weak_ptr<Server> server);
|
||||
virtual void handle (Request& request) override;
|
||||
|
||||
static void error (Request& request, Response::Status status);
|
||||
private:
|
||||
std::weak_ptr<Server> server;
|
||||
};
|
||||
}
|
|
@ -26,7 +26,18 @@ void Handler::ListAssets::handle (Request& request) {
|
|||
|
||||
try {
|
||||
Session& session = srv->getSession(access);
|
||||
std::vector<DB::Asset> assets = srv->getDatabase()->listAssets(session.owner);
|
||||
|
||||
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();
|
||||
|
||||
} catch (const DB::NoSession& e) {
|
||||
return error(request, Response::Status::unauthorized);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue