diff --git a/client/internal/CMakeLists.txt b/client/internal/CMakeLists.txt index f76cc95852d6c9979711c7956fd08f908d82c92a..790ec143130bef8ba27e526a4f50cdc9e8907cb4 100644 --- a/client/internal/CMakeLists.txt +++ b/client/internal/CMakeLists.txt @@ -1,6 +1,7 @@ add_subdirectory(entities) add_subdirectory(httpClient) add_subdirectory(gui) +add_subdirectory(core) set(libClientEntities_LIB ${libClientEntities_LIB} PARENT_SCOPE) set(libClientEntities_INCLUDE_DIRS ${libClientEntities_INCLUDE_DIRS} PARENT_SCOPE) @@ -9,4 +10,7 @@ set(HTTPCLIENT_lib_LIB ${HTTPCLIENT_lib_LIB} PARENT_SCOPE) set(HTTPCLIENT_lib_INCLUDE_DIRS ${HTTPCLIENT_lib_INCLUDE_DIRS} PARENT_SCOPE) set(GUI_lib_LIB ${GUI_lib_LIB} PARENT_SCOPE) -set(GUI_lib_INCLUDE_DIRS ${GUI_lib_INCLUDE_DIRS} PARENT_SCOPE) \ No newline at end of file +set(GUI_lib_INCLUDE_DIRS ${GUI_lib_INCLUDE_DIRS} PARENT_SCOPE) + +set(CORE_lib_LIB ${CORE_lib_LIB} PARENT_SCOPE) +set(CORE_lib_INCLUDE_DIRS ${CORE_lib_INCLUDE_DIRS} PARENT_SCOPE) \ No newline at end of file diff --git a/client/internal/core/CMakeLists.txt b/client/internal/core/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..8a2af6623c90b54e3a1719fd9206b2ebd723306c --- /dev/null +++ b/client/internal/core/CMakeLists.txt @@ -0,0 +1,18 @@ +project("CoreLib") + +set(CMAKE_CXX_STANDARD 20) + +file(GLOB SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp) +file(GLOB INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/include) +file(GLOB INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}/include/*.h) + +include_directories(${INCLUDE_DIRS} ${libClientEntities_INCLUDE_DIRS} ${HTTPCLIENT_lib_INCLUDE_DIRS}) + +add_library(${PROJECT_NAME} ${SOURCES} ${INCLUDES}) +target_link_libraries(${PROJECT_NAME} ${libClientEntities_LIB} ${HTTPCLIENT_lib_LIB}) + +set(CORE_lib_LIB ${PROJECT_NAME}) +set(CORE_lib_LIB ${CORE_lib_LIB} PARENT_SCOPE) + +set(CORE_lib_INCLUDE_DIRS ${INCLUDE_DIRS}) +set(CORE_lib_INCLUDE_DIRS ${CORE_lib_INCLUDE_DIRS} PARENT_SCOPE) \ No newline at end of file diff --git a/client/internal/core/include/Core.h b/client/internal/core/include/Core.h new file mode 100644 index 0000000000000000000000000000000000000000..d88af4de561aba58bb1c97077e1ae5bd0ff44010 --- /dev/null +++ b/client/internal/core/include/Core.h @@ -0,0 +1,20 @@ +#pragma once + +#include +#include + +#include "Task.h" + +class Core { +public: + Core() { user_id = -1; }; + + static unsigned signUp(const std::string &login, const std::string &username, const std::string &pass); + + static unsigned login(const std::string &login, const std::string &pass); + + static void logout(); + +private: + static std::size_t user_id; +}; diff --git a/client/internal/core/src/Core.cpp b/client/internal/core/src/Core.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bf3d43dbd11bf0eb2b29af5fa71cb8bff771483b --- /dev/null +++ b/client/internal/core/src/Core.cpp @@ -0,0 +1,26 @@ +// +// Created by Николай Степанов on 11.05.2023. +// +#include "Core.h" +#include "HttpClientManager.h" + +const std::string CLIENT_IP = "0.0.0.0"; +const std::string CLIENT_PORT = "80"; +HttpClientManager client(CLIENT_IP, CLIENT_PORT); + +unsigned Core::signUp(const std::string &login, const std::string &username, const std::string &pass) { + auto res = client.registerUser(login, username, pass); + user_id = res.second.id; + return res.first; +} + +unsigned Core::login(const std::string &login, const std::string &pass) { + auto res = client.loginUser(login, pass); + user_id = res.second.id; + return res.first; +} + +void Core::logout() { + user_id = -1; +} + diff --git a/client/internal/entities/include/Metric.h b/client/internal/entities/include/Metric.h index 3ab8ea715cfd8be2f766a56b001ce23887c1252a..db320cf9732f1a3b1031e4ff370f717ae99f4ff0 100644 --- a/client/internal/entities/include/Metric.h +++ b/client/internal/entities/include/Metric.h @@ -1,5 +1,4 @@ -#ifndef SOURCEDOUT_METRIC_H -#define SOURCEDOUT_METRIC_H +#pragma once #include @@ -7,5 +6,3 @@ struct Metric { std::string name; unsigned int value; }; - -#endif //SOURCEDOUT_METRIC_H diff --git a/client/internal/entities/include/Solution.h b/client/internal/entities/include/Solution.h index 6282f4720c15e6c10825ce75310509585bac04f6..384e648c4b1c3647776396c1a1da467758839748 100644 --- a/client/internal/entities/include/Solution.h +++ b/client/internal/entities/include/Solution.h @@ -1,58 +1,10 @@ -#ifndef SOURCEDOUT_SOLUTION_HPP -#define SOURCEDOUT_SOLUTION_HPP +#pragma once #include #include #include -class Solution { -public: - Solution() =default; - Solution(size_t id, std::string sendDate, size_t senderId, std::string source, - std::string tokens, std::string astTree, size_t taskId, std::string result); - - [[nodiscard]] size_t getId() const; - - - [[nodiscard]] const std::string &getSendDate() const; - - void setSendDate(const std::string &sendDate); - - [[nodiscard]] size_t getSenderId() const; - - void setSenderId(size_t senderId); - - [[nodiscard]] const std::string &getSource() const; - - void setSource(const std::string &source); - - [[nodiscard]] const std::string &getTokens() const; - - void setTokens(const std::string &tokens); - - [[nodiscard]] const std::string &getAstTree() const; - - void setAstTree(const std::string &astTree); - - [[nodiscard]] size_t getTaskId() const; - - void setTaskId(size_t taskId); - - [[nodiscard]] const std::string &getResult() const; - - void setResult(const std::string &result); - -private: +struct Solution { size_t id; - std::string send_date; - size_t sender_id; std::string source; - std::string tokens; - std::string astTree; - size_t task_id; - std::string result; -public: - -}; - -#endif //SOURCEDOUT_SOLUTION_HPP \ No newline at end of file +}; \ No newline at end of file diff --git a/client/internal/entities/include/Task.h b/client/internal/entities/include/Task.h index 9d87ca6b44176625da12296a15fcae656aae4dd1..de9c48848fffdc53ce7675dcf5112077f6ecffe9 100644 --- a/client/internal/entities/include/Task.h +++ b/client/internal/entities/include/Task.h @@ -1,11 +1,10 @@ -#ifndef SOURCEDOUT_TASK_HPP -#define SOURCEDOUT_TASK_HPP +#pragma once #include struct Task{ std::size_t id; std::string description; -}; -#endif //SOURCEDOUT_TASK_HPP \ No newline at end of file + Task(std::size_t id, std::string_view desc); +}; diff --git a/client/internal/entities/include/User.h b/client/internal/entities/include/User.h index 5802df2c3c5291cc7e0b697c71e7e3efdf005b94..94825402abfefa1f9518fd38f9f35389aef7eae7 100644 --- a/client/internal/entities/include/User.h +++ b/client/internal/entities/include/User.h @@ -1,38 +1,11 @@ -#ifndef SOURCEDOUT_USER_HPP +#pragma once #include #include -#define SOURCEDOUT_USER_HPP - -class User { -private: +struct User { size_t id; std::string login; std::string password; std::string username; - -public: - User()=default; - User(size_t id_, std::string login_, std::string password_, std::string username_); - - User(std::string login_, std::string password_, std::string username_); - - [[nodiscard]] const std::string &getLogin() const; - - void setLogin(const std::string &login); - - [[nodiscard]] const std::string &getPassword() const; - - void setPassword(const std::string &password); - - [[nodiscard]] const std::string &getUsername() const; - - void setUsername(const std::string &username); - - [[nodiscard]] size_t getId() const; - - friend std::ostream &operator<<(std::ostream &os, const User &user); -}; - -#endif //SOURCEDOUT_USER_HPP \ No newline at end of file +}; \ No newline at end of file diff --git a/client/internal/entities/src/Solution.cpp b/client/internal/entities/src/Solution.cpp deleted file mode 100644 index 2e9b1e8d60ddb9bb95972ad24d66519cda5b3a92..0000000000000000000000000000000000000000 --- a/client/internal/entities/src/Solution.cpp +++ /dev/null @@ -1,73 +0,0 @@ - -#include -#include -#include "Solution.h" - -Solution::Solution(unsigned long id, std::string sendDate, unsigned long senderId, - std::string source, std::string tokens, - std::string astTree, unsigned long taskId, - std::string result) : id(id), send_date(std::move(sendDate)), sender_id(senderId), - source(std::move(source)), tokens(std::move(tokens)), - astTree(std::move(astTree)), - task_id(taskId), result(std::move(result)) {} - -size_t Solution::getId() const { - return id; -} - - -const std::string &Solution::getSendDate() const { - return send_date; -} - -void Solution::setSendDate(const std::string &sendDate) { - send_date = sendDate; -} - -size_t Solution::getSenderId() const { - return sender_id; -} - -void Solution::setSenderId(size_t senderId) { - sender_id = senderId; -} - -const std::string &Solution::getSource() const { - return source; -} - -void Solution::setSource(const std::string &source_) { - Solution::source = source_; -} - -const std::string &Solution::getTokens() const { - return tokens; -} - -void Solution::setTokens(const std::string &tokens_) { - Solution::tokens = tokens_; -} - -const std::string &Solution::getAstTree() const { - return astTree; -} - -void Solution::setAstTree(const std::string &astTree_) { - Solution::astTree = astTree_; -} - -size_t Solution::getTaskId() const { - return task_id; -} - -void Solution::setTaskId(size_t taskId) { - task_id = taskId; -} - -const std::string &Solution::getResult() const { - return result; -} - -void Solution::setResult(const std::string &result_) { - Solution::result = result_; -} \ No newline at end of file diff --git a/client/internal/entities/src/Task.cpp b/client/internal/entities/src/Task.cpp new file mode 100644 index 0000000000000000000000000000000000000000..496fddceead02a74be19079d28d93bef9641d1e2 --- /dev/null +++ b/client/internal/entities/src/Task.cpp @@ -0,0 +1,3 @@ +#include "Task.h" + +Task::Task(std::size_t id, std::string_view desc) : id(id), description(desc); \ No newline at end of file diff --git a/client/internal/entities/src/User.cpp b/client/internal/entities/src/User.cpp deleted file mode 100644 index 53706e252495f37340c0f1b8443c2126f8eb87af..0000000000000000000000000000000000000000 --- a/client/internal/entities/src/User.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include -#include "User.h" - -User::User(size_t id_, std::string login_, std::string password_, std::string username_) : - id(id_), login(std::move(login_)), password(std::move(password_)), username(std::move(username_)) { -} -User::User(std::string login_, std::string password_, std::string username_) : - id(0), login(std::move(login_)), password(std::move(password_)), username(std::move(username_)) { -} - -const std::string &User::getLogin() const { - return login; -} - -void User::setLogin(const std::string &login_) { - User::login = login_; -} - -const std::string &User::getPassword() const { - return password; -} - -void User::setPassword(const std::string &password_) { - User::password = password_; -} - -const std::string &User::getUsername() const { - return username; -} - -void User::setUsername(const std::string &username_) { - User::username = username_; -} - -size_t User::getId() const { - return id; -} - -std::ostream &operator<<(std::ostream &os, const User &user) { - os << "id: " << user.id << " login: " << user.login << " password: " << user.password << " username: " - << user.username; - return os; -} \ No newline at end of file diff --git a/client/internal/httpClient/include/HttpClientManager.h b/client/internal/httpClient/include/HttpClientManager.h index 7c3ed331ffc822240414d9151f253b549b74e90d..eb93fcb01ee452bd0db18918760ab8f4fd7cbc51 100644 --- a/client/internal/httpClient/include/HttpClientManager.h +++ b/client/internal/httpClient/include/HttpClientManager.h @@ -14,17 +14,17 @@ class HttpClientManager { public: - HttpClientManager(std::string_view host_, std::string_view port_, std::string_view saved_path_); + HttpClientManager(std::string_view host_, std::string_view port_); - unsigned int loginUser(const std::string &login, const std::string &password); - unsigned int registerUser(const std::string &login, const std::string &username, const std::string &password); + std::pair loginUser(const std::string &login, const std::string &password); + std::pair registerUser(const std::string &login, const std::string &username, + const std::string &password); unsigned int submitSolution(const int& user_id, const std::string &path_to_solution); unsigned int getAllSolutionsForTask(const int& user_id, const int& task_id); std::vector getAllTasks(); std::vector getMetrics(const int& sol_id); void setHttpClient(std::shared_ptr client_); private: - std::string saved_path; std::string host; std::string port; std::shared_ptr client; diff --git a/client/internal/httpClient/include/Serializer.h b/client/internal/httpClient/include/Serializer.h index 4f4dc3206297456d77c351cae15fff724fac655c..42be6c840fb20f92e06a50248c737a9fef90f3c6 100644 --- a/client/internal/httpClient/include/Serializer.h +++ b/client/internal/httpClient/include/Serializer.h @@ -1,12 +1,14 @@ -#ifndef APP_HTTPCLIENT_HTTPCLIENT_SERIALIZER_H_ -#define APP_HTTPCLIENT_HTTPCLIENT_SERIALIZER_H_ +#pragma once #include #include #include +#include "User.h" -class Serializer {}; +class Serializer { +public: + std::string serialLoginData(std::string_view login, std::string_view password); + std::string serialRegisterData(std::string_view login, std::string_view username, std::string_view password); + User deserialUserData(std::string_view body); +}; - - -#endif // APP_HTTPCLIENT_HTTPCLIENT_SERIALIZER_H_ diff --git a/client/internal/httpClient/src/HttpClientManager.cpp b/client/internal/httpClient/src/HttpClientManager.cpp index 9d7b1e4243d37ab0ad408f9d48613ab0edcbf7ec..a269f65a6a2ce3c831859d8c4c7d0fbc80980348 100644 --- a/client/internal/httpClient/src/HttpClientManager.cpp +++ b/client/internal/httpClient/src/HttpClientManager.cpp @@ -6,18 +6,36 @@ #include "HttpClient.h" -HttpClientManager::HttpClientManager(std::string_view host_, std::string_view port_, std::string_view saved_path_) : - host(host_), port(port_), saved_path(saved_path_), +HttpClientManager::HttpClientManager(std::string_view host_, std::string_view port_) : + host(host_), port(port_), client(std::make_shared(host_, port_)), serializer(std::make_shared()) {} -unsigned int HttpClientManager::loginUser(const std::string &login, const std::string &password) { - +std::pair HttpClientManager::loginUser(const std::string &login, const std::string &password) { + std::string body = serializer->serialLoginData(login, password); + http::response res = client->makeGetRequest("/user/login", body); + unsigned status = res.result_int(); + std::string res_body; + for (auto seq : res.body().data()) { + auto* cbuf = boost::asio::buffer_cast(seq); + res_body.append(cbuf, boost::asio::buffer_size(seq)); + } + User user = serializer->deserialUserData(res_body); + return {status, user}; } -unsigned int HttpClientManager::registerUser(const std::string &login, const std::string &username, +std::pair HttpClientManager::registerUser(const std::string &login, const std::string &username, const std::string &password) { - return 0; + std::string body = serializer->serialRegisterData(login, username, password); + http::response res = client->makeGetRequest("/user/register", body); + unsigned status = res.result_int(); + std::string res_body; + for (auto seq : res.body().data()) { + auto* cbuf = boost::asio::buffer_cast(seq); + res_body.append(cbuf, boost::asio::buffer_size(seq)); + } + User user = serializer->deserialUserData(res_body); + return {status, user}; } unsigned int HttpClientManager::submitSolution(const int &user_id, const std::string &path_to_sound) { diff --git a/client/internal/httpClient/src/Serializer.cpp b/client/internal/httpClient/src/Serializer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c2d556799fb6933f1b49735e6905b70700c9d641 --- /dev/null +++ b/client/internal/httpClient/src/Serializer.cpp @@ -0,0 +1,39 @@ +#include "Serializer.h" + +#include +#include +#include + +std::string Serializer::serialLoginData(std::string_view login, std::string_view password) { + boost::property_tree::ptree json; + json.put("login", login); + json.put("password", password); + std::stringstream out; + boost::property_tree::write_json(out, json); + return out.str(); +} + +User Serializer::deserialUserData(std::string_view body) { + std::stringstream ss; + ss << body; + boost::property_tree::ptree json; + boost::property_tree::read_json(ss, json); + User res = { + json.get("user_id"), + json.get("login"), + json.get("password"), + json.get("username") + }; + return res; +} + +std::string +Serializer::serialRegisterData(std::string_view login, std::string_view username, std::string_view password) { + boost::property_tree::ptree json; + json.put("login", login); + json.put("username", username); + json.put("password", password); + std::stringstream out; + boost::property_tree::write_json(out, json); + return out.str(); +}