From 5bce421326159a6740ba95c026efcb7652f01a48 Mon Sep 17 00:00:00 2001 From: AJ Date: Mon, 8 Jul 2024 19:44:08 +0200 Subject: [PATCH] hoffentlich klappts --- script.sql | 4 +- .../EinsatzplanFrameController.cpp | 12 +- .../EinsatzplanFrameController.hpp | 3 +- .../PlanGridController/PlanGridController.cpp | 6 +- .../PlanGridController/PlanGridController.hpp | 2 +- src/Core/DBHandler/DBHandler/DBHandler.cpp | 190 ++++++ src/Core/DBHandler/DBHandler/DBHandler.hpp | 9 + src/Core/DBHandler/DBPlan/DBPlan.cpp | 539 ------------------ src/Core/DBHandler/DBPlan/DBPlan.hpp | 113 ---- 9 files changed, 212 insertions(+), 666 deletions(-) delete mode 100644 src/Core/DBHandler/DBPlan/DBPlan.cpp delete mode 100644 src/Core/DBHandler/DBPlan/DBPlan.hpp diff --git a/script.sql b/script.sql index 7f3150b..1d8a082 100644 --- a/script.sql +++ b/script.sql @@ -61,7 +61,7 @@ CREATE TABLE Veranstalter ( ID SERIAL PRIMARY KEY, ort VARCHAR(1) DEFAULT random_between_two(), raum INTEGER NOT NULL, - name VARCHAR(3) NOT NULL, + name VARCHAR(3) NOT NULL UNIQUE, dauer INTEGER NOT NULL, used INTEGER DEFAULT(0) ); @@ -77,7 +77,7 @@ CREATE TABLE StundenImPlan( CREATE TABLE Krankmeldung( uhrzeit_id INTEGER REFERENCES StundenImPlan(uhrzeit_ID), tag INTEGER REFERENCES StundenImPlan(tag), - veranstalter_id INTEGER REFERENCES StundenImPlan(veranstalter_ID), + veranstalter_id INTEGER REFERENCES StundenImPlan(veranstalter_ID) ON DELETE CASCADE, PRIMARY KEY (uhrzeit_ID,tag,veranstalter_id) ) diff --git a/src/Controller/EinsatzplanFrameController/EinsatzplanFrameController.cpp b/src/Controller/EinsatzplanFrameController/EinsatzplanFrameController.cpp index edab3e1..8d07d32 100644 --- a/src/Controller/EinsatzplanFrameController/EinsatzplanFrameController.cpp +++ b/src/Controller/EinsatzplanFrameController/EinsatzplanFrameController.cpp @@ -16,21 +16,21 @@ EinsatzplanFrameController::EinsatzplanFrameController(QString id, bool admin) } void EinsatzplanFrameController::deleteMember(QString id) { - DBPlan* db = new DBPlan(m_connectionString); + DBHandler* db = new DBHandler(m_connectionString); db->deleteVeranstalter(id.toStdString( )); } void EinsatzplanFrameController::deleteVeranstaltung(QString veranstaltungsname) { - DBPlan* db = new DBPlan(m_connectionString); + DBHandler* db = new DBHandler(m_connectionString); db->deleteVeranstaltung(veranstaltungsname.toStdString( )); } void EinsatzplanFrameController::createMember(QString name, QString email, QString passwort, bool admin) { - DBPlan* db = new DBPlan(m_connectionString); - db->hinzufuegenVeranstalter(email.toStdString( ), name.toStdString( ), passwort.toStdString( ), admin ? "TRUE" : "FALSE"); + DBHandler* db = new DBHandler(m_connectionString); + db->createVeranstalter(email.toStdString( ), name.toStdString( ), passwort.toStdString( ), admin ? "TRUE" : "FALSE"); } void EinsatzplanFrameController::createVeranstaltung(QString name, QString raum, QString campus, QString time) { - DBPlan* db = new DBPlan(m_connectionString); - db->hinzufuegenVeranstaltung(name.toStdString( ), std::to_string((char)time.toStdString( ).at(0) - 48), campus.toStdString( ), raum.toStdString( )); + DBHandler* db = new DBHandler(m_connectionString); + db->createVeranstaltung(name.toStdString( ), std::to_string((char)time.toStdString( ).at(0) - 48), campus.toStdString( ), raum.toStdString( )); } diff --git a/src/Controller/EinsatzplanFrameController/EinsatzplanFrameController.hpp b/src/Controller/EinsatzplanFrameController/EinsatzplanFrameController.hpp index b8f1164..f209478 100644 --- a/src/Controller/EinsatzplanFrameController/EinsatzplanFrameController.hpp +++ b/src/Controller/EinsatzplanFrameController/EinsatzplanFrameController.hpp @@ -3,9 +3,8 @@ #include #include -#include "../../Core/DBHandler/DBPlan/DBPlan.hpp" +#include "../../Core/DBHandler/DBHandler/DBHandler.hpp" #include "../../Core/config/config.hpp" -#include "../PlanGridController/PlanGridController.hpp" class EinsatzplanFrameController { private: diff --git a/src/Controller/PlanGridController/PlanGridController.cpp b/src/Controller/PlanGridController/PlanGridController.cpp index 917cef7..e4e02c3 100644 --- a/src/Controller/PlanGridController/PlanGridController.cpp +++ b/src/Controller/PlanGridController/PlanGridController.cpp @@ -28,9 +28,9 @@ PlanGridController::PlanGridController( ) { } QMap, QWidget*>* PlanGridController::getVeranstaltungen( ) { - DBPlan* db = new DBPlan(m_connectionString); + DBHandler* db = new DBHandler(m_connectionString); - std::vector planData = db->getPlan( ); + std::vector planData = db->getVeranstaltung( ); for (int i = 0; i < 5; ++i) for (int j = 0; j < 5; ++j) { @@ -128,7 +128,7 @@ QMap, QWidget*>* PlanGridController::getVeranstaltungen( } void PlanGridController::Krankmelden(const int id, int tag, int stunde) { - DBPlan db(m_connectionString); + DBHandler db(m_connectionString); db.meldeKrank(std::to_string(id), std::to_string(tag), std::to_string(stunde)); } diff --git a/src/Controller/PlanGridController/PlanGridController.hpp b/src/Controller/PlanGridController/PlanGridController.hpp index 38c1f8a..a0c5bbc 100644 --- a/src/Controller/PlanGridController/PlanGridController.hpp +++ b/src/Controller/PlanGridController/PlanGridController.hpp @@ -7,7 +7,7 @@ #include #include "../../Core/config/config.hpp" -#include "../../Core/DBHandler/DBPlan/DBPlan.hpp" +#include "../../Core/DBHandler/DBHandler/DBHandler.hpp" class PlanGridController : public QObject { Q_OBJECT diff --git a/src/Core/DBHandler/DBHandler/DBHandler.cpp b/src/Core/DBHandler/DBHandler/DBHandler.cpp index dd9769b..d86e9cd 100644 --- a/src/Core/DBHandler/DBHandler/DBHandler.cpp +++ b/src/Core/DBHandler/DBHandler/DBHandler.cpp @@ -12,3 +12,193 @@ DBHandler::DBHandler(std::string connStr) : fmt::print(e.what( )); } }; + +void DBHandler::deleteVeranstalter(std::string id){ + try { + pqxx::work worker(connectionObject); + std::string query = fmt::format("DELETE FROM Student WHERE id = {0}",id); + worker.exec(query); + worker.commit(); + query = fmt::format("DELETE FROM Veranstalter WHERE id = {0}",id); + worker.exec_params(query); + worker.commit(); + fmt::print("Veranstalter with ID {} deleted successfully.\n", id); + } + catch (const std::exception& e) { + fmt::print("ERROR: {0}", e.what( )); + } +} + +void DBHandler::deleteVeranstaltung(std::string name) { + try { + pqxx::work worker(connectionObject); + std::string query = fmt::format("DELETE FROM Veranstaltung WHERE name = '{0}'", name); + worker.exec(query); + worker.commit(); + fmt::print("Veranstaltung with name '{}' deleted successfully.\n", name); + } catch (const std::exception& e) { + fmt::print(stderr, "Error deleting Veranstaltung: {}\n", e.what()); + } +} + +void DBHandler::createVeranstalter(std::string email, std::string name, std::string passwort, std::string admin) { + try { + pqxx::work worker(connectionObject); + std::string query = fmt::format( + "INSERT INTO Veranstalter (email, name, passwort, admin) VALUES ('{}', '{}', '{}', {})", + email, name, passwort, admin + ); + worker.exec(query); + worker.commit(); + fmt::print("Veranstalter {} created successfully.\n", name); + } catch (const std::exception& e) { + fmt::print(stderr, "Error creating Veranstalter: {}\n", e.what()); + } +} + +void DBHandler::createVeranstaltung(std::string name, std::string uhrzeit, std::string standort, std::string raum) { + try { + pqxx::work worker(connectionObject); + std::string query = fmt::format( + "INSERT INTO Veranstaltung (name, uhrzeit, standort, raum) VALUES ('{}', '{}', '{}', '{}')", + name, uhrzeit, standort, raum + ); + worker.exec(query); + worker.commit(); + fmt::print("Veranstaltung {} created successfully.\n", name); + } catch (const std::exception& e) { + fmt::print(stderr, "Error creating Veranstaltung: {}\n", e.what()); + } +} + +void DBHandler::createEinsatzplan() { + try { + pqxx::work worker(connectionObject); + + // Fetch all Veranstaltungen + pqxx::result veranstaltungen = worker.exec("SELECT ID, dauer, used FROM Veranstaltung"); + + // Fetch all available Uhrzeiten + std::vector uhrzeiten = {1, 2, 3, 4, 5}; // Representing 1 to 5 time slots + + // Fetch all available Veranstalter + pqxx::result veranstalter = worker.exec("SELECT ID FROM Veranstalter"); + + std::mt19937 rng(static_cast(std::time(nullptr))); // Random number generator + std::uniform_int_distribution day_dist(1, 5); // Random days between 1 and 5 (Monday to Friday) + + for (auto row : veranstaltungen) { + int veranstaltungID = row["id"].as(); + int dauer = row["dauer"].as(); + int used = row["used"].as(); + + while (used < dauer) { + // Randomly select a day + int day = day_dist(rng); + + // Randomly select a Uhrzeit + std::uniform_int_distribution uhrzeit_dist(0, uhrzeiten.size() - 1); + int uhrzeitIndex = uhrzeit_dist(rng); + int uhrzeitID = uhrzeiten[uhrzeitIndex]; + + // Randomly select a Veranstalter + std::uniform_int_distribution veranstalter_dist(0, veranstalter.size() - 1); + int veranstalterIndex = veranstalter_dist(rng); + int veranstalterID = veranstalter[veranstalterIndex]["id"].as(); + + // Check if the selected Veranstalter is valid for the selected time and location + std::string checkQuery = fmt::format( + "SELECT COUNT(*) FROM StundenImPlan sip " + "JOIN Veranstaltung v ON sip.veranstaltung_ID = v.ID " + "WHERE sip.tag = {} AND (sip.uhrzeit_ID = {} OR sip.uhrzeit_ID = {} - 1) " + "AND v.ort != (SELECT ort FROM Veranstaltung WHERE ID = {}) " + "AND sip.veranstalter_ID = {}", + day, uhrzeitID, uhrzeitID, veranstaltungID, veranstalterID); + pqxx::result checkResult = worker.exec(checkQuery); + int count = checkResult[0][0].as(); + + if (count == 0) { + // Assign to StundenImPlan + std::string query = fmt::format( + "INSERT INTO StundenImPlan (uhrzeit_ID, tag, veranstalter_ID, veranstaltung_ID) VALUES ({}, {}, {}, {})", + uhrzeitID, day, veranstalterID, veranstaltungID + ); + worker.exec(query); + + // Update used count + used += 2; + std::string updateQuery = fmt::format( + "UPDATE Veranstaltung SET used = {} WHERE ID = {}", + used, veranstaltungID + ); + worker.exec(updateQuery); + } + } + } + + worker.commit(); + fmt::print("Random schedule assigned successfully.\n"); + } catch (const std::exception& e) { + fmt::print(stderr, "Error assigning random schedule: {}\n", e.what()); + } +} + + +std::vector DBHandler::getVeranstaltung() { + std::vector results; + + try { + pqxx::work txn(connectionObject); + + // Check if StundenImPlan is empty + std::string checkQuery = "SELECT COUNT(*) FROM StundenImPlan"; + pqxx::result countResult = txn.exec(checkQuery); + int count = countResult[0][0].as(); + + if (count == 0) { + createEinsatzplan(); + } + + // Fetch data from StundenImPlan + pqxx::result stundenImPlanResult = txn.exec( + "SELECT sip.tag, u.anfangszeit, u.endzeit, v.ort, v.name AS veranstaltungsname, " + "a.name AS veranstaltername, v.raum, sip.veranstalter_ID " + "FROM StundenImPlan sip " + "JOIN Uhrzeit u ON sip.uhrzeit_ID = u.ID " + "JOIN Veranstaltung v ON sip.veranstaltung_ID = v.ID " + "JOIN Veranstalter a ON sip.veranstalter_ID = a.ID" + ); + + // Format the results + for (const auto &row : stundenImPlanResult) { + std::string result = fmt::format("{},{},{},{},{},{},{},{}", + row["tag"].as(), + row["anfangszeit"].as(), + row["endzeit"].as(), + row["ort"].as(), + row["veranstaltungsname"].as(), + row["veranstaltername"].as(), + row["raum"].as(), + row["veranstalter_id"].as()); + results.push_back(result); + } + + } catch (const std::exception& e) { + fmt::print(stderr, "Error retrieving Veranstaltung data: {}\n", e.what()); + } + + return results; +} + +void DBHandler::meldeKrank(std::string id, std::string tag, std::string stunde){ + try { + pqxx::work worker(connectionObject); + std::string query = fmt::format("Update StundeImPlan SET veranstalter_ID = NULL WHERE veranstalter_ID = {0}",id); + worker.exec(query); + worker.commit(); + fmt::print("Veranstalter with ID {} marked ill successfully.\n", id); + } + catch (const std::exception& e) { + fmt::print("ERROR: {0}", e.what( )); + } +} \ No newline at end of file diff --git a/src/Core/DBHandler/DBHandler/DBHandler.hpp b/src/Core/DBHandler/DBHandler/DBHandler.hpp index 655cfcd..4129c7e 100644 --- a/src/Core/DBHandler/DBHandler/DBHandler.hpp +++ b/src/Core/DBHandler/DBHandler/DBHandler.hpp @@ -3,11 +3,20 @@ #include #include #include +#include +#include class DBHandler { protected: pqxx::connection connectionObject; + void createEinsatzplan(); public: DBHandler(std::string connStr); + void deleteVeranstalter(std::string id); + void deleteVeranstaltung(std::string name); + void createVeranstalter(std::string email, std::string name, std::string passwort, std::string admin); + void createVeranstaltung(std::string name, std::string uhrzeit, std::string standort, std::string raum); + std::vector getVeranstaltung(); + void meldeKrank(std::string id, std::string tag, std::string stunde); }; diff --git a/src/Core/DBHandler/DBPlan/DBPlan.cpp b/src/Core/DBHandler/DBPlan/DBPlan.cpp deleted file mode 100644 index e2cacf2..0000000 --- a/src/Core/DBHandler/DBPlan/DBPlan.cpp +++ /dev/null @@ -1,539 +0,0 @@ -#include "DBPlan.hpp" - -DBPlan::DBPlan(std::string connStr) : DBHandler(connStr) { }; - -void DBPlan::vertretung(std::string tag, std::string stunde) { - try { - std::string dauer = getDauer(tag, stunde); - std::string prevStunde = std::to_string(std::stoi(stunde) - 1); - std::string cap = std::to_string(18 - std::stoi(dauer)); - std::string nextStunde; - std::string nextTag = tag; - std::string prevTag = tag; - if (dauer == "4") { - - nextStunde = std::to_string(std::stoi(stunde) + 2); - } - else { - nextStunde = std::to_string(std::stoi(stunde) + 1); - - } - - if (prevStunde == "0") { - prevStunde = "5"; - prevTag = std::to_string(std::stoi(tag) - 1); - if (prevTag == "0") { - prevTag = "5"; - } - } - - if (nextStunde == "6") { - nextStunde = "1"; - nextTag = std::to_string(std::stoi(tag)+1); - if (nextTag == "6") { - nextTag = "1"; - } - } - - pqxx::work worker(connectionObject); - pqxx::result response; - int i = 1; - while (i != 0) { - std::string query0 = - R"(SELECT Veranstalter_ID FROM Veranstalter_Veranstaltung_Uhrzeit WHERE veranstalter_ID IS NOT NULL AND uhrzeit_id = $1 AND tag = $2;)"; - response = worker.exec_params(query0, prevStunde, prevTag); - worker.commit(); - - fmt::print("{0}\n",response.size()); - if (response.size() != 0) { - i = 0; - break; - } - prevStunde = std::to_string(std::stoi(prevStunde) - 1); - if (prevStunde == "0") { - prevStunde = "5"; - prevTag = std::to_string(std::stoi(prevTag) - 1); - if (prevTag == "0") { - prevTag = "5"; - } - } - } - - - pqxx::result res; - i = 1; - while (i != 0) { - - std::string query00 = - R"(SELECT Veranstalter_ID FROM Veranstalter_Veranstaltung_Uhrzeit WHERE veranstalter_ID IS NOT NULL AND uhrzeit_id = $1 AND tag = $2;)"; - res = worker.exec_params(query00, nextStunde, nextTag); - worker.commit(); - - if (response.size() != 0) { - i = 0; - break; - } - nextStunde = std::to_string(std::stoi(nextStunde) + 1); - if (nextStunde == "6") { - nextStunde = "1"; - nextTag = std::to_string(std::stoi(nextTag) + 1); - if (nextTag == "6") { - nextTag = "1"; - } - } - } - - - std::string query = - R"(UPDATE Veranstalter_Veranstaltung_Uhrzeit SET Veranstalter_ID = - (SELECT ID FROM Veranstalter WHERE ID != (SELECT Veranstalter_ID FROM Veranstalter_Veranstaltung_Uhrzeit WHERE uhrzeit_id = $1 AND tag = $2 LIMIT 1) - AND ID != (SELECT Veranstalter_ID FROM Veranstalter_Veranstaltung_Uhrzeit WHERE uhrzeit_id = $3 AND tag = $4 LIMIT 1) - AND uhrzeit_id != $5 AND tag != $6 LIMIT 1) WHERE uhrzeit_id = $7 AND tag = $8;)"; - - response = worker.exec_params(query, prevStunde, prevTag, nextStunde, nextTag, stunde, tag, stunde, tag); - - worker.commit(); - - if (response.affected_rows() == 0) { - meldeGesund(tag, stunde); - std::string query2 = - "DELETE FROM Veranstalter_Veranstaltung_Uhrzeit WHERE tag = $1 AND uhrzeit_id = $2"; - worker.exec_params(query2, tag, stunde); - worker.commit(); - - if (dauer == "4") { - std::string query3 = - "DELETE FROM Veranstalter_Veranstaltung_Uhrzeit WHERE tag = $1 AND uhrzeit_id = $2"; - worker.exec_params(query3, tag, nextStunde); - worker.commit(); - } - } - else { - incarbeitszeit(tag, stunde, dauer); - if (dauer == "4") { - upperHour(tag, stunde); - } - } - - - } - catch (const std::exception& e) { - fmt::println(e.what( )); - - } -} - -void DBPlan::meldeKrank(std::string id, std::string tag, std::string stunde) { - try { - pqxx::work worker(connectionObject); - std::string query = - "UPDATE Veranstalter SET krank = TRUE, tag = $1, uhrzeit_id = $2 WHERE ID = $3;"; - - worker.exec_params(query, tag, stunde, id); - worker.commit( ); - - vertretung(tag, stunde); - versendeEmails( ); - } - catch (const std::exception& e) { - fmt::println(e.what( )); - } -} - -void DBPlan::meldeGesund(std::string tag, std::string stunde) { - try { - pqxx::work worker(connectionObject); - std::string query = "UPDATE Veranstalter SET krank = FALSE, uhrzeit_ID = 0, tag = 0 WHERE tag = $1 AND uhrzeit_id = $2;"; - - worker.exec_params(query, id); - worker.commit( ); - } - catch (const std::exception& e) { - fmt::println(e.what( )); - } -} - -void DBPlan::deleteVeranstalterForeign(std::string id) { - try { - pqxx::work worker(connectionObject); - std::string query = - "SELECT tag, uhrzeit_id FROM Veranstalter_Veranstaltung_Uhrzeit WHERE Veranstalter_ID = $1; "; - - pqxx::result response = worker.exec_params(query, id); - worker.commit( ); - - std::string tag, stunde; - - for (int i = 0; i < response.affected_rows( ); i++) { - tag = response[i][0].c_str( ); - stunde = response[i][1].c_str( ); - std::string query2 = - "UPDATE Veranstalter SET krank = TRUE, tag = $1, uhrzeit_id = $2 WHERE ID = $3;"; - - pqxx::result response = worker.exec_params(query, tag, stunde, id); - worker.commit( ); - vertretung(tag, stunde); - if (getDauer(tag, stunde) == "4") - i++; - } - } - catch (const std::exception& e) { - fmt::println(e.what( )); - } -} - -void DBPlan::deleteVeranstalter(std::string id) { - try { - deleteVeranstalterForeign(id); - pqxx::work worker(connectionObject); - std::string query = "DELETE FROM Veranstalter WHERE ID = $1;"; - - worker.exec_params(query, id); - worker.commit( ); - versendeEmails( ); - } - catch (const std::exception& e) { - fmt::println(e.what( )); - } -} - -void DBPlan::deleteVeranstaltung(std::string name) { - try { - pqxx::work worker(connectionObject); - std::string query = "DELETE FROM Veranstaltung WHERE name = $1;"; - - worker.exec_params(query, name); - worker.commit( ); - } - catch (const std::exception& e) { - fmt::println(e.what( )); - } -} - -void DBPlan::hinzufuegenVeranstaltung(std::string name, std::string dauer, std::string ort, std::string raum) { - try { - pqxx::work worker(connectionObject); - std::string query = - "INSERT INTO Veranstaltung (name, dauer, ort, raum) VALUES ($1, $2, $3, $4);"; - worker.exec_params(query, name, dauer, ort, raum); - worker.commit( ); - } - catch (const std::exception& e) { - fmt::println(e.what( )); - } -} - -void DBPlan::hinzufuegenVeranstalter(std::string email, std::string name, std::string pw, std::string admin) { - try { - pqxx::work worker(connectionObject); - std::string query = - "INSERT INTO Veranstalter (email, name, passwort, admin) VALUES ($1, $2, $3, $4);"; - worker.exec_params(query, email, name, pw, admin); - worker.commit( ); - } - catch (const std::exception& e) { - fmt::println(e.what( )); - } -} - -void DBPlan::deleteStudent(std::string id) { - try { - pqxx::work worker(connectionObject); - std::string query = - "DELETE FROM studenten WHERE ID = $1;"; - worker.exec_params(query, id); - worker.commit( ); - } - catch (const std::exception& e) { - fmt::println(e.what( )); - } -} - -void DBPlan::hinzufuegenStudent(std::string email, std::string name, std::string pw) { - try { - pqxx::work worker(connectionObject); - std::string query = - "INSERT INTO studenten (email, name, pw) VALUES ($1, $2, $3);"; - worker.exec_params(query, email, name, pw); - worker.commit( ); - } - catch (const std::exception& e) { - fmt::println(e.what( )); - } -} - -std::string DBPlan::getDauer(std::string tag, std::string stunde) { - try { - pqxx::work worker(connectionObject); - std::string query = - "SELECT dauer FROM Veranstaltung WHERE ID = (SELECT Veranstaltung_ID FROM Veranstalter_Veranstaltung_Uhrzeit WHERE uhrzeit_id = $1 AND tag = $2);"; - - pqxx::result response = worker.exec_params(query, stunde, tag); - - worker.commit( ); - - return response[0][0].c_str( ); - } - catch (const std::exception& e) { - fmt::println(e.what( )); - } - return ""; -} - -void DBPlan::addFirstOfDayTwo(std::string tag) { - try { - pqxx::work worker(connectionObject); - std::string query = - R"(UPDATE Veranstalter_Veranstaltung_Uhrzeit SET Veranstalter_ID = (SELECT ID FROM Veranstalter WHERE arbeitszeit <= 16 AND krank = FALSE LIMIT 1) - WHERE uhrzeit_id = 1 AND tag = $1;)"; - worker.exec_params(query, tag); - worker.commit( ); - } - catch (const std::exception& e) { - fmt::println(e.what( )); - } -} - -void DBPlan::addTwoHour(std::string tag, std::string stunde) { - try { - std::string prevStunde = std::to_string(std::stoi(stunde) - 1); - - pqxx::work worker(connectionObject); - std::string query = - R"(UPDATE Veranstalter_Veranstaltung_Uhrzeit SET Veranstalter_id = (SELECT ID FROM Veranstalter - WHERE arbeitszeit <= 16 AND krank = FALSE - AND(standort = (SELECT ort FROM Veranstaltung WHERE ID = (SELECT veranstaltung_ID FROM Veranstalter_Veranstaltung_Uhrzeit WHERE uhrzeit_id = $1 AND tag = $2)) - AND ID != (SELECT veranstalter_ID FROM Veranstalter_Veranstaltung_Uhrzeit WHERE uhrzeit_id = $1 AND tag = $2)) - ORDER BY random() LIMIT 1) - WHERE uhrzeit_id = $3 AND tag = $4;)"; - worker.exec_params(query, prevStunde, tag, stunde, tag); - worker.commit( ); - } - catch (const std::exception& e) { - fmt::println(e.what( )); - } -} - -void DBPlan::addFirstOfDayFour(std::string tag) { - try { - pqxx::work worker(connectionObject); - std::string query = - R"(UPDATE Veranstalter_Veranstaltung_Uhrzeit SET Veranstalter_ID = (SELECT ID FROM Veranstalter WHERE arbeitszeit <= 14 AND krank = FALSE LIMIT 1) - WHERE uhrzeit_id = 1 AND tag = $1;)"; - worker.exec_params(query, tag); - worker.commit( ); - } - catch (const std::exception& e) { - fmt::println(e.what( )); - } -} - -void DBPlan::addFourHour(std::string tag, std::string stunde) { - try { - std::string prevStunde = std::to_string(std::stoi(stunde) - 1); - pqxx::work worker(connectionObject); - std::string query = - R"(UPDATE Veranstalter_Veranstaltung_Uhrzeit SET Veranstalter_id = (SELECT ID FROM Veranstalter - WHERE arbeitszeit <= 14 AND krank = FALSE - AND(standort = (SELECT ort FROM Veranstaltung WHERE ID = (SELECT veranstaltung_ID FROM Veranstalter_Veranstaltung_Uhrzeit WHERE uhrzeit_id = $1 AND tag = $2)) - AND ID != (SELECT veranstalter_ID FROM Veranstalter_Veranstaltung_Uhrzeit WHERE uhrzeit_id = $1 AND tag = $2)) - ORDER BY random() LIMIT 1) - WHERE uhrzeit_id = $3 AND tag = $4;)"; - worker.exec_params(query, prevStunde, tag, stunde, tag); - worker.commit( ); - } - catch (const std::exception& e) { - fmt::println(e.what( )); - } -} - -void DBPlan::upperHour(std::string tag, std::string stunde) { - try { - std::string nextStunde = std::to_string(std::stoi(stunde) + 1); - - pqxx::work worker(connectionObject); - std::string query = - R"(UPDATE Veranstalter_Veranstaltung_Uhrzeit SET Veranstalter_ID = (SELECT Veranstalter_ID FROM Veranstalter_Veranstaltung_Uhrzeit WHERE uhrzeit_id = $1 AND tag = $2) - WHERE uhrzeit_id = $3 AND tag = $2;)"; - worker.exec_params(query, stunde, tag, nextStunde); - worker.commit( ); - } - catch (const std::exception& e) { - fmt::println(e.what( )); - } -} - -void DBPlan::updateStandort(std::string tag, std::string stunde) { - try { - pqxx::work worker(connectionObject); - std::string query = - R"(UPDATE Veranstalter SET standort = (SELECT ort FROM Veranstaltung WHERE ID = (SELECT Veranstaltung_ID FROM Veranstalter_Veranstaltung_Uhrzeit WHERE uhrzeit_id = $1 AND tag = $2)) WHERE ID = (SELECT Veranstalter_ID FROM Veranstalter_Veranstaltung_Uhrzeit WHERE uhrzeit_id = $1 AND tag = $2);)"; - worker.exec_params(query, stunde, tag); - worker.commit( ); - } - catch (const std::exception& e) { - fmt::println(e.what( )); - } -} - -void DBPlan::incarbeitszeit(std::string tag, std::string stunde, std::string amount) { - try { - pqxx::work worker(connectionObject); - - std::string query = - "UPDATE Veranstalter SET arbeitszeit = arbeitszeit + $1 WHERE ID = (SELECT Veranstalter_ID FROM Veranstalter_Veranstaltung_Uhrzeit WHERE uhrzeit_id = $2 AND tag = $3);"; - - worker.exec_params(query, amount, stunde, tag); - worker.commit( ); - } - catch (const std::exception& e) { - fmt::println(e.what( )); - } -} - -void DBPlan::insertVeranstaltungenIntoPlan(std::string cap, std::string tag, std::string stunde) { - try { - pqxx::work worker(connectionObject); - std::string query = - R"(INSERT INTO Veranstalter_Veranstaltung_Uhrzeit (Veranstaltung_ID, tag, uhrzeit_ID) - SELECT ID, $1, $2 FROM Veranstaltung WHERE dauer <= $3 - ORDER BY used ASC - LIMIT 1;)"; - - worker.exec_params(query, tag, stunde, cap); - worker.commit( ); - - std::string query2 = - R"(UPDATE Veranstaltung SET used = used+1 WHERE ID = - (SELECT Veranstaltung_ID FROM Veranstalter_Veranstaltung_Uhrzeit WHERE tag = $1 AND uhrzeit_id = $2); )"; - - worker.exec_params(query2, tag, stunde); - worker.commit( ); - } - catch (const std::exception& e) { - fmt::println(e.what( )); - } -} - -void DBPlan::insertAgain(std::string tag, std::string stunde) { - try { - std::string nextStunde = std::to_string(std::stoi(stunde) + 1); - - pqxx::work worker(connectionObject); - - std::string query = - R"(INSERT INTO Veranstalter_Veranstaltung_Uhrzeit (Veranstaltung_ID, tag, uhrzeit_ID) - SELECT Veranstaltung_ID, $1, $2 FROM Veranstalter_Veranstaltung_Uhrzeit WHERE tag = $3 AND uhrzeit_id = $4;)"; - - worker.exec_params(query, tag, nextStunde, tag, stunde); - worker.commit( ); - - } - catch (const std::exception& e) { - fmt::println(e.what( )); - } -} - -void DBPlan::insertVeranstaltungIntoPlanHandler( ) { - - int timeDay; - for (int i = 1; i <= 5; i++) { - timeDay = 1; - - while (timeDay <= 5) { - std::string tagStr = std::to_string(i); - std::string stundeStr = std::to_string(timeDay); - if (timeDay == 5) - insertVeranstaltungenIntoPlan("2", tagStr, stundeStr); - else { - insertVeranstaltungenIntoPlan("4", tagStr, stundeStr); - if (getDauer(tagStr, stundeStr) == "4") { - insertAgain(tagStr, stundeStr); - timeDay++; - } - } - timeDay++; - } - } -} - -void DBPlan::createPlan( ) { - try { - insertVeranstaltungIntoPlanHandler( ); - for (int tag = 1; tag < 6; tag++) { - std::string tagStr = std::to_string(tag); - for (int stunde = 1; stunde < 6; stunde++) { - std::string stundeStr = std::to_string(stunde); - //get dauer of next class - if (std::stoi(getDauer(tagStr, stundeStr)) == 2) { - stunde == 1 ? addFirstOfDayTwo(tagStr) : addTwoHour(tagStr, stundeStr); - updateStandort(tagStr, stundeStr); - incarbeitszeit(tagStr, stundeStr, "2"); - } else { - stunde == 1 ? addFirstOfDayFour(tagStr) : addFourHour(tagStr, stundeStr); - upperHour(tagStr, stundeStr); - incarbeitszeit(tagStr, stundeStr, "4"); - updateStandort(tagStr, stundeStr); - stunde++; - } - } - } - } - catch (const std::exception& e) { - fmt::println(e.what( )); - } -} - -std::string DBPlan::checkPlanSize( ) { - try { - pqxx::work worker(connectionObject); - std::string query = - "SELECT count(*) FROM Veranstalter_Veranstaltung_Uhrzeit;"; - pqxx::result response = worker.exec(query); - worker.commit( ); - return response[0][0].c_str( ); - } - catch (const std::exception& e) { - fmt::println(e.what( )); - } - return ""; -} - -std::vector DBPlan::getPlan( ) { - std::vector plan; - - try { - if (checkPlanSize( ) == "0") - createPlan( ); - - pqxx::work worker(connectionObject); - - std::string query = - R"(SELECT Veranstalter_Veranstaltung_Uhrzeit.tag, u.anfangszeit, u.endzeit, o.ort, o.name, v.name, o.raum, v.ID FROM Veranstalter_Veranstaltung_Uhrzeit - JOIN Veranstalter v ON Veranstalter_Veranstaltung_Uhrzeit.veranstalter_ID = v.ID - JOIN Uhrzeit u ON Veranstalter_Veranstaltung_Uhrzeit.uhrzeit_ID = u.ID - JOIN Veranstaltung o ON Veranstalter_Veranstaltung_Uhrzeit.veranstaltung_ID = o.ID - ORDER BY Veranstalter_Veranstaltung_Uhrzeit.tag, Veranstalter_Veranstaltung_Uhrzeit.uhrzeit_ID;)"; - - pqxx::result response = worker.exec(query); - worker.commit( ); - - for (const auto& row : response) { - std::string rowstring; - for (const auto& col : row) { - rowstring.append(col.c_str( )); - rowstring.append(","); - } - plan.push_back(rowstring); - } - } - catch (const std::exception& e) { - fmt::println(e.what( )); - } - return plan; -} - -void DBPlan::versendeEmails( ) { - //Email an alle Nutzer - fmt::println("Der Einsatzplan wurde geupdated"); -} diff --git a/src/Core/DBHandler/DBPlan/DBPlan.hpp b/src/Core/DBHandler/DBPlan/DBPlan.hpp deleted file mode 100644 index 965b31e..0000000 --- a/src/Core/DBHandler/DBPlan/DBPlan.hpp +++ /dev/null @@ -1,113 +0,0 @@ -#pragma once - -#include -#include -#include - -#include "../DBHandler/DBHandler.hpp" - - - -class DBPlan : public DBHandler { -private: - //Functions needed for creation and updating of the plan - std::string getDauer(std::string tag, std::string stunde); - void addFirstOfDayTwo(std::string tag); - void addTwoHour(std::string tag, std::string stunde); - void addFirstOfDayFour(std::string tag); - void upperHour(std::string tag, std::string stunde); - void addFourHour(std::string tag, std::string stunde); - void vertretung(std::string tag, std::string stunde); - void deleteVeranstalterForeign(std::string id); - void updateStandort(std::string tag, std::string stunde); - void incarbeitszeit(std::string tag, std::string stunde, std::string amount); - void insertVeranstaltungenIntoPlan(std::string cap, std::string tag, std::string stunde); - void insertVeranstaltungIntoPlanHandler( ); - void insertAgain(std::string tag, std::string stunde); - std::string checkPlanSize( ); - void versendeEmails( ); - -public: - DBPlan(std::string connStr); - - /** - * @brief Deletes prof from plan and searches new one - * - * @param id - * @param tag - * @param stunde - */ - void meldeKrank(std::string id, std::string tag, std::string stunde); - - /** - * @brief Currently directly used after meldeKrank, so Mitarbeiter is only sick for specific hour - * - * @param id - */ - //void meldeGesund(std::string tag, std::string stunde); - - /** - * @brief Deletes Veranstaltung from relation Veranstaltung and from Einsatzplan - * - * @param id - */ - void deleteVeranstaltung(std::string name); - - /** - * @brief Adds Veranstaltung to relation Veranstaltung - * - * @param name - * @param dauer - * @param ort - * @param raum - */ - void hinzufuegenVeranstaltung(std::string name, std::string dauer, std::string ort, std::string raum); - - /** - * @brief Deletes Veranstlater from relattion Veranstalter and from Einsatzplan - * - * @param id - */ - void deleteVeranstalter(std::string id); - - /** - * @brief Add Veranstalter to relation Veranstalter - * - * @param email - * @param name - * @param pw - * @param admin - */ - void hinzufuegenVeranstalter(std::string email, std::string name, std::string pw, std::string admin); - - /** - * @brief Deletes a student from the database - * - * @param id - */ - void deleteStudent(std::string id); - - /** - * @brief Adds a new student - * - * @param email - * @param name - * @param pw - */ - void hinzufuegenStudent(std::string email, std::string name, std::string pw); - - /** - * @brief Create a Plan object - * - */ - void createPlan( ); - - /** - * @brief Each string in form of (tag , anfangszeit , endzeit , Ort , Veranstaltung , Mitarbeiter , mitarbeiterID , ) - * - * @return std::vector - */ - - std::vector getPlan( ); -}; -