57 lines
2.5 KiB
C
57 lines
2.5 KiB
C
|
#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
|