From 878ff31a522f85e6414006eab941bf491e421ff3 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Sun, 17 Jan 2021 00:20:42 +0100 Subject: [PATCH] add mieter, refactor to getOne and getMany --- Mieter.py | 44 ++++++++++++++++++++++++++++++++++ Objekte.py | 46 ++++-------------------------------- Wohnungen.py | 51 ++++----------------------------------- dbpool.py | 34 ++++++++++++++++++++++++++ swagger.yaml | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 153 insertions(+), 89 deletions(-) create mode 100644 Mieter.py diff --git a/Mieter.py b/Mieter.py new file mode 100644 index 0000000..e4408bd --- /dev/null +++ b/Mieter.py @@ -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") diff --git a/Objekte.py b/Objekte.py index af9e19e..b77a85c 100644 --- a/Objekte.py +++ b/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") diff --git a/Wohnungen.py b/Wohnungen.py index d34a7d2..be3bacc 100644 --- a/Wohnungen.py +++ b/Wohnungen.py @@ -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") diff --git a/dbpool.py b/dbpool.py index e8feb53..b279611 100644 --- a/dbpool.py +++ b/dbpool.py @@ -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() + \ No newline at end of file diff --git a/swagger.yaml b/swagger.yaml index d5afe02..daa2274 100644 --- a/swagger.yaml +++ b/swagger.yaml @@ -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