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

44
Mieter.py Normal file
View File

@ -0,0 +1,44 @@
from dbpool import getConnection, getMany, getOne
def get_mieters():
return getMany("""
SELECT m.id as id,
o.id as objekt_id,
w.id as wohnung_id,
w.shortname as wohnung,
o.shortname as objekt,
COALESCE(m.anrede, '-') as anrede,
COALESCE(m.vorname, '-') as vorname,
m.nachname as nachname,
COALESCE(m.strasse, '-') as strasse,
COALESCE(m.plz, '-') as plz,
COALESCE(m.ort, '-') as ort,
COALESCE(m.telefon, '-') as telefon,
m.einzug as einzug,
COALESCE(m.auszug, '-') as auszug
FROM wohnung w, objekt o, mieter m
WHERE o.id = w.objekt AND
w.id = m.wohnung
""")
def get_mieter(id=None):
return getOne("""
SELECT m.id as id,
o.id as objekt_id,
w.id as wohnung_id,
w.shortname as wohnung,
o.shortname as objekt,
COALESCE(m.anrede, '-') as anrede,
COALESCE(m.vorname, '-') as vorname,
m.nachname as nachname,
COALESCE(m.strasse, '-') as strasse,
COALESCE(m.plz, '-') as plz,
COALESCE(m.ort, '-') as ort,
COALESCE(m.telefon, '-') as telefon,
m.einzug as einzug,
COALESCE(m.auszug, '-') as auszug
FROM wohnung w, objekt o, mieter m
WHERE o.id = w.objekt AND
w.id = m.wohnung AND
m.id = ?
""", (id, ), "Mieter")

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")

View File

@ -1,11 +1,7 @@
from dbpool import getConnection
from dbpool import getConnection, getOne, getMany
def get_wohnungen():
try:
dbh = getConnection()
wohnungen = []
cur = dbh.cursor()
cur.execute("""
return getMany("""
SELECT w.id as id,
w.objekt as objekt_id,
w.shortname as wohnung,
@ -14,26 +10,9 @@ SELECT w.id as id,
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("""
return getOne("""
SELECT w.id as id,
w.objekt as objekt_id,
w.shortname as wohnung,
@ -42,26 +21,4 @@ SELECT w.id as id,
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()
""", (id, ), "Wohnung")

View File

@ -26,3 +26,37 @@ def createConnectionPool():
def getConnection():
global pool
return pool.get_connection()
def getMany(stmt):
try:
dbh = getConnection()
objs = []
cur = dbh.cursor(dictionary=True)
cur.execute(stmt)
for obj in cur:
objs.append(obj)
return objs
except Exception as err:
return str(err), 500
finally:
dbh.close()
def getOne(stmt, params, objName):
try:
dbh = getConnection()
cur = dbh.cursor(dictionary=True)
cur.execute(stmt, params)
obj = cur.next()
if not obj:
return "{} not found".format(objName), 404
invObj = cur.next()
if invObj:
return "More than one {} by that id ({}, {})".format(objName, id, invObj), 500
return obj
except Exception as err:
return str(err), 500
finally:
dbh.close()

View File

@ -74,6 +74,41 @@ paths:
description: Wohnung not found
500:
description: Some server error
/hv/mieters:
get:
tags: [ "Mieter" ]
operationId: Mieter.get_mieters
summary: Returns all Mieter
responses:
200:
description: Successful response.
schema:
type: array
items:
$ref: '#/definitions/Mieter'
404:
description: No Mieter available
500:
description: Some server error
/hv/mieter/{id}:
get:
tags: [ "Mieter" ]
operationId: Mieter.get_mieter
summary: Returns Mieter by id
parameters:
- name: id
in: path
type: integer
required: true
responses:
200:
description: Successful response.
schema:
$ref: '#/definitions/Mieter'
404:
description: Mieter not found
500:
description: Some server error
definitions:
Objekt:
@ -100,3 +135,35 @@ definitions:
type: number
objekt:
type: string
Mieter:
description: Mieter type
type: object
properties:
id:
type: integer
objekt_id:
type: integer
wohnung_id:
type: integer
wohnung:
type: string
objekt:
type: string
anrede:
type: string
vorname:
type: string
nachname:
type: string
strasse:
type: string
plz:
type: string
ort:
type: string
telefon:
type: string
einzug:
type: string
auszug:
type: string