From 5d161c7443a754a30aa47edb5471773ffdb9203a Mon Sep 17 00:00:00 2001 From: Chris-bot374 Date: Sun, 30 Jun 2024 20:30:54 +0200 Subject: [PATCH] Add files via upload --- DBHandler/DBHandler.cpp | 25 ++++++++++++++++++ DBHandler/DBHandler.hpp | 14 +++++++++++ DBHandler/DBLogin.cpp | 40 +++++++++++++++++++++++++++++ DBHandler/DBLogin.hpp | 18 +++++++++++++ DBHandler/DBPlan.cpp | 56 +++++++++++++++++++++++++++++++++++++++++ DBHandler/DBPlan.hpp | 28 +++++++++++++++++++++ script.sql | 54 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 235 insertions(+) create mode 100644 DBHandler/DBHandler.cpp create mode 100644 DBHandler/DBHandler.hpp create mode 100644 DBHandler/DBLogin.cpp create mode 100644 DBHandler/DBLogin.hpp create mode 100644 DBHandler/DBPlan.cpp create mode 100644 DBHandler/DBPlan.hpp create mode 100644 script.sql diff --git a/DBHandler/DBHandler.cpp b/DBHandler/DBHandler.cpp new file mode 100644 index 0000000..58d50ab --- /dev/null +++ b/DBHandler/DBHandler.cpp @@ -0,0 +1,25 @@ +#include "DBHandler.hpp" + +#include +//host = localhost port = 5432 dbname = postgres user = postgres password = **** + + +DBHandler::DBHandler(std::string connStr) : connectionObject(connStr.c_str()) { + try { + if (connectionObject.is_open()) { + std::cout << "Databased connected" << std::endl; + } + else + std::cout << "Failed to connect to Databased" << std::endl; + } + catch (const std::exception& e) { + std::cerr << "Error: " << e.what() << std::endl; + } +}; + + + + + + + diff --git a/DBHandler/DBHandler.hpp b/DBHandler/DBHandler.hpp new file mode 100644 index 0000000..71f9db7 --- /dev/null +++ b/DBHandler/DBHandler.hpp @@ -0,0 +1,14 @@ +#ifndef DBHANDLER_HPP_ +#define DBHANDLER_HPP_ +#include +#include + +class DBHandler { +protected: + pqxx::connection connectionObject; + +public: + DBHandler(std::string connStr); +}; + +#endif \ No newline at end of file diff --git a/DBHandler/DBLogin.cpp b/DBHandler/DBLogin.cpp new file mode 100644 index 0000000..e942725 --- /dev/null +++ b/DBHandler/DBLogin.cpp @@ -0,0 +1,40 @@ +#include "DBLogin.hpp" +#include + +DBLogin::DBLogin(std::string connStr) : DBHandler(connStr) {}; + + + +/* + +return 1 if admin | 0 if not admin | -1 if failed + +*/ + +int DBLogin::checkValidLogin(int id, std::string pw) { + + try { + pqxx::work worker(connectionObject); + + std::string query = + "SELECT admin FROM studenten_veranstalterr WHERE id = $1 AND passwort = $2"; + + pqxx::result response = worker.exec_params(query, id, pw); + + if (response.affected_rows() > 0) { + + std::cout << response[0][0] << std::endl; + if (response[0][0].is_null()) + return 0; + return response[0][0].as(); + + } + + return -1; +} +catch (const std::exception& e) { + std::cerr << "Error: " << e.what() << std::endl; +} + +} + diff --git a/DBHandler/DBLogin.hpp b/DBHandler/DBLogin.hpp new file mode 100644 index 0000000..f29e0c0 --- /dev/null +++ b/DBHandler/DBLogin.hpp @@ -0,0 +1,18 @@ +#ifndef _DBLOGIN_HPP_ +#define _DBLOGIN_HPP_ +#include "DBHandler.hpp" + +class DBLogin : public DBHandler { +public: + DBLogin(std::string connStr); + /* + +return 1 if admin | 0 if not admin | -1 if failed + +*/ + int checkValidLogin(int id, std::string pw); +}; + + + +#endif \ No newline at end of file diff --git a/DBHandler/DBPlan.cpp b/DBHandler/DBPlan.cpp new file mode 100644 index 0000000..f3d4284 --- /dev/null +++ b/DBHandler/DBPlan.cpp @@ -0,0 +1,56 @@ +#include "DBPlan.hpp" + +DBPlan::DBPlan(std::string connStr) : DBHandler(connStr) {}; + + +void DBPlan::meldeKrank(int id) { + + try { + pqxx::work worker(connectionObject); + std::string query = + "INSERT INTO krank (veranstalter_ID) VALUES($1);"; + + pqxx::result response = worker.exec_params(query, id); + worker.commit(); + } + catch (const std::exception& e) { + std::cerr << "Error: " << e.what() << std::endl; + + } + } + + + void DBPlan::meldeGesund(int id) { + try { + pqxx::work worker(connectionObject); + std::string query = + "DELETE FROM krank WHERE veranstalter_ID = $1 ;"; + + pqxx::result response = worker.exec_params(query, id); + worker.commit(); + } + catch (const std::exception& e) { + std::cerr << "Error: " << e.what() << std::endl; + } + } + + + + + + void DBPlan:: deleteVeranstalter(int id) { + try { + pqxx::work worker(connectionObject); + std::string query = + "DELETE FROM Veranstalterrrr WHERE ID = $1"; + + pqxx::result response = worker.exec_params(query, id); + worker.commit(); + } + catch (const std::exception& e) { + std::cerr << "Error: " << e.what() << std::endl; + } + } + + +}; \ No newline at end of file diff --git a/DBHandler/DBPlan.hpp b/DBHandler/DBPlan.hpp new file mode 100644 index 0000000..e138c1d --- /dev/null +++ b/DBHandler/DBPlan.hpp @@ -0,0 +1,28 @@ +#ifndef _DBPLAN_HPP_ +#define _DBPLAN_HPP_ + + +#include "DBHandler.hpp" +#include +#include + +class DBPlan : public DBHandler { +public: + DBPlan(std::string connStr); + void meldeKrank(int id); + void meldeGesund(int id); + bool deleteVeranstaltung(int id); + bool hinzufuegenVeranstaltung(); //Zu Liste mit Veranstaltungen oder direkt in den Einsatzplan? + + + void deleteVeranstalter(int id); + bool hinzufuegenVeranstalter(std::string email, std::string name, std::string pw, bool admin); + std::vector < std::vector> getVeranstalter(); + + bool deleteStudent(int id); + bool hinzufuegenStudent(std::string email, std::string name, std::string pw); + std::vector < std::vector> getStudenten(); + +}; + +#endif \ No newline at end of file diff --git a/script.sql b/script.sql new file mode 100644 index 0000000..c0a9f72 --- /dev/null +++ b/script.sql @@ -0,0 +1,54 @@ +CREATE SEQUENCE global_id_seq; + +CREATE TABLE Studenten ( + matrikelnummer INTEGER PRIMARY KEY DEFAULT nextval('global_id_seq'), + "name VARCHAR(30) NOT NULL, + "email VARCHAR(30) NOT NULL, + "passwort VARCHAR(30) NOT NULL); + + + "CREATE TABLE Veranstalter ( + ID INTEGER PRIMARY KEY DEFAULT nextval('global_id_seq'), + name VARCHAR(30), + email VARCHAR(30), + passwort VARCHAR(30), + admin BOOLEAN NOT NULL DEFAULT(FALSE) + + ); + + +CREATE TABLE krank ( + veranstalter_ID INTEGER, + FOREIGN KEY (veranstalter_ID) REFERENCES veranstalterrrr(ID) ON DELETE CASCADE); + + + + CREATE VIEW studenten_veranstalterr AS + SELECT matrikelnummer AS id, passwort, NULL AS admin FROM Studenten + UNION ALL + SELECT ID, passwort, admin FROM Veranstalterrrr; + + + + CREATE TABLE Veranstaltung ( + ID SERIAL PRIMARY KEY, + ort VARCHAR(30) NOT NULL, + raum VARCHAR(30) NOT NULL, + name VARCHAR(30) NOT NULL ); + + + + + CREATE TABLE Uhrzeit ( + ID SERIAL PRIMARY KEY, + anfangszeit TIME NOT NULL, + endzeit TIME NOT NULL ); + + + + INSERT INTO Uhrzeit (anfangszeit, endzeit) VALUES + ('08:00:00', '10:00:00'), + ('10:00:00', '12:00:00'), + ('12:00:00', '14:00:00'), + ('14:00:00', '16:00:00'), + ('16:00:00', '18:00:00'); \ No newline at end of file