Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
@@ -12,8 +12,8 @@ from fastapi.responses import JSONResponse
|
|
12 |
import os
|
13 |
|
14 |
# Connect to the database
|
15 |
-
connection = pymysql.connect(host=
|
16 |
-
user='
|
17 |
password=os.environ['MURMEL_DB_PASSWORD'],
|
18 |
database='murmel',
|
19 |
cursorclass=pymysql.cursors.DictCursor)
|
@@ -23,6 +23,7 @@ app = FastAPI()
|
|
23 |
|
24 |
@app.get("/groups")
|
25 |
def get_groups():
|
|
|
26 |
with connection.cursor() as cursor:
|
27 |
# Read a single record
|
28 |
sql = "SELECT Gruppe, idGruppe FROM `gruppe` WHERE aktuell is True ORDER BY idGruppe ASC;"
|
@@ -33,6 +34,7 @@ def get_groups():
|
|
33 |
|
34 |
@app.get("/students/{idGruppe}")
|
35 |
def get_students(idGruppe):
|
|
|
36 |
with connection.cursor() as cursor:
|
37 |
#sql = "SELECT Gruppe, idGruppe FROM `gruppe` WHERE idGruppe=%s ORDER BY name ASC;"
|
38 |
sql = "select concat(`ki`.`Vorname`,' ',`ki`.`Nachnamen`) AS `Kind`, `ki`.`idKind` AS `idKind` from (((`kind` `ki` join `gruppe` `gr`) join `kind_x_gruppe_x_schuljahr` `kgs`) join `schuljahr` `sch`) where `ki`.`idKind` = `kgs`.`x_kind` and `sch`.`idschuljahr` = `kgs`.`x_schuljahr` and `gr`.`idGruppe` = `kgs`.`x_gruppe` and `sch`.`aktuell` = 1 and (`gr`.`Gruppe` = %s or `gr`.`idGruppe` = %s) order by 1"
|
@@ -54,6 +56,7 @@ def rueckgabe(idBuch, grund='rueckgabe'):
|
|
54 |
- int: 0 if the book was not found or already returned.
|
55 |
|
56 |
"""
|
|
|
57 |
with connection.cursor() as cursor:
|
58 |
# check if the book is already returned
|
59 |
sql = "SELECT `idBuch`, `idKind`, `ausleihe`, `rueckgabe`, `rueckGrund` FROM `ausleihe` WHERE `idBuch` = %s AND `rueckgabe` is NULL;"
|
@@ -83,6 +86,7 @@ def ausleihe(idBuch, idKind):
|
|
83 |
"""
|
84 |
rueckgabe_result = rueckgabe(idBuch, grund="neu-ausleihe") # Buch kann nicht durch andere ausgeliehen sein
|
85 |
sql = "INSERT INTO `ausleihe` (`idBuch`, `idKind`, `ausleihe`) VALUES (%s, %s, NOW());"
|
|
|
86 |
with connection.cursor() as cursor:
|
87 |
cursor.execute(sql, (idBuch, idKind))
|
88 |
connection.commit()
|
@@ -101,6 +105,7 @@ def ausgeliehen(idKind):
|
|
101 |
Returns:
|
102 |
list: A list of tuples containing the book ID and the borrowing date for each book that is currently borrowed by the child.
|
103 |
"""
|
|
|
104 |
with connection.cursor() as cursor:
|
105 |
sql = "SELECT `idBuch`, `ausleihe` FROM `ausleihe` WHERE `idKind` = %s AND `rueckgabe` IS NULL;"
|
106 |
cursor.execute(sql, (idKind,))
|
|
|
12 |
import os
|
13 |
|
14 |
# Connect to the database
|
15 |
+
connection = pymysql.connect(host=os.environ['MURMEL_DB_HOST'],
|
16 |
+
user=os.environ['MURMEL_DB_USER'],
|
17 |
password=os.environ['MURMEL_DB_PASSWORD'],
|
18 |
database='murmel',
|
19 |
cursorclass=pymysql.cursors.DictCursor)
|
|
|
23 |
|
24 |
@app.get("/groups")
|
25 |
def get_groups():
|
26 |
+
connection.ping(reconnect=True)
|
27 |
with connection.cursor() as cursor:
|
28 |
# Read a single record
|
29 |
sql = "SELECT Gruppe, idGruppe FROM `gruppe` WHERE aktuell is True ORDER BY idGruppe ASC;"
|
|
|
34 |
|
35 |
@app.get("/students/{idGruppe}")
|
36 |
def get_students(idGruppe):
|
37 |
+
connection.ping(reconnect=True)
|
38 |
with connection.cursor() as cursor:
|
39 |
#sql = "SELECT Gruppe, idGruppe FROM `gruppe` WHERE idGruppe=%s ORDER BY name ASC;"
|
40 |
sql = "select concat(`ki`.`Vorname`,' ',`ki`.`Nachnamen`) AS `Kind`, `ki`.`idKind` AS `idKind` from (((`kind` `ki` join `gruppe` `gr`) join `kind_x_gruppe_x_schuljahr` `kgs`) join `schuljahr` `sch`) where `ki`.`idKind` = `kgs`.`x_kind` and `sch`.`idschuljahr` = `kgs`.`x_schuljahr` and `gr`.`idGruppe` = `kgs`.`x_gruppe` and `sch`.`aktuell` = 1 and (`gr`.`Gruppe` = %s or `gr`.`idGruppe` = %s) order by 1"
|
|
|
56 |
- int: 0 if the book was not found or already returned.
|
57 |
|
58 |
"""
|
59 |
+
connection.ping(reconnect=True)
|
60 |
with connection.cursor() as cursor:
|
61 |
# check if the book is already returned
|
62 |
sql = "SELECT `idBuch`, `idKind`, `ausleihe`, `rueckgabe`, `rueckGrund` FROM `ausleihe` WHERE `idBuch` = %s AND `rueckgabe` is NULL;"
|
|
|
86 |
"""
|
87 |
rueckgabe_result = rueckgabe(idBuch, grund="neu-ausleihe") # Buch kann nicht durch andere ausgeliehen sein
|
88 |
sql = "INSERT INTO `ausleihe` (`idBuch`, `idKind`, `ausleihe`) VALUES (%s, %s, NOW());"
|
89 |
+
connection.ping(reconnect=True)
|
90 |
with connection.cursor() as cursor:
|
91 |
cursor.execute(sql, (idBuch, idKind))
|
92 |
connection.commit()
|
|
|
105 |
Returns:
|
106 |
list: A list of tuples containing the book ID and the borrowing date for each book that is currently borrowed by the child.
|
107 |
"""
|
108 |
+
connection.ping(reconnect=True)
|
109 |
with connection.cursor() as cursor:
|
110 |
sql = "SELECT `idBuch`, `ausleihe` FROM `ausleihe` WHERE `idKind` = %s AND `rueckgabe` IS NULL;"
|
111 |
cursor.execute(sql, (idKind,))
|