From 155ce5c9d58f3bc4e059d5ae1c1380a09849395f Mon Sep 17 00:00:00 2001 From: "Manuel Weiser (aider)" Date: Tue, 3 Sep 2024 14:29:21 +0200 Subject: [PATCH] feat: add delete_game route to remove game and its associated image file --- game_collection/app.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/game_collection/app.py b/game_collection/app.py index d5cef03..96e10fa 100644 --- a/game_collection/app.py +++ b/game_collection/app.py @@ -73,6 +73,23 @@ def get_games(): }) return jsonify(games_list) +@app.route('/games/', methods=['DELETE']) +def delete_game(game_id): + if not authenticate(): + return jsonify({'message': 'Unauthorized access!'}), 401 + game = Game.query.get(game_id) + if not game: + return jsonify({'message': 'Game not found!'}), 404 + + # Delete the image file + image_path = os.path.join(app.instance_path, game.image) + if os.path.exists(image_path): + os.remove(image_path) + + db.session.delete(game) + db.session.commit() + return jsonify({'message': 'Game deleted!'}), 200 + @app.route('/games/search', methods=['GET']) def search_game_api(): if not authenticate(): # Ensure the user is authenticated