add mieter, refactor to getOne and getMany

This commit is contained in:
2021-01-17 00:20:42 +01:00
parent 9a907c3a8e
commit 878ff31a52
5 changed files with 153 additions and 89 deletions

View File

@ -1,46 +1,8 @@
from dbpool import getConnection
from dbpool import getConnection, getMany, getOne
def get_objekte():
try:
dbh = getConnection()
objekte = []
cur = dbh.cursor()
cur.execute("SELECT id, shortname, flaeche FROM objekt")
for (id, shortname, flaeche) in cur:
objekte.append({
"id": id,
"shortname": shortname,
"flaeche": flaeche
})
return objekte
except Exception as err:
return str(err), 500
finally:
dbh.close()
return getMany("SELECT id, shortname, flaeche FROM objekt")
def get_objekt(id=None):
try:
dbh = getConnection()
cur = dbh.cursor()
cur.execute("SELECT id, shortname, flaeche FROM objekt WHERE id = ?", (id,))
objekt = None
try:
(id, shortname, flaeche) = cur.next()
objekt = {
"id": id,
"shortname": shortname,
"flaeche": flaeche
}
except StopIteration:
return "Objekt not found", 404
try:
(id, shortname, flaeche) = cur.next()
return "More than one Objekt by that id ({}, {}, {})".format(id, shortname, flaeche), 500
except:
pass
return objekt
except Exception as err:
return str(err), 500
finally:
dbh.close()
return getOne("SELECT id, shortname, flaeche FROM objekt WHERE id = ?",
(id,), "Objekt")