add mieter, refactor to getOne and getMany
This commit is contained in:
44
Mieter.py
Normal file
44
Mieter.py
Normal 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")
|
46
Objekte.py
46
Objekte.py
@ -1,46 +1,8 @@
|
|||||||
from dbpool import getConnection
|
from dbpool import getConnection, getMany, getOne
|
||||||
|
|
||||||
def get_objekte():
|
def get_objekte():
|
||||||
try:
|
return getMany("SELECT id, shortname, flaeche FROM objekt")
|
||||||
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()
|
|
||||||
|
|
||||||
|
|
||||||
def get_objekt(id=None):
|
def get_objekt(id=None):
|
||||||
try:
|
return getOne("SELECT id, shortname, flaeche FROM objekt WHERE id = ?",
|
||||||
dbh = getConnection()
|
(id,), "Objekt")
|
||||||
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()
|
|
||||||
|
51
Wohnungen.py
51
Wohnungen.py
@ -1,11 +1,7 @@
|
|||||||
from dbpool import getConnection
|
from dbpool import getConnection, getOne, getMany
|
||||||
|
|
||||||
def get_wohnungen():
|
def get_wohnungen():
|
||||||
try:
|
return getMany("""
|
||||||
dbh = getConnection()
|
|
||||||
wohnungen = []
|
|
||||||
cur = dbh.cursor()
|
|
||||||
cur.execute("""
|
|
||||||
SELECT w.id as id,
|
SELECT w.id as id,
|
||||||
w.objekt as objekt_id,
|
w.objekt as objekt_id,
|
||||||
w.shortname as wohnung,
|
w.shortname as wohnung,
|
||||||
@ -14,26 +10,9 @@ SELECT w.id as id,
|
|||||||
FROM wohnung w, objekt o
|
FROM wohnung w, objekt o
|
||||||
WHERE o.id = w.objekt
|
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):
|
def get_wohnung(id=None):
|
||||||
try:
|
return getOne("""
|
||||||
dbh = getConnection()
|
|
||||||
cur = dbh.cursor()
|
|
||||||
cur.execute("""
|
|
||||||
SELECT w.id as id,
|
SELECT w.id as id,
|
||||||
w.objekt as objekt_id,
|
w.objekt as objekt_id,
|
||||||
w.shortname as wohnung,
|
w.shortname as wohnung,
|
||||||
@ -42,26 +21,4 @@ SELECT w.id as id,
|
|||||||
FROM wohnung w, objekt o
|
FROM wohnung w, objekt o
|
||||||
WHERE o.id = w.objekt AND
|
WHERE o.id = w.objekt AND
|
||||||
w.id = ?
|
w.id = ?
|
||||||
""", (id, ))
|
""", (id, ), "Wohnung")
|
||||||
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()
|
|
||||||
|
34
dbpool.py
34
dbpool.py
@ -26,3 +26,37 @@ def createConnectionPool():
|
|||||||
def getConnection():
|
def getConnection():
|
||||||
global pool
|
global pool
|
||||||
return pool.get_connection()
|
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()
|
||||||
|
|
67
swagger.yaml
67
swagger.yaml
@ -74,6 +74,41 @@ paths:
|
|||||||
description: Wohnung not found
|
description: Wohnung not found
|
||||||
500:
|
500:
|
||||||
description: Some server error
|
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:
|
definitions:
|
||||||
Objekt:
|
Objekt:
|
||||||
@ -100,3 +135,35 @@ definitions:
|
|||||||
type: number
|
type: number
|
||||||
objekt:
|
objekt:
|
||||||
type: string
|
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
|
||||||
|
Reference in New Issue
Block a user