From fa5097da108fecb645d7cecaedd8f3c364a59212 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Sun, 23 Aug 2020 21:14:43 +0200 Subject: [PATCH] =?UTF-8?q?L=C3=B6schen=20und=20modifizieren=20von=20Freig?= =?UTF-8?q?etr=C3=A4nken?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Der Vorstand hat nun die Möglichkeit Freigetränke zu löschen oder zu ändern. Beim Löschen wird auch der gesamte Verlauf dieses Freigetränks gelöscht. --- .../dbFreeDrinkListConfigController.py | 33 +++++++++++++++++++ .../mainFreeDrinkListConfigController.py | 6 ++++ geruecht/routes.py | 24 ++++++++++++-- 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/geruecht/controller/databaseController/dbFreeDrinkListConfigController.py b/geruecht/controller/databaseController/dbFreeDrinkListConfigController.py index e7a7e3e..dbc4738 100644 --- a/geruecht/controller/databaseController/dbFreeDrinkListConfigController.py +++ b/geruecht/controller/databaseController/dbFreeDrinkListConfigController.py @@ -39,6 +39,39 @@ class Base: cursor = self.db.connection.cursor() cursor.execute(f'insert into free_drink_list_config (drink_id, label, price) values ({free_drink_list_config["drink"]["id"]}, "{free_drink_list_config["label"]}", {free_drink_list_config["price"]})') self.db.connection.commit() + cursor.execute(f'select id from free_drink_list_config where drink_id={free_drink_list_config["drink"]["id"]} and label="{free_drink_list_config["label"]}" and price={free_drink_list_config["price"]}') + data = cursor.fetchone() + for free_drink_type in free_drink_list_config["free_drink_types"]: + cursor.execute( + f'insert into free_drink_list_type_config (free_drink_list_config_id, free_drink_list_type_id) values ({data["id"]},{free_drink_type["id"]})') + self.db.connection.commit() + return self.get_free_drink_list_configs() + except Exception as err: + traceback.print_exc() + self.db.connection.rollback() + raise DatabaseExecption("Something went wrong with Database: {}".format(err)) + + def update_free_drink_list_config(self, free_drink_list_config): + try: + cursor = self.db.connection.cursor() + cursor.execute(f'update free_drink_list_config set drink_id={free_drink_list_config["drink"]["id"]}, label="{free_drink_list_config["label"]}", price={free_drink_list_config["price"]} where id={free_drink_list_config["id"]}') + cursor.execute(f'delete from free_drink_list_type_config where free_drink_list_config_id={free_drink_list_config["id"]}') + for free_drink_type in free_drink_list_config["free_drink_types"]: + cursor.execute(f'insert into free_drink_list_type_config (free_drink_list_config_id, free_drink_list_type_id) values ({free_drink_list_config["id"]},{free_drink_type["id"]})') + self.db.connection.commit() + return self.get_free_drink_list_configs() + except Exception as err: + traceback.print_exc() + self.db.connection.rollback() + raise DatabaseExecption("Something went wrong with Database: {}".format(err)) + + def delete_free_drink_list_config(self, free_drink_list_config): + try: + cursor = self.db.connection.cursor() + cursor.execute(f'delete from free_drink_list_type_config where free_drink_list_config_id={free_drink_list_config["id"]}') + cursor.execute(f'delete from free_drink_list_history where free_drink_config_id={free_drink_list_config["id"]}') + cursor.execute(f'delete from free_drink_list_config where id={free_drink_list_config["id"]}') + self.db.connection.commit() return self.get_free_drink_list_configs() except Exception as err: traceback.print_exc() diff --git a/geruecht/controller/mainController/mainFreeDrinkListConfigController.py b/geruecht/controller/mainController/mainFreeDrinkListConfigController.py index ac271e5..ffcf3e6 100644 --- a/geruecht/controller/mainController/mainFreeDrinkListConfigController.py +++ b/geruecht/controller/mainController/mainFreeDrinkListConfigController.py @@ -11,6 +11,12 @@ class Base: def set_free_drink_list_config(self, data): return db.set_free_drink_list_config(data) + def update_free_drink_list_config(self, data): + return db.update_free_drink_list_config(data) + + def delete_free_drink_list_config(self, data): + return db.delete_free_drink_list_config(data) + def set_free_drink_list_history(self, user, data): return db.set_free_drink_list_history(user, data) diff --git a/geruecht/routes.py b/geruecht/routes.py index 2208f94..e58a5bf 100644 --- a/geruecht/routes.py +++ b/geruecht/routes.py @@ -26,18 +26,38 @@ def _valid(**kwargs): debug.warning("exception in valide.", exc_info=True) return jsonify({"error": str(err)}), 500 -@app.route("/freeDrinkListConfig", methods=['GET']) +@app.route("/freeDrinkListConfig", methods=['GET', 'POST', 'PUT']) @login_required() def _free_drink_list_config(**kwargs): try: debug.info("get free_drink_list_config") - retVal = mainController.get_free_drink_list_configs() + if request.method == 'GET': + retVal = mainController.get_free_drink_list_configs() + if request.method == 'POST': + data = request.get_json() + retVal = mainController.set_free_drink_list_config(data) + if request.method == 'PUT': + data = request.get_json() + retVal = mainController.update_free_drink_list_config(data) debug.info("return free_drink_list_config {{ {} }}".format(retVal)) return jsonify(retVal) except Exception as err: debug.warning("exception in get free_dirnk_list_config.", exc_info=True) return jsonify({"error": str(err)}), 500 +@app.route("/deleteFreeDrinkListConfig", methods=['POST']) +@login_required() +def _delete_free_drink_list_config(**kwargs): + try: + debug.info("delete free_drink_list_config") + data = request.get_json() + retVal = mainController.delete_free_drink_list_config(data) + debug.info("return delete_free_drink_list_config {{ {} }}".format(retVal)) + return jsonify(retVal) + except Exception as err: + debug.warning("exception in delete_free_dirnk_list_config.", exc_info=True) + return jsonify({"error": str(err)}), 500 + @app.route("/freeDrinkListHistory", methods=['GET', 'POST', 'PUT']) @login_required() def _free_drink_list_history(**kwargs):