hoffentlich klappts
This commit is contained in:
@@ -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)
|
||||
)
|
||||
|
||||
|
||||
@@ -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( ));
|
||||
}
|
||||
|
||||
@@ -3,9 +3,8 @@
|
||||
#include <QString>
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include "../../Core/DBHandler/DBPlan/DBPlan.hpp"
|
||||
#include "../../Core/DBHandler/DBHandler/DBHandler.hpp"
|
||||
#include "../../Core/config/config.hpp"
|
||||
#include "../PlanGridController/PlanGridController.hpp"
|
||||
|
||||
class EinsatzplanFrameController {
|
||||
private:
|
||||
|
||||
@@ -28,9 +28,9 @@ PlanGridController::PlanGridController( ) {
|
||||
}
|
||||
|
||||
QMap<QPair<QString, QString>, QWidget*>* PlanGridController::getVeranstaltungen( ) {
|
||||
DBPlan* db = new DBPlan(m_connectionString);
|
||||
DBHandler* db = new DBHandler(m_connectionString);
|
||||
|
||||
std::vector<std::string> planData = db->getPlan( );
|
||||
std::vector<std::string> planData = db->getVeranstaltung( );
|
||||
|
||||
for (int i = 0; i < 5; ++i)
|
||||
for (int j = 0; j < 5; ++j) {
|
||||
@@ -128,7 +128,7 @@ QMap<QPair<QString, QString>, 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));
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include <QPushButton>
|
||||
|
||||
#include "../../Core/config/config.hpp"
|
||||
#include "../../Core/DBHandler/DBPlan/DBPlan.hpp"
|
||||
#include "../../Core/DBHandler/DBHandler/DBHandler.hpp"
|
||||
|
||||
class PlanGridController : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
@@ -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<int> 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<unsigned int>(std::time(nullptr))); // Random number generator
|
||||
std::uniform_int_distribution<int> day_dist(1, 5); // Random days between 1 and 5 (Monday to Friday)
|
||||
|
||||
for (auto row : veranstaltungen) {
|
||||
int veranstaltungID = row["id"].as<int>();
|
||||
int dauer = row["dauer"].as<int>();
|
||||
int used = row["used"].as<int>();
|
||||
|
||||
while (used < dauer) {
|
||||
// Randomly select a day
|
||||
int day = day_dist(rng);
|
||||
|
||||
// Randomly select a Uhrzeit
|
||||
std::uniform_int_distribution<int> uhrzeit_dist(0, uhrzeiten.size() - 1);
|
||||
int uhrzeitIndex = uhrzeit_dist(rng);
|
||||
int uhrzeitID = uhrzeiten[uhrzeitIndex];
|
||||
|
||||
// Randomly select a Veranstalter
|
||||
std::uniform_int_distribution<int> veranstalter_dist(0, veranstalter.size() - 1);
|
||||
int veranstalterIndex = veranstalter_dist(rng);
|
||||
int veranstalterID = veranstalter[veranstalterIndex]["id"].as<int>();
|
||||
|
||||
// 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<int>();
|
||||
|
||||
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<std::string> DBHandler::getVeranstaltung() {
|
||||
std::vector<std::string> 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<int>();
|
||||
|
||||
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<int>(),
|
||||
row["anfangszeit"].as<std::string>(),
|
||||
row["endzeit"].as<std::string>(),
|
||||
row["ort"].as<std::string>(),
|
||||
row["veranstaltungsname"].as<std::string>(),
|
||||
row["veranstaltername"].as<std::string>(),
|
||||
row["raum"].as<std::string>(),
|
||||
row["veranstalter_id"].as<int>());
|
||||
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( ));
|
||||
}
|
||||
}
|
||||
@@ -3,11 +3,20 @@
|
||||
#include <pqxx/pqxx>
|
||||
#include <string>
|
||||
#include <fmt/core.h>
|
||||
#include <random>
|
||||
#include <ctime>
|
||||
|
||||
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<std::string> getVeranstaltung();
|
||||
void meldeKrank(std::string id, std::string tag, std::string stunde);
|
||||
};
|
||||
|
||||
@@ -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<std::string> DBPlan::getPlan( ) {
|
||||
std::vector<std::string> 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");
|
||||
}
|
||||
@@ -1,113 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <fmt/core.h>
|
||||
#include <fmt/printf.h>
|
||||
|
||||
#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::string>
|
||||
*/
|
||||
|
||||
std::vector<std::string> getPlan( );
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user