From fe721d8d64208d0db2d82cfe4c9e3c95fae51020 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B9=20=D0=A1=D1=82?= =?UTF-8?q?=D0=B5=D0=BF=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Fri, 19 May 2023 11:32:58 +0300 Subject: [PATCH] integration works))))) --- Dockerfile | 24 +++++----- Makefile | 3 +- client/internal/gui/src/SolutionsWindow.cpp | 2 +- .../httpClient/src/HttpClientManager.cpp | 6 ++- docker-compose.yml | 2 +- server/cmd/CMakeLists.txt | 2 +- server/internal/httpServer/CMakeLists.txt | 2 +- server/internal/httpServer/src/Router.cpp | 3 -- .../httpServer/src/SolutionManager.cpp | 33 +++++++------ .../internal/httpServer/src/TaskManager.cpp | 2 +- .../internal/httpServer/src/UserManager.cpp | 16 +++++-- server/internal/service/CMakeLists.txt | 4 +- .../include/{Utils.h => ServiceUtils.h} | 20 ++++---- .../src/{Utils.cpp => ServiceUtils.cpp} | 46 +++++++++---------- .../internal/service/src/SolutionService.cpp | 15 ++---- server/internal/service/tests/CMakeLists.txt | 2 +- 16 files changed, 92 insertions(+), 90 deletions(-) rename server/internal/service/include/{Utils.h => ServiceUtils.h} (95%) rename server/internal/service/src/{Utils.cpp => ServiceUtils.cpp} (92%) diff --git a/Dockerfile b/Dockerfile index 75baccc..d6343d8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,15 +1,15 @@ FROM ubuntu:latest - -ENV TZ=Europe/Moscow -RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone - + +ENV TZ=Europe/Moscow +RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone + RUN apt update -y RUN apt install -y gcc RUN apt install -y libpqxx-dev RUN apt install -y clang-tidy RUN apt-get install -y wget RUN apt install -y g++ -RUN apt install -y cmake +RUN apt install -y cmake RUN wget https://github.com/jtv/libpqxx/archive/refs/tags/7.7.5.tar.gz RUN tar -zxvf 7.7.5.tar.gz @@ -18,7 +18,7 @@ RUN ./configure CXXFLAGS=-std=c++17 --disable-dependency-tracking RUN make RUN make install -RUN apt install -y python3-pip +RUN apt install -y python3-pip RUN apt install -y cppcheck RUN apt-get update RUN apt install -y nlohmann-json3-dev @@ -40,16 +40,16 @@ RUN git clone https://github.com/google/googletest.git -b release-1.11.0 WORKDIR googletest/build RUN cmake .. -DBUILD_GMOCK=ON RUN make -RUN make install +RUN make install -RUN wget https://boostorg.jfrog.io/artifactory/main/release/1.82.0/source/boost_1_82_0.tar.gz -RUN tar xvf boost_1_82_0.tar.gz -WORKDIR boost_1_82_0 -RUN ./bootstrap.sh --prefix=/usr/ +RUN wget https://boostorg.jfrog.io/artifactory/main/release/1.82.0/source/boost_1_82_0.tar.gz +RUN tar xvf boost_1_82_0.tar.gz +WORKDIR boost_1_82_0 +RUN ./bootstrap.sh --prefix=/usr/ RUN ./b2 install RUN apt install -y clang-format - + WORKDIR /project COPY . . diff --git a/Makefile b/Makefile index 6c3ea37..64180d7 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ generate: cmake -B build/ build-project: - cd ./build && make --no-print-directory + cd ./build && cmake .. && make --no-print-directory clean: rm -rf build @@ -36,6 +36,5 @@ stop-docker: docker stop app run-docker-compose: - docker compose down --volumes docker compose up -d --build diff --git a/client/internal/gui/src/SolutionsWindow.cpp b/client/internal/gui/src/SolutionsWindow.cpp index 17832a3..ccbc7e5 100644 --- a/client/internal/gui/src/SolutionsWindow.cpp +++ b/client/internal/gui/src/SolutionsWindow.cpp @@ -70,7 +70,7 @@ void SolutionsWindow::on_chooseFileButton_clicked() { } void SolutionsWindow::on_sendButton_clicked() { - Solution sol = Core::submitSolution(task.id, path_to_file); + Solution sol = Core::submitSolution(task.id, filename->text().toUtf8().constData(), path_to_file); result->setText(QString::fromStdString(sol.result)); } diff --git a/client/internal/httpClient/src/HttpClientManager.cpp b/client/internal/httpClient/src/HttpClientManager.cpp index 18de286..9f9f7cd 100644 --- a/client/internal/httpClient/src/HttpClientManager.cpp +++ b/client/internal/httpClient/src/HttpClientManager.cpp @@ -36,7 +36,10 @@ std::pair HttpClientManager::registerUser(const std::string &log auto* cbuf = boost::asio::buffer_cast(seq); res_body.append(cbuf, boost::asio::buffer_size(seq)); } - User user = serializer->deserialUserData(res_body); + User user; + if (status == 200) { + user = serializer->deserialUserData(res_body); + } return {status, user}; } @@ -44,6 +47,7 @@ Solution HttpClientManager::submitSolution(const int &user_id, const int &task_i const std::string &path_to_sound) { std::string body = serializer->serialSolutionData(user_id, task_id, filename, path_to_sound); http::response res = client->makeGetRequest("/solution/submit", body); + std::cout << res << std::endl; // unsigned status = res.result_int(); std::string res_body; for (auto seq : res.body().data()) { diff --git a/docker-compose.yml b/docker-compose.yml index e101d36..77be625 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -22,7 +22,7 @@ services: networks: - db-network server: - image: raiden454/cpp-app:latest + image: kodkio/cpp_app:latest # use raiden454/cpp_app:latest networks: - db-network env_file: diff --git a/server/cmd/CMakeLists.txt b/server/cmd/CMakeLists.txt index e8ea8b4..fa268c2 100644 --- a/server/cmd/CMakeLists.txt +++ b/server/cmd/CMakeLists.txt @@ -4,7 +4,7 @@ project(${PROJECT_NAME}) add_executable(Server main.cpp) target_link_libraries(${PROJECT_NAME} ${HTTPSERVER_LIB} ${SERVICE_lib_LIBRARY} ${libRepository_LIB} - ${METRICS_LIBRARY} ${libEntities_LIB} ${ANTLR4_LIB} ${libDbManager_LIB}) + ${METRICS_LIBRARY} ${libEntities_LIB} ${ANTLR4_LIB} ${libDbManager_LIB} ${PQXX_LIB}) message("a = ${SERVICE_lib_INCLUDE_DIRS}") target_include_directories(Server PUBLIC ${Boost_INCLUDE_DIR} ${HTTPSERVER_INCLUDE_DIRS} ${SERVICE_lib_INCLUDE_DIRS} ${libRepository_INCLUDE_DIRS} ${libDbManager_INCLUDE_DIRS} ${METRICS_lib_INCLUDE_DIRS} ${libEntities_INCLUDE_DIRS} ${ANTLR4_LIB_INCLUDE_DIRS}) diff --git a/server/internal/httpServer/CMakeLists.txt b/server/internal/httpServer/CMakeLists.txt index a0fdd28..b49f36a 100644 --- a/server/internal/httpServer/CMakeLists.txt +++ b/server/internal/httpServer/CMakeLists.txt @@ -9,7 +9,7 @@ include_directories(${INCLUDE_DIRS}) add_library(${PROJECT_NAME} ${SOURCES}) target_include_directories(${PROJECT_NAME} PUBLIC ${INCLUDE_DIRS} ${Boost_INCLUDE_DIR} ${libEntities_INCLUDE_DIRS} ${SERVICE_lib_INCLUDE_DIRS}) -target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES} ${SERVICE_lib_LIBRARY} ${libEntities_LIB}) +target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES} ${SERVICE_lib_LIBRARY} ${libEntities_LIB} ${PQXX_LIB}) set(HTTPSERVER_LIB ${PROJECT_NAME}) set(HTTPSERVER_LIB ${HTTPSERVER_LIB} PARENT_SCOPE) diff --git a/server/internal/httpServer/src/Router.cpp b/server/internal/httpServer/src/Router.cpp index b4e3f61..68aba41 100644 --- a/server/internal/httpServer/src/Router.cpp +++ b/server/internal/httpServer/src/Router.cpp @@ -12,7 +12,6 @@ Router::Router(std::string_view doc_root_) doc_root(doc_root_) {} http::message_generator Router::handleRequest(http::request &&req) { - std::cout << req << std::endl; if (req.method() != http::verb::get && req.method() != http::verb::post) return getBadRequest(req, "Unknown HTTP-method"); @@ -20,8 +19,6 @@ http::message_generator Router::handleRequest(http::request & return getBadRequest(req, "Illegal request-target"); } - std::cout << req.target() << std::endl; - if (req.target() == "/solution/submit") { return solutionManager->createSolution(std::move(req)); } else if (req.target() == "/solution/all") { diff --git a/server/internal/httpServer/src/SolutionManager.cpp b/server/internal/httpServer/src/SolutionManager.cpp index f8e1dcf..9e08d63 100644 --- a/server/internal/httpServer/src/SolutionManager.cpp +++ b/server/internal/httpServer/src/SolutionManager.cpp @@ -16,11 +16,10 @@ SolutionManager::SolutionManager() void SolutionManager::setService(std::shared_ptr service) { solutionService = std::move(service); } http::message_generator SolutionManager::createSolution(http::request&& req) { - size_t user_id, task_id; - std::string filename, filedata; - std::tie(user_id, task_id, filename, filedata) = serializer->deserialNewSolutionData(req.body()); - // Solution sol = TmpSolutionService::createSolution(user_id, task_id, source); try { + size_t user_id, task_id; + std::string filename, filedata; + std::tie(user_id, task_id, filename, filedata) = serializer->deserialNewSolutionData(req.body()); Solution sol = solutionService->createSolution(user_id, task_id, filename, filedata); http::response res{http::status::ok, req.version()}; @@ -36,15 +35,19 @@ http::message_generator SolutionManager::createSolution(http::request&& req) { - size_t user_id, task_id; - std::tie(user_id, task_id) = serializer->deserialTaskData(req.body()); - std::vector solutions; - solutions = solutionService->getSolutionsByUserAndTaskId(user_id, task_id); - http::response res{http::status::ok, req.version()}; - res.set(http::field::server, BOOST_BEAST_VERSION_STRING); - res.set(http::field::content_type, "text/plain"); - res.keep_alive(req.keep_alive()); - res.body() = serializer->serialSolutions(solutions); - res.prepare_payload(); - return res; + try { + size_t user_id, task_id; + std::tie(user_id, task_id) = serializer->deserialTaskData(req.body()); + std::vector solutions; + // solutions = solutionService->getSolutionsByUserAndTaskId(user_id, task_id); + http::response res{http::status::ok, req.version()}; + res.set(http::field::server, BOOST_BEAST_VERSION_STRING); + res.set(http::field::content_type, "text/plain"); + res.keep_alive(req.keep_alive()); + res.body() = serializer->serialSolutions(solutions); + res.prepare_payload(); + return res; + } catch (const std::exception& e) { + return getBadRequest(req, e.what()); + } } diff --git a/server/internal/httpServer/src/TaskManager.cpp b/server/internal/httpServer/src/TaskManager.cpp index 4a6fd97..6c24bbf 100644 --- a/server/internal/httpServer/src/TaskManager.cpp +++ b/server/internal/httpServer/src/TaskManager.cpp @@ -12,9 +12,9 @@ TaskManager::TaskManager() : serializer(std::make_shared()), taskSer void TaskManager::setService(std::shared_ptr service) { taskService = service; } http::message_generator TaskManager::createTask(http::request &&req) { - std::string description = serializer->deserialNewTaskData(req.body()); try { + std::string description = serializer->deserialNewTaskData(req.body()); taskService->createTask(description, 0.5); // TmpTaskService::createTask(description); http::response res{http::status::ok, req.version()}; diff --git a/server/internal/httpServer/src/UserManager.cpp b/server/internal/httpServer/src/UserManager.cpp index 881dd8b..b94bca7 100644 --- a/server/internal/httpServer/src/UserManager.cpp +++ b/server/internal/httpServer/src/UserManager.cpp @@ -9,13 +9,18 @@ void UserManager::setService(std::shared_ptr service) { userServic http::message_generator UserManager::loginUser(http::request &&req) { std::string login, password; - std::tie(login, password) = serializer->deserialUserData(req.body()); + + try { + std::tie(login, password) = serializer->deserialUserData(req.body()); + } catch (...) { + return getBadRequest(req, "Something went wrong!"); + } User user; bool flag = true; try { - auto result = userService->login(login, password); + user = userService->login(login, password); } catch (...) { flag = false; } @@ -26,6 +31,7 @@ http::message_generator UserManager::loginUser(http::request res.set(http::field::content_type, "text/plain"); res.keep_alive(req.keep_alive()); res.body() = serializer->serialUserData(user); + std::cout << serializer->serialUserData(user) << std::endl; res.prepare_payload(); return res; } else { @@ -34,16 +40,16 @@ http::message_generator UserManager::loginUser(http::request res.set(http::field::content_type, "text/plain"); res.keep_alive(req.keep_alive()); res.body() = serializer->serialUserData(user); + std::cout << serializer->serialUserData(user) << std::endl; res.prepare_payload(); return res; } } http::message_generator UserManager::registerUser(http::request &&req) { - std::string login, password, username; - std::tie(login, password, username) = serializer->deserialNewUserData(req.body()); - try { + std::string login, password, username; + std::tie(login, password, username) = serializer->deserialNewUserData(req.body()); User user = userService->createUser(login, username, password); http::response res{http::status::ok, req.version()}; res.set(http::field::server, BOOST_BEAST_VERSION_STRING); diff --git a/server/internal/service/CMakeLists.txt b/server/internal/service/CMakeLists.txt index ab9c04c..b598428 100644 --- a/server/internal/service/CMakeLists.txt +++ b/server/internal/service/CMakeLists.txt @@ -23,6 +23,6 @@ set(SERVICE_lib_INCLUDE_DIRS ${INCLUDE_DIRS}) set(SERVICE_lib_INCLUDE_DIRS ${SERVICE_lib_INCLUDE_DIRS} PARENT_SCOPE) -enable_testing() -add_subdirectory(tests) +#enable_testing() +#add_subdirectory(tests) diff --git a/server/internal/service/include/Utils.h b/server/internal/service/include/ServiceUtils.h similarity index 95% rename from server/internal/service/include/Utils.h rename to server/internal/service/include/ServiceUtils.h index 432521e..4361070 100644 --- a/server/internal/service/include/Utils.h +++ b/server/internal/service/include/ServiceUtils.h @@ -1,10 +1,10 @@ -#pragma once - -#include -#include - -class Utils { - public: - static std::string convertIntArrayIntoString(std::vector& arr); - static std::vector convertStringIntoIntArray(const std::string& str); -}; +#pragma once + +#include +#include + +class Utils { + public: + static std::string convertIntArrayIntoString(std::vector& arr); + static std::vector convertStringIntoIntArray(const std::string& str); +}; diff --git a/server/internal/service/src/Utils.cpp b/server/internal/service/src/ServiceUtils.cpp similarity index 92% rename from server/internal/service/src/Utils.cpp rename to server/internal/service/src/ServiceUtils.cpp index 95a1629..02671b1 100644 --- a/server/internal/service/src/Utils.cpp +++ b/server/internal/service/src/ServiceUtils.cpp @@ -1,23 +1,23 @@ -#include "Utils.h" - -#include -#include -#include -#include -#include - -std::string Utils::convertIntArrayIntoString(std::vector& arr) { - std::string str; - for (size_t i = 0; i < arr.size(); i++) { - str += std::to_string(arr[i]) + " "; - } - return str; -} - -std::vector Utils::convertStringIntoIntArray(const std::string& str) { - std::stringstream ss(str); - std::vector v; - - std::copy(std::istream_iterator(ss), {}, back_inserter(v)); - return v; -} +#include "ServiceUtils.h" + +#include +#include +#include +#include +#include + +std::string Utils::convertIntArrayIntoString(std::vector& arr) { + std::string str; + for (size_t i = 0; i < arr.size(); i++) { + str += std::to_string(arr[i]) + " "; + } + return str; +} + +std::vector Utils::convertStringIntoIntArray(const std::string& str) { + std::stringstream ss(str); + std::vector v; + + std::copy(std::istream_iterator(ss), {}, back_inserter(v)); + return v; +} diff --git a/server/internal/service/src/SolutionService.cpp b/server/internal/service/src/SolutionService.cpp index 89e754a..20e6af0 100644 --- a/server/internal/service/src/SolutionService.cpp +++ b/server/internal/service/src/SolutionService.cpp @@ -9,7 +9,7 @@ #include "PythonAntlr.h" #include "SolutionRepository.hpp" #include "TaskRepository.hpp" -#include "Utils.h" +#include "ServiceUtils.h" const std::string PLAGIAT_VERDICT = "Не, ну вы не палитесь. Плагиат."; const std::string NOT_PLAGIAT_VERDICT = "Красивое решение. А главное уникальное !"; @@ -67,15 +67,14 @@ std::pair SolutionService::getMaxTextResMetric(std::vector SolutionService::getMaxTokenResMetric(std::vector& solutions, std::vector& tokens, float treshold) { std::pair maxMatch = std::make_pair(0.0, 0ul); - for (auto sol : solutions) { - std::cout << "Convert to string" << std::endl; - std::cout << Utils::convertIntArrayIntoString(tokens) << std::endl; - std::cout << sol.getTokens() << std::endl; + for (auto sol : solutions) { tokenMetric = std::make_unique(); tokenMetric->setData(tokens, Utils::convertStringIntoIntArray(sol.getTokens())); float tokenBasedRes = float(tokenMetric->getMetric()); + auto sol_tokens = Utils::convertStringIntoIntArray(sol.getTokens()); + tokenMetric = std::make_unique(); tokenMetric->setData(tokens, Utils::convertStringIntoIntArray(sol.getTokens())); tokenBasedRes = (tokenBasedRes + float(tokenMetric->getMetric())) / 2; @@ -124,15 +123,9 @@ Solution SolutionService::createSolution(size_t userId, size_t taskId, const std } } - std::cout << "Plagiat" << std::endl; - std::cout << plagiatSolId << std::endl; - Solution sol = Solution(std::ctime(&now), userId, filedata, taskId, result, Utils::convertIntArrayIntoString(tokensTypes), astTree, plagiatSolId); - std::cout << "Dick" << std::endl; - std::cout << sol.getOrigSolution() << std::endl; - size_t id = solutionRepo->storeSolution(sol); sol.setId(id); return sol; diff --git a/server/internal/service/tests/CMakeLists.txt b/server/internal/service/tests/CMakeLists.txt index e1cdd49..2b5223f 100644 --- a/server/internal/service/tests/CMakeLists.txt +++ b/server/internal/service/tests/CMakeLists.txt @@ -7,7 +7,7 @@ find_package(GTest REQUIRED) add_executable(${PROJECT_NAME} ${SOURCES}) -target_link_libraries(${PROJECT_NAME} ${SERVICE_lib_LIBRARY} GTest::GTest gmock) +target_link_libraries(${PROJECT_NAME} ${SERVICE_lib_LIBRARY} GTest::GTest gmock ${PQXX_LIB}) target_include_directories(${PROJECT_NAME} PUBLIC ${SERVICE_lib_INCLUDE_DIRS}) -- GitLab