Add files via upload

This commit is contained in:
Chris-bot374
2024-06-30 20:30:54 +02:00
committed by GitHub
parent 5f4c6ba3b0
commit 5d161c7443
7 changed files with 235 additions and 0 deletions

25
DBHandler/DBHandler.cpp Normal file
View File

@@ -0,0 +1,25 @@
#include "DBHandler.hpp"
#include <iostream>
//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;
}
};

14
DBHandler/DBHandler.hpp Normal file
View File

@@ -0,0 +1,14 @@
#ifndef DBHANDLER_HPP_
#define DBHANDLER_HPP_
#include <pqxx/pqxx>
#include <string>
class DBHandler {
protected:
pqxx::connection connectionObject;
public:
DBHandler(std::string connStr);
};
#endif

40
DBHandler/DBLogin.cpp Normal file
View File

@@ -0,0 +1,40 @@
#include "DBLogin.hpp"
#include <iostream>
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<bool>();
}
return -1;
}
catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
}

18
DBHandler/DBLogin.hpp Normal file
View File

@@ -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

56
DBHandler/DBPlan.cpp Normal file
View File

@@ -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;
}
}
};

28
DBHandler/DBPlan.hpp Normal file
View File

@@ -0,0 +1,28 @@
#ifndef _DBPLAN_HPP_
#define _DBPLAN_HPP_
#include "DBHandler.hpp"
#include <iostream>
#include <vector>
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<std::string>> getVeranstalter();
bool deleteStudent(int id);
bool hinzufuegenStudent(std::string email, std::string name, std::string pw);
std::vector < std::vector<std::string>> getStudenten();
};
#endif

54
script.sql Normal file
View File

@@ -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');