hv-service/Wohnungen.py
2021-01-16 19:47:34 +01:00

68 lines
1.8 KiB
Python

from dbpool import getConnection
def get_wohnungen():
try:
dbh = getConnection()
wohnungen = []
cur = dbh.cursor()
cur.execute("""
SELECT w.id as id,
w.objekt as objekt_id,
w.shortname as wohnung,
w.flaeche as flaeche,
o.shortname as objekt
FROM wohnung w, objekt o
WHERE o.id = w.objekt
""")
for (id, objekt_id, wohnung, flaeche, objekt) in cur:
wohnungen.append({
"id": id,
"objekt_id": objekt_id,
"wohnung": wohnung,
"flaeche": flaeche,
"objekt": objekt
})
return wohnungen
except Exception as err:
return str(err), 500
finally:
dbh.close()
def get_wohnung(id=None):
try:
dbh = getConnection()
cur = dbh.cursor()
cur.execute("""
SELECT w.id as id,
w.objekt as objekt_id,
w.shortname as wohnung,
w.flaeche as flaeche,
o.shortname as objekt
FROM wohnung w, objekt o
WHERE o.id = w.objekt AND
w.id = ?
""", (id, ))
wohnung = None
try:
(id, objekt_id, wohnung_name, flaeche, objekt_name) = cur.next()
wohnung = {
"id": id,
"objekt_id": objekt_id,
"wohnung": wohnung_name,
"flaeche": flaeche,
"objekt": objekt_name
}
except StopIteration:
return "Wohnung not found", 404
try:
(id, objekt_id, wohnung_name, flaeche, objekt_name) = cur.next()
return "More than one Wohnung by that id ({}, {}, {}, {})".format(id, objekt_name, wohnung_name, flaeche), 500
except:
pass
return wohnung
except Exception as err:
return str(err), 500
finally:
dbh.close()