From c0753dec07bf434dca7643422049ddf435838b8c 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: Sun, 21 May 2023 20:02:54 +0300 Subject: [PATCH] add advanced task creation --- client/internal/core/include/Core.h | 3 ++- client/internal/core/src/Core.cpp | 5 ++-- client/internal/entities/include/Task.h | 4 ++- client/internal/entities/src/Task.cpp | 3 ++- client/internal/gui/include/AddTaskDialog.h | 5 ++++ client/internal/gui/src/AddTaskDialog.cpp | 23 +++++++++++++--- client/internal/gui/src/TasksWindow.cpp | 4 +-- .../httpClient/include/HttpClientManager.h | 4 +-- .../internal/httpClient/include/Serializer.h | 2 +- .../httpClient/src/HttpClientManager.cpp | 5 ++-- client/internal/httpClient/src/Serializer.cpp | 11 +++++--- .../internal/httpServer/include/Serializer.h | 3 ++- server/internal/httpServer/include/Utils.h | 3 +++ server/internal/httpServer/src/Serializer.cpp | 27 ++++++++++++++++--- .../httpServer/src/SolutionManager.cpp | 20 ++++++++++---- .../internal/httpServer/src/TaskManager.cpp | 19 ++++++++----- .../internal/httpServer/src/UserManager.cpp | 8 +++--- server/internal/httpServer/src/Utils.cpp | 11 ++++++++ 18 files changed, 121 insertions(+), 39 deletions(-) diff --git a/client/internal/core/include/Core.h b/client/internal/core/include/Core.h index c837a25..31d3cee 100644 --- a/client/internal/core/include/Core.h +++ b/client/internal/core/include/Core.h @@ -16,7 +16,8 @@ public: static Solution submitSolution(const int& task_id, const std::string& filename, const std::string& path_to_file); - static unsigned int createTask(const std::string &desc); + static unsigned int createTask(const std::string& name, const std::string &desc, + const double& threshold); static void logout(); diff --git a/client/internal/core/src/Core.cpp b/client/internal/core/src/Core.cpp index e200e63..3f8acb6 100644 --- a/client/internal/core/src/Core.cpp +++ b/client/internal/core/src/Core.cpp @@ -34,7 +34,8 @@ Solution Core::submitSolution(const int &task_id, const std::string& filename, c return client.submitSolution(user_id, task_id, filename, path_to_file); } -unsigned int Core::createTask(const std::string &desc) { - return client.createTask(desc); +unsigned int Core::createTask(const std::string& name, const std::string &desc, + const double& threshold) { + return client.createTask(name, desc, threshold); } diff --git a/client/internal/entities/include/Task.h b/client/internal/entities/include/Task.h index de9c488..c9b2325 100644 --- a/client/internal/entities/include/Task.h +++ b/client/internal/entities/include/Task.h @@ -4,7 +4,9 @@ struct Task{ std::size_t id; + std::string name; std::string description; + double threshold; - Task(std::size_t id, std::string_view desc); + Task(std::size_t id, std::string_view desc, std::string_view name, double threshold); }; diff --git a/client/internal/entities/src/Task.cpp b/client/internal/entities/src/Task.cpp index 38e9e6e..e58c116 100644 --- a/client/internal/entities/src/Task.cpp +++ b/client/internal/entities/src/Task.cpp @@ -1,3 +1,4 @@ #include "Task.h" -Task::Task(std::size_t id, std::string_view desc) : id(id), description(desc) {} +Task::Task(std::size_t id, std::string_view desc, std::string_view name, double threshold) : + id(id), description(desc), name(name), threshold(threshold) {}; diff --git a/client/internal/gui/include/AddTaskDialog.h b/client/internal/gui/include/AddTaskDialog.h index c5b6547..0a63513 100644 --- a/client/internal/gui/include/AddTaskDialog.h +++ b/client/internal/gui/include/AddTaskDialog.h @@ -7,6 +7,7 @@ #include #include #include +#include class AddTaskDialog : public QDialog { Q_OBJECT @@ -20,6 +21,10 @@ public slots: private: QVBoxLayout* verticalLayout = nullptr; + QLineEdit* name = nullptr; + QLabel* nameLabel = nullptr; + QLineEdit* threshold = nullptr; + QLabel* thresholLabel = nullptr; QLabel* label = nullptr; QTextEdit* editor = nullptr; QPushButton *createButton = nullptr; diff --git a/client/internal/gui/src/AddTaskDialog.cpp b/client/internal/gui/src/AddTaskDialog.cpp index 4955001..7d72d99 100644 --- a/client/internal/gui/src/AddTaskDialog.cpp +++ b/client/internal/gui/src/AddTaskDialog.cpp @@ -20,6 +20,20 @@ void AddTaskDialog::setupUi(QDialog *AddTaskDialog) { verticalLayout = new QVBoxLayout(AddTaskDialog); + nameLabel = new QLabel(AddTaskDialog); + nameLabel->setText(QString::fromUtf8("Имя задания:")); + verticalLayout->addWidget(nameLabel); + + name = new QLineEdit(AddTaskDialog); + verticalLayout->addWidget(name); + + thresholLabel = new QLabel(AddTaskDialog); + thresholLabel->setText(QString::fromUtf8("Порог:")); + verticalLayout->addWidget(thresholLabel); + + threshold = new QLineEdit(AddTaskDialog); + verticalLayout->addWidget(threshold); + label = new QLabel(AddTaskDialog); label->setText(QString::fromUtf8("Введите текст задания")); verticalLayout->addWidget(label); @@ -38,11 +52,14 @@ void AddTaskDialog::setupUi(QDialog *AddTaskDialog) { void AddTaskDialog::on_createButton_clicked() { std::string desc = editor->toPlainText().toUtf8().constData(); - if (desc.empty()) { - QMessageBox::warning(this, "Ошибка отправки", "Описание не может быть пустыми"); + double th = threshold->text().toDouble(); + std::string n = name->text().toUtf8().constData(); + if (desc.empty() || n.empty() || threshold->text().isEmpty()) { + QMessageBox::warning(this, "Ошибка отправки", "Поля не могут быть пустыми"); return; } - unsigned result = Core::createTask(desc); + + unsigned result = Core::createTask(n, desc, th); switch (result) { case 200: accept(); diff --git a/client/internal/gui/src/TasksWindow.cpp b/client/internal/gui/src/TasksWindow.cpp index 3f14654..a74b63c 100644 --- a/client/internal/gui/src/TasksWindow.cpp +++ b/client/internal/gui/src/TasksWindow.cpp @@ -43,7 +43,7 @@ void TasksWindow::setupUi(QMainWindow *UserWindow) { tasks = new QComboBox(this); for (int i = 0; i < tasks_vector.size(); i++) { - tasks->insertItem(i, QString::number(tasks_vector[i].id)); + tasks->insertItem(i, QString::fromUtf8(tasks_vector[i].name)); } if (tasks_vector.empty()) { @@ -116,7 +116,7 @@ void TasksWindow::updateTasks() { tasks_vector = Core::getAllTasks(); tasks->clear(); for (int i = 0; i < tasks_vector.size(); i++) { - tasks->insertItem(i, QString::number(tasks_vector[i].id)); + tasks->insertItem(i, QString::fromUtf8(tasks_vector[i].name)); } tasks->setCurrentIndex(0); diff --git a/client/internal/httpClient/include/HttpClientManager.h b/client/internal/httpClient/include/HttpClientManager.h index 9007351..2d15f7f 100644 --- a/client/internal/httpClient/include/HttpClientManager.h +++ b/client/internal/httpClient/include/HttpClientManager.h @@ -23,8 +23,8 @@ public: const std::string &path_to_solution); unsigned int getAllSolutionsForTask(const int& user_id, const int& task_id); std::vector getAllTasks(); - unsigned int createTask(const std::string& desc); -// std::vector getMetrics(const int& sol_id); + unsigned int createTask(const std::string& name, const std::string &desc, + const double& threshold); void setHttpClient(std::shared_ptr client_); private: std::string host; diff --git a/client/internal/httpClient/include/Serializer.h b/client/internal/httpClient/include/Serializer.h index 0e874c1..ba742f2 100644 --- a/client/internal/httpClient/include/Serializer.h +++ b/client/internal/httpClient/include/Serializer.h @@ -13,7 +13,7 @@ public: std::string serialRegisterData(std::string_view login, std::string_view username, std::string_view password); std::string serialSolutionData(const int& user_id, const int& task_id, const std::string& filename, const std::string& path_to_file); - std::string serialNewTaskData(std::string_view desc); + std::string serialNewTaskData(std::string_view name, std::string_view desc, double threshold); User deserialUserData(std::string_view body); Solution deserialSolutionData(std::string_view body); diff --git a/client/internal/httpClient/src/HttpClientManager.cpp b/client/internal/httpClient/src/HttpClientManager.cpp index 9f9f7cd..a8831c0 100644 --- a/client/internal/httpClient/src/HttpClientManager.cpp +++ b/client/internal/httpClient/src/HttpClientManager.cpp @@ -77,8 +77,9 @@ std::vector HttpClientManager::getAllTasks() { return tasks; } -unsigned int HttpClientManager::createTask(const std::string &desc) { - std::string body = serializer->serialNewTaskData(desc); +unsigned int HttpClientManager::createTask(const std::string& name, const std::string &desc, + const double& threshold) { + std::string body = serializer->serialNewTaskData(name, desc, threshold); http::response res = client->makeGetRequest("/task/create", body); unsigned int result = res.result_int(); return result; diff --git a/client/internal/httpClient/src/Serializer.cpp b/client/internal/httpClient/src/Serializer.cpp index 859db2c..c2d803d 100644 --- a/client/internal/httpClient/src/Serializer.cpp +++ b/client/internal/httpClient/src/Serializer.cpp @@ -22,7 +22,7 @@ User Serializer::deserialUserData(std::string_view body) { User res = { json.get("user_id"), json.get("login"), - json.get("password"), + "", json.get("username") }; return res; @@ -45,8 +45,9 @@ std::vector Serializer::deserialAllTasks(std::string_view body) { boost::property_tree::ptree json; boost::property_tree::read_json(ss, json); std::vector tasks; - for (auto &sound : json.get_child("tasks")) { - Task new_task(sound.second.get("task_id"), sound.second.get("description")); + for (auto &task : json.get_child("tasks")) { + Task new_task(task.second.get("task_id"), task.second.get("description"), + task.second.get("name"), task.second.get("threshold")); tasks.push_back(new_task); } return tasks; @@ -81,9 +82,11 @@ Solution Serializer::deserialSolutionData(std::string_view body) { return res; } -std::string Serializer::serialNewTaskData(std::string_view desc) { +std::string Serializer::serialNewTaskData(std::string_view name, std::string_view desc, double threshold) { boost::property_tree::ptree json; + json.put("name", name); json.put("description", desc); + json.put("threshold", threshold); std::stringstream out; boost::property_tree::write_json(out, json); return out.str(); diff --git a/server/internal/httpServer/include/Serializer.h b/server/internal/httpServer/include/Serializer.h index 1398b05..9790ac5 100644 --- a/server/internal/httpServer/include/Serializer.h +++ b/server/internal/httpServer/include/Serializer.h @@ -13,7 +13,7 @@ class Serializer { public: std::tuple deserialNewSolutionData(const std::string& val); std::tuple deserialTaskData(const std::string& val); - std::string deserialNewTaskData(const std::string& val); + std::tuple deserialNewTaskData(const std::string& val); size_t deserialSolutionData(const std::string& val); std::tuple deserialUserData(const std::string& val); std::tuple deserialNewUserData(const std::string& val); @@ -22,6 +22,7 @@ class Serializer { std::string serialAllTasks(const std::vector& tasks); std::string serialUserData(const User& user); std::string serialSolution(const Solution& sol); + std::string serialTask(const Task& task); }; #endif // APP_HTTPSERVER_HTTPSERVER_MANAGERS_SERIALIZER_H_ diff --git a/server/internal/httpServer/include/Utils.h b/server/internal/httpServer/include/Utils.h index a817790..b8f0323 100644 --- a/server/internal/httpServer/include/Utils.h +++ b/server/internal/httpServer/include/Utils.h @@ -10,3 +10,6 @@ void fail(beast::error_code ec, char const* what); http::response getBadRequest(const http::request& request, beast::string_view why); + +http::response getInternalServerError(const http::request& request, + beast::string_view why); \ No newline at end of file diff --git a/server/internal/httpServer/src/Serializer.cpp b/server/internal/httpServer/src/Serializer.cpp index de400b7..ee7eb6a 100644 --- a/server/internal/httpServer/src/Serializer.cpp +++ b/server/internal/httpServer/src/Serializer.cpp @@ -29,16 +29,24 @@ std::tuple Serializer::deserialTaskData(const std::str ss << val; boost::property_tree::ptree json; boost::property_tree::read_json(ss, json); - std::tuple res = {json.get("user_id"), json.get("task_id")}; + std::tuple res = { + json.get("user_id"), + json.get("task_id") + }; return res; } -std::string Serializer::deserialNewTaskData(const std::string &val) { +std::tuple Serializer::deserialNewTaskData(const std::string &val) { std::stringstream ss; ss << val; boost::property_tree::ptree json; boost::property_tree::read_json(ss, json); - return json.get("description"); + std::tuple res = { + json.get("name"), + json.get("description"), + json.get("threshold") + }; + return res; } std::tuple Serializer::deserialUserData(const std::string &val) { @@ -84,6 +92,8 @@ std::string Serializer::serialAllTasks(const std::vector &tasks) { boost::property_tree::ptree node; node.put("task_id", i.getId()); node.put("description", i.getDescription()); + node.put("name", i.getName()); + node.put("threshold", i.getTreshhold()); tasks_nodes.push_back(std::make_pair("", node)); } json.add_child("tasks", tasks_nodes); @@ -97,7 +107,6 @@ std::string Serializer::serialUserData(const User &user) { json.put("user_id", user.getId()); json.put("login", user.getLogin()); json.put("username", user.getUsername()); - json.put("password", user.getPassword()); std::stringstream out; boost::property_tree::write_json(out, json); return out.str(); @@ -112,3 +121,13 @@ std::string Serializer::serialSolution(const Solution &sol) { boost::property_tree::write_json(out, json); return out.str(); } + +std::string Serializer::serialTask(const Task &task) { + boost::property_tree::ptree json; + json.put("name", task.getName()); + json.put("description", task.getDescription()); + json.put("threshold", task.getTreshhold()); + std::stringstream out; + boost::property_tree::write_json(out, json); + return out.str(); +} diff --git a/server/internal/httpServer/src/SolutionManager.cpp b/server/internal/httpServer/src/SolutionManager.cpp index 6174c7e..742a8f9 100644 --- a/server/internal/httpServer/src/SolutionManager.cpp +++ b/server/internal/httpServer/src/SolutionManager.cpp @@ -15,12 +15,16 @@ 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; 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); + } catch (...) { + return getBadRequest(req, "Bad parameters"); + } + try { + Solution sol = solutionService->createSolution(user_id, task_id, filename, filedata); 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"); @@ -29,14 +33,20 @@ http::message_generator SolutionManager::createSolution(http::request&& req) { + size_t user_id, task_id; + try { - size_t user_id, task_id; std::tie(user_id, task_id) = serializer->deserialTaskData(req.body()); + } catch (...) { + return getBadRequest(req, "Bad parameters"); + } + + try { std::vector solutions; http::response res{http::status::ok, req.version()}; res.set(http::field::server, BOOST_BEAST_VERSION_STRING); diff --git a/server/internal/httpServer/src/TaskManager.cpp b/server/internal/httpServer/src/TaskManager.cpp index 9f8f579..42551eb 100644 --- a/server/internal/httpServer/src/TaskManager.cpp +++ b/server/internal/httpServer/src/TaskManager.cpp @@ -11,24 +11,31 @@ 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, name; + double threshold; try { - std::string description = serializer->deserialNewTaskData(req.body()); - taskService->createTask(description); - http::response res{http::status::ok, req.version()}; + std::tie(name, description, threshold) = serializer->deserialNewTaskData(req.body()); + } catch (...) { + return getBadRequest(req, "Bad parameters"); + } + + try { + Task task = taskService->createTask(description, name, threshold); + 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->serialTask(task); res.prepare_payload(); return res; } catch (...) { - return getBadRequest(req, "Something went wrong!"); + return getInternalServerError(req, "Something went wrong!"); } } http::message_generator TaskManager::getAllTasks(http::request &&req) { try { std::vector tasks = taskService->getAllTasks(); - // std::vector tasks = TmpTaskService::getAllTasks(); 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"); @@ -37,6 +44,6 @@ http::message_generator TaskManager::getAllTasks(http::request try { std::tie(login, password) = serializer->deserialUserData(req.body()); } catch (...) { - return getBadRequest(req, "Неправильные параметры!"); + return getBadRequest(req, "Bad parameters"); } User user; @@ -51,7 +51,7 @@ http::message_generator UserManager::registerUser(http::requestdeserialNewUserData(req.body()); } catch (...) { - return getBadRequest(req, "Неправильные параметры!"); + return getBadRequest(req, "Bad parameters"); } try { @@ -68,10 +68,10 @@ http::message_generator UserManager::registerUser(http::request getBadRequest(const http::request getInternalServerError(const http::request& request, + beast::string_view why) { + http::response res{http::status::internal_server_error, request.version()}; + res.set(http::field::server, BOOST_BEAST_VERSION_STRING); + res.set(http::field::content_type, "text/plain"); + res.keep_alive(request.keep_alive()); + res.body() = std::string(why); + res.prepare_payload(); + return res; +} \ No newline at end of file -- GitLab