add mieter, refactor to getOne and getMany
This commit is contained in:
46
Objekte.py
46
Objekte.py
@ -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")
|
||||
|
Reference in New Issue
Block a user