2022-08-28 17:37:09 +02:00
|
|
|
#ifndef BACKEND_DTO_H
|
|
|
|
#define BACKEND_DTO_H
|
|
|
|
|
|
|
|
#include <drogon/HttpResponse.h>
|
|
|
|
#include "db/db.h"
|
|
|
|
|
|
|
|
namespace dto {
|
|
|
|
template<typename T>
|
|
|
|
std::optional<T> json_get(const Json::Value& j, const std::string& key) {
|
|
|
|
return j.isMember(key)
|
|
|
|
? std::make_optional(j[key].as<T>())
|
|
|
|
: std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline db::User get_user(const drogon::HttpRequestPtr& req) {
|
|
|
|
return req->attributes()->get<db::User>("user");
|
|
|
|
}
|
|
|
|
|
|
|
|
inline db::Token get_token(const drogon::HttpRequestPtr& req) {
|
|
|
|
return req->attributes()->get<db::Token>("token");
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Responses {
|
|
|
|
struct GetUsersEntry {
|
2022-09-04 00:11:11 +02:00
|
|
|
GetUsersEntry(uint64_t id, bool gitlab, bool tfa, std::string name, db::UserRole role)
|
2022-08-28 17:37:09 +02:00
|
|
|
: id(id), gitlab(gitlab), tfa(tfa), name(std::move(name)), role(role) {}
|
2022-09-04 00:11:11 +02:00
|
|
|
uint64_t id;
|
2022-08-28 17:37:09 +02:00
|
|
|
bool gitlab, tfa;
|
|
|
|
std::string name;
|
|
|
|
db::UserRole role;
|
|
|
|
};
|
|
|
|
|
2022-09-03 23:32:20 +02:00
|
|
|
struct GetNodeEntry {
|
|
|
|
explicit GetNodeEntry(const db::INode& node) : id(node.getValueOfId()), name(node.getValueOfName()), is_file(node.getValueOfIsFile() != 0), has_preview(node.getValueOfHasPreview() != 0), parent(node.getParentId()) {
|
|
|
|
if (node.getValueOfIsFile() != 0) size = node.getValueOfSize();
|
|
|
|
}
|
|
|
|
uint64_t id, size;
|
|
|
|
std::string name;
|
|
|
|
bool is_file, has_preview;
|
|
|
|
std::shared_ptr<uint64_t> parent;
|
|
|
|
};
|
|
|
|
|
2022-08-28 17:37:09 +02:00
|
|
|
drogon::HttpResponsePtr get_error_res(drogon::HttpStatusCode, const std::string &msg);
|
|
|
|
drogon::HttpResponsePtr get_success_res();
|
|
|
|
drogon::HttpResponsePtr get_success_res(Json::Value &);
|
|
|
|
|
|
|
|
inline drogon::HttpResponsePtr get_badreq_res(const std::string &msg) { return get_error_res(drogon::HttpStatusCode::k400BadRequest, msg); }
|
|
|
|
inline drogon::HttpResponsePtr get_unauth_res(const std::string &msg) { return get_error_res(drogon::HttpStatusCode::k401Unauthorized, msg); }
|
|
|
|
inline drogon::HttpResponsePtr get_forbdn_res(const std::string &msg) { return get_error_res(drogon::HttpStatusCode::k403Forbidden, msg); }
|
|
|
|
|
|
|
|
drogon::HttpResponsePtr get_login_res(const std::string &jwt);
|
|
|
|
drogon::HttpResponsePtr get_tfa_setup_res(const std::string& secret, const std::string& qrcode);
|
|
|
|
|
|
|
|
drogon::HttpResponsePtr get_user_info_res(const std::string& name, bool gitlab, bool tfa);
|
|
|
|
|
|
|
|
drogon::HttpResponsePtr get_admin_users_res(const std::vector<GetUsersEntry>& users);
|
|
|
|
|
|
|
|
drogon::HttpResponsePtr get_root_res(uint64_t root);
|
2022-09-03 23:32:20 +02:00
|
|
|
drogon::HttpResponsePtr get_node_res(const GetNodeEntry& node, const std::vector<GetNodeEntry>& children);
|
2022-08-28 17:37:09 +02:00
|
|
|
drogon::HttpResponsePtr get_new_node_res(uint64_t id);
|
2022-09-03 23:32:20 +02:00
|
|
|
drogon::HttpResponsePtr get_node_exists_res(uint64_t id, bool file);
|
|
|
|
drogon::HttpResponsePtr get_download_base64_res(const std::string& data);
|
|
|
|
drogon::HttpResponsePtr get_type_res(const std::string& type);
|
|
|
|
drogon::HttpResponsePtr get_create_zip_done_res();
|
|
|
|
drogon::HttpResponsePtr get_create_zip_done_res(uint64_t progress, uint64_t total);
|
2022-08-28 17:37:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif //BACKEND_DTO_H
|