Rewrote backend in c++
This commit is contained in:
56
backend/src/dto/dto.h
Normal file
56
backend/src/dto/dto.h
Normal file
@@ -0,0 +1,56 @@
|
||||
#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 {
|
||||
GetUsersEntry(int id, bool gitlab, bool tfa, std::string name, db::UserRole role)
|
||||
: id(id), gitlab(gitlab), tfa(tfa), name(std::move(name)), role(role) {}
|
||||
int id;
|
||||
bool gitlab, tfa;
|
||||
std::string name;
|
||||
db::UserRole role;
|
||||
};
|
||||
|
||||
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);
|
||||
drogon::HttpResponsePtr get_node_folder_res(uint64_t id, const std::string& name, const std::shared_ptr<uint64_t>& parent, const std::vector<uint64_t>& children);
|
||||
drogon::HttpResponsePtr get_node_file_res(uint64_t id, const std::string& name, const std::shared_ptr<uint64_t>& parent, uint64_t size);
|
||||
drogon::HttpResponsePtr get_path_res(const std::string& path);
|
||||
drogon::HttpResponsePtr get_new_node_res(uint64_t id);
|
||||
}
|
||||
}
|
||||
|
||||
#endif //BACKEND_DTO_H
|
||||
98
backend/src/dto/responses.cpp
Normal file
98
backend/src/dto/responses.cpp
Normal file
@@ -0,0 +1,98 @@
|
||||
#include "dto.h"
|
||||
|
||||
namespace dto::Responses {
|
||||
drogon::HttpResponsePtr get_error_res(drogon::HttpStatusCode code, const std::string& msg) {
|
||||
Json::Value json;
|
||||
json["statusCode"] = static_cast<int>(code);
|
||||
json["message"] = msg;
|
||||
auto res = drogon::HttpResponse::newHttpJsonResponse(json);
|
||||
res->setStatusCode(code);
|
||||
return res;
|
||||
}
|
||||
|
||||
drogon::HttpResponsePtr get_success_res() {
|
||||
Json::Value json;
|
||||
return get_success_res(json);
|
||||
}
|
||||
|
||||
drogon::HttpResponsePtr get_success_res(Json::Value& json) {
|
||||
json["statusCode"] = 200;
|
||||
auto res = drogon::HttpResponse::newHttpJsonResponse(json);
|
||||
res->setStatusCode(drogon::HttpStatusCode::k200OK);
|
||||
return res;
|
||||
}
|
||||
|
||||
drogon::HttpResponsePtr get_login_res(const std::string &jwt) {
|
||||
Json::Value json;
|
||||
json["jwt"] = jwt;
|
||||
return get_success_res(json);
|
||||
}
|
||||
|
||||
drogon::HttpResponsePtr get_tfa_setup_res(const std::string& secret, const std::string& qrcode) {
|
||||
Json::Value json;
|
||||
json["secret"] = secret;
|
||||
json["qrCode"] = qrcode;
|
||||
return get_success_res(json);
|
||||
}
|
||||
|
||||
drogon::HttpResponsePtr get_user_info_res(const std::string &name, bool gitlab, bool tfa) {
|
||||
Json::Value json;
|
||||
json["name"] = name;
|
||||
json["gitlab"] = gitlab;
|
||||
json["tfaEnabled"] = tfa;
|
||||
return get_success_res(json);
|
||||
}
|
||||
|
||||
drogon::HttpResponsePtr get_admin_users_res(const std::vector<GetUsersEntry>& users) {
|
||||
Json::Value json;
|
||||
for (const GetUsersEntry& user : users) {
|
||||
Json::Value entry;
|
||||
entry["id"] = user.id;
|
||||
entry["gitlab"] = user.gitlab;
|
||||
entry["name"] = user.name;
|
||||
entry["role"] = user.role;
|
||||
entry["tfaEnabled"] = user.tfa;
|
||||
json["users"].append(entry);
|
||||
}
|
||||
return get_success_res(json);
|
||||
}
|
||||
|
||||
drogon::HttpResponsePtr get_root_res(uint64_t root) {
|
||||
Json::Value json;
|
||||
json["rootId"] = root;
|
||||
return get_success_res(json);
|
||||
}
|
||||
|
||||
drogon::HttpResponsePtr get_node_folder_res(uint64_t id, const std::string &name, const std::shared_ptr<uint64_t> &parent, const std::vector<uint64_t> &children) {
|
||||
Json::Value json;
|
||||
json["id"] = id;
|
||||
json["name"] = name;
|
||||
json["isFile"] = false;
|
||||
json["parent"] = (parent != nullptr) ? *parent : Json::Value::nullSingleton();
|
||||
for (uint64_t child : children)
|
||||
json["children"].append(child);
|
||||
return get_success_res(json);
|
||||
}
|
||||
|
||||
drogon::HttpResponsePtr get_node_file_res(uint64_t id, const std::string &name, const std::shared_ptr<uint64_t> &parent, uint64_t size) {
|
||||
Json::Value json;
|
||||
json["id"] = id;
|
||||
json["name"] = name;
|
||||
json["isFile"] = true;
|
||||
json["parent"] = (parent != nullptr) ? *parent : Json::Value::nullSingleton();
|
||||
json["size"] = size;
|
||||
return get_success_res(json);
|
||||
}
|
||||
|
||||
drogon::HttpResponsePtr get_path_res(const std::string& path) {
|
||||
Json::Value json;
|
||||
json["path"] = path;
|
||||
return get_success_res(json);
|
||||
}
|
||||
|
||||
drogon::HttpResponsePtr get_new_node_res(uint64_t id) {
|
||||
Json::Value json;
|
||||
json["id"] = id;
|
||||
return get_success_res(json);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user