generated from Sithas/conan_template
Рабочая сборка второго executor'а
This commit is contained in:
@@ -0,0 +1,138 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <regex>
|
||||||
|
#include <boost/json.hpp>
|
||||||
|
#include <mysqlx/xdevapi.h>
|
||||||
|
#include <mysqlx/common/api.h>
|
||||||
|
#include <boost/uuid.hpp>
|
||||||
|
|
||||||
|
#include "IExecutor.h"
|
||||||
|
#include "../DAO/IUserDAO.h"
|
||||||
|
#include "../DAO/IAuthDAO.h"
|
||||||
|
|
||||||
|
namespace uad
|
||||||
|
{
|
||||||
|
template <class Body, class Allocator, class ResponseType>
|
||||||
|
class AuthLoginExecutor : public IExecutor<Body, Allocator, ResponseType>
|
||||||
|
{
|
||||||
|
mysqlx::Session& session_;
|
||||||
|
std::shared_ptr<IUserDAO> user_dao_;
|
||||||
|
std::shared_ptr<IAuthDAO> auth_dao_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
AuthLoginExecutor(mysqlx::Session& session,
|
||||||
|
std::shared_ptr<IUserDAO> user_dao,
|
||||||
|
std::shared_ptr<IAuthDAO> auth_dao)
|
||||||
|
: session_(session), user_dao_(user_dao), auth_dao_(auth_dao)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
boost::beast::http::response<ResponseType> operator ()(
|
||||||
|
boost::beast::http::request<Body, boost::beast::http::basic_fields<Allocator>>&& req
|
||||||
|
) override
|
||||||
|
{
|
||||||
|
using namespace boost;
|
||||||
|
using namespace boost::json;
|
||||||
|
using namespace boost::beast;
|
||||||
|
using namespace std::string_literals;
|
||||||
|
|
||||||
|
auto body = req.body();
|
||||||
|
value req_json;
|
||||||
|
value response_body;
|
||||||
|
|
||||||
|
response_body.emplace_object();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
req_json = json::parse(body);
|
||||||
|
}
|
||||||
|
catch (const system::system_error& err)
|
||||||
|
{
|
||||||
|
http::response<ResponseType> res{http::status::bad_request, req.version()};
|
||||||
|
response_body.as_object().emplace("Result", "cannot deserialize json");
|
||||||
|
|
||||||
|
res.body() = serialize(response_body);
|
||||||
|
res.set(http::field::content_type, "application/json");
|
||||||
|
res.content_length(res.body().size());
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string login = req_json.as_object().at("login").as_string().c_str();
|
||||||
|
std::string password = req_json.as_object().at("password").as_string().c_str();
|
||||||
|
|
||||||
|
if (!ValidateLogin(login) || !ValidatePassword(password))
|
||||||
|
{
|
||||||
|
http::response<ResponseType> res{http::status::unprocessable_entity, req.version()};
|
||||||
|
|
||||||
|
response_body.as_object().emplace(
|
||||||
|
"Result",
|
||||||
|
"Validations failed. Login should have length from 3 to 50. Password from 5 characters length."
|
||||||
|
);
|
||||||
|
|
||||||
|
res.body() = serialize(response_body);
|
||||||
|
res.set(http::field::content_type, "application/json");
|
||||||
|
res.content_length(res.body().size());
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (user_dao_->GetByLogin(login).has_value())
|
||||||
|
{
|
||||||
|
http::response<ResponseType> res{http::status::conflict, req.version()};
|
||||||
|
|
||||||
|
response_body.as_object().emplace(
|
||||||
|
"Result",
|
||||||
|
"user with login "s + login + " exists"s
|
||||||
|
);
|
||||||
|
|
||||||
|
res.body() = serialize(response_body);
|
||||||
|
res.set(http::field::content_type, "application/json");
|
||||||
|
res.content_length(res.body().size());
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
User user;
|
||||||
|
|
||||||
|
user.SetLogin(login);
|
||||||
|
user.SetPassword(password);
|
||||||
|
|
||||||
|
const auto uuid_stringified = user_dao_->Create(user);
|
||||||
|
|
||||||
|
http::response<ResponseType> res{
|
||||||
|
http::status::created, req.version()
|
||||||
|
};
|
||||||
|
|
||||||
|
response_body.as_object().emplace(
|
||||||
|
"uuid",
|
||||||
|
uuid_stringified
|
||||||
|
);
|
||||||
|
response_body.as_object().emplace(
|
||||||
|
"login",
|
||||||
|
user.GetLogin()
|
||||||
|
);
|
||||||
|
|
||||||
|
res.body() = serialize(response_body);
|
||||||
|
res.set(http::field::content_type, "application/json");
|
||||||
|
res.content_length(res.body().size());
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool ValidateLogin(const std::string& login)
|
||||||
|
{
|
||||||
|
if (login.size() < 3 || login.size() > 50) return false;
|
||||||
|
|
||||||
|
std::regex pattern(std::string("^[A-Za-z0-9_]+$"));
|
||||||
|
|
||||||
|
return std::regex_match(login, pattern);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ValidatePassword(const std::string& password)
|
||||||
|
{
|
||||||
|
return password.size() >= 5;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@
|
|||||||
#include "../DAO/IUserDAO.h"
|
#include "../DAO/IUserDAO.h"
|
||||||
#include "AuthRegistrationExecutor.h"
|
#include "AuthRegistrationExecutor.h"
|
||||||
#include "RootExecutor.h"
|
#include "RootExecutor.h"
|
||||||
|
#include "../DAO/MemoryAuthDAO.h"
|
||||||
#include "../DAO/MySQLUserDAO.h"
|
#include "../DAO/MySQLUserDAO.h"
|
||||||
|
|
||||||
namespace uad
|
namespace uad
|
||||||
@@ -17,7 +18,9 @@ void HandleRequest(
|
|||||||
Send&& send)
|
Send&& send)
|
||||||
{
|
{
|
||||||
static RootExecutor<Body, Allocator, boost::beast::http::string_body, Send> root_executor(
|
static RootExecutor<Body, Allocator, boost::beast::http::string_body, Send> root_executor(
|
||||||
GetMySqlSession(), std::make_shared<MySQLUserDAO>(GetMySqlSession())
|
GetMySqlSession(),
|
||||||
|
std::make_shared<MySQLUserDAO>(GetMySqlSession()),
|
||||||
|
std::make_shared<MemoryAuthDAO>(GetMySqlSession())
|
||||||
);
|
);
|
||||||
|
|
||||||
root_executor(doc_root, std::move(req), std::forward<Send>(send));
|
root_executor(doc_root, std::move(req), std::forward<Send>(send));
|
||||||
|
|||||||
@@ -2,7 +2,9 @@
|
|||||||
#include "IController.h"
|
#include "IController.h"
|
||||||
#include "Controller.h"
|
#include "Controller.h"
|
||||||
#include "AuthRegistrationExecutor.h"
|
#include "AuthRegistrationExecutor.h"
|
||||||
|
#include "AuthLoginExecutor.h"
|
||||||
#include "../DAO/IUserDAO.h"
|
#include "../DAO/IUserDAO.h"
|
||||||
|
#include "../DAO/IAuthDAO.h"
|
||||||
#include "./../helpers/helpers.h"
|
#include "./../helpers/helpers.h"
|
||||||
|
|
||||||
namespace uad
|
namespace uad
|
||||||
@@ -13,6 +15,8 @@ class RootExecutor
|
|||||||
using IRouteExecutor = IExecutor<Body, Allocator, boost::beast::http::string_body>;
|
using IRouteExecutor = IExecutor<Body, Allocator, boost::beast::http::string_body>;
|
||||||
using RouteAuthRegistrationExecutor = AuthRegistrationExecutor<
|
using RouteAuthRegistrationExecutor = AuthRegistrationExecutor<
|
||||||
Body, Allocator, boost::beast::http::string_body>;
|
Body, Allocator, boost::beast::http::string_body>;
|
||||||
|
using RouteAuthLoginExecutor = AuthLoginExecutor<
|
||||||
|
Body, Allocator, boost::beast::http::string_body>;
|
||||||
using IRouteController = IController<Body, Allocator, boost::beast::http::string_body>;
|
using IRouteController = IController<Body, Allocator, boost::beast::http::string_body>;
|
||||||
using RouteController = Controller<Body, Allocator, boost::beast::http::string_body>;
|
using RouteController = Controller<Body, Allocator, boost::beast::http::string_body>;
|
||||||
using RoutesPathes = std::unordered_map<std::string, std::unique_ptr<IRouteController>>;
|
using RoutesPathes = std::unordered_map<std::string, std::unique_ptr<IRouteController>>;
|
||||||
@@ -25,14 +29,29 @@ private:
|
|||||||
RoutesPathes routes_pathes_;
|
RoutesPathes routes_pathes_;
|
||||||
mysqlx::Session& session_;
|
mysqlx::Session& session_;
|
||||||
std::shared_ptr<IUserDAO> user_dao_;
|
std::shared_ptr<IUserDAO> user_dao_;
|
||||||
|
std::shared_ptr<IAuthDAO> auth_dao_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
RootExecutor(mysqlx::Session& session, std::shared_ptr<IUserDAO> user_dao)
|
RootExecutor(
|
||||||
: session_(session), user_dao_(user_dao)
|
mysqlx::Session& session,
|
||||||
|
std::shared_ptr<IUserDAO> user_dao,
|
||||||
|
std::shared_ptr<IAuthDAO> auth_dao) :
|
||||||
|
session_(session), user_dao_(user_dao), auth_dao_(auth_dao)
|
||||||
|
|
||||||
{
|
{
|
||||||
routes_pathes_["/api/v1/Auth/Register"] = std::make_unique<RouteController>(
|
routes_pathes_["/api/v1/Auth/Register"] = std::make_unique<RouteController>(
|
||||||
typename RouteController::HTTPMethodsToExecutors{
|
typename RouteController::HTTPMethodsToExecutors{
|
||||||
{boost::beast::http::verb::post, std::make_shared<RouteAuthRegistrationExecutor>(session_, user_dao_)}
|
{boost::beast::http::verb::post, std::make_shared<RouteAuthRegistrationExecutor>(
|
||||||
|
session_,
|
||||||
|
user_dao_
|
||||||
|
)}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
routes_pathes_["/api/v1/Auth/Login"] = std::make_unique<RouteController>(
|
||||||
|
typename RouteController::HTTPMethodsToExecutors{
|
||||||
|
{boost::beast::http::verb::post,
|
||||||
|
std::make_shared<RouteAuthLoginExecutor>(session_, user_dao_, auth_dao_)}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user