From 42635e228171029aa230d43c73df63a4fc22451a Mon Sep 17 00:00:00 2001 From: tylen Date: Thu, 25 Sep 2025 00:50:48 +0300 Subject: [PATCH] backend: add CRUD for car --- backend/docker-compose.yaml | 8 ---- backend/src/car.py | 93 ++++++++++++++++++++++++++++++++++++ backend/src/server.py | 53 +------------------- database/.gitignore | 1 - database/docker-compose.yaml | 14 ------ docker-compose.yaml | 23 +++++++++ 6 files changed, 118 insertions(+), 74 deletions(-) delete mode 100644 backend/docker-compose.yaml create mode 100644 backend/src/car.py delete mode 100644 database/.gitignore delete mode 100644 database/docker-compose.yaml create mode 100644 docker-compose.yaml diff --git a/backend/docker-compose.yaml b/backend/docker-compose.yaml deleted file mode 100644 index 93cc26d..0000000 --- a/backend/docker-compose.yaml +++ /dev/null @@ -1,8 +0,0 @@ -services: - backend: - build: - context: . - dockerfile: Dockerfile - ports: - - "2027:5000" - container_name: "${CONTAINER_NAME:-nyi-backend}" diff --git a/backend/src/car.py b/backend/src/car.py new file mode 100644 index 0000000..4b4bac3 --- /dev/null +++ b/backend/src/car.py @@ -0,0 +1,93 @@ +#!/usr/bin/env python +# encoding: utf-8 + +''' +car.py is a source for all car endpoints. +''' + +from flask import request, jsonify + +def registerCarEndpoints(app, database): + @app.route('/car', methods=['GET']) + def get_car(): + if not request.is_json: + return jsonify({'error': 'Request must contain JSON data'}), 400 + + data = request.get_json() + + if not data.get('name'): + return jsonify({'error': 'Request must contain name field'}), 400 + + query = f'SELECT * from car WHERE name = %s' + output = database.query(query_str=query, params=(data['name'],)) + if not output: + return jsonify({"message": "No car by that name exist"}), 404 + car = output[0] + if len(car) != 3: + return jsonify({'error': 'Car data is corrupted'}), 500 + + response = { + "name": car[0], + "car": car[1], + "freeCarSpaces": car[2] + } + return jsonify(response), 200 + + @app.route('/car', methods=['POST']) + def add_car(): + if not request.is_json: + return jsonify({'error': 'Request must contain JSON data'}), 400 + + data = request.get_json() + if not data.get('name') or not data.get('car') or data.get('spaces') is None: + return jsonify({'error': 'JSON must contain car and name fields'}), 400 + + query = 'SELECT * from car WHERE name = %s' + output = database.query(query_str=query, params=(data['name'],)) + if output: + return jsonify({'error': 'A person has a car already'}), 409 + + query = 'INSERT into car (Name, Car, FreeCarSpaces) VALUES (%s, %s, %s)' + output = database.query(query_str=query, params=(data['name'],data['car'],data['spaces'])) + + database.commit() + return jsonify({"message": "car added", "car": data}), 200 + + @app.route('/car', methods=['UPDATE']) + def update_car(): + if not request.is_json: + return jsonify({'error': 'Request must contain JSON data'}), 400 + + data = request.get_json() + if not data.get('name') or not data.get('car') or data.get('spaces') is None: + return jsonify({'error': 'JSON must contain car,name,space fields'}), 400 + + query = 'SELECT * from car WHERE name = %s' + output = database.query(query_str=query, params=(data['name'],)) + if not output: + return jsonify({'error': 'Such car does not exist. Add it first'}), 409 + + query = 'UPDATE car SET Name = %s, Car = %s, FreeCarSpaces = %s' + output = database.query(query_str=query, params=(data['name'],data['car'],data['spaces'])) + + database.commit() + return jsonify({"message": "car modified", "car": data}), 200 + + @app.route('/car', methods=['DELETE']) + def delete_car(): + if not request.is_json: + return jsonify({'error': 'Request must contain JSON data'}), 400 + + data = request.get_json() + if not data.get('name'): + return jsonify({'error': 'JSON must contain persons name whose car to delete'}), 400 + + query = 'SELECT * from car WHERE name = %s' + output = database.query(query_str=query, params=(data['name'],)) + if not output: + return jsonify({'error': 'Such person does not have a car'}), 409 + + query = 'DELETE FROM car WHERE Name = %s' + output = database.query(query_str=query, params=(data['name'],)) + database.commit() + return jsonify({"message": "car deleted"}), 200 \ No newline at end of file diff --git a/backend/src/server.py b/backend/src/server.py index ce9051a..905022f 100644 --- a/backend/src/server.py +++ b/backend/src/server.py @@ -8,10 +8,12 @@ server.py is the main source file for the Dungeon's backend service. from flask import Flask, request, jsonify from dotenv import load_dotenv from db_client import DBClient +from car import registerCarEndpoints load_dotenv() app = Flask(__name__) database = DBClient() +registerCarEndpoints(app=app, database=database) @app.route('/login', methods=['POST']) def login(): @@ -20,58 +22,7 @@ def login(): else: return jsonify({'error': 'Request must contain JSON data'}), 400 -@app.route('/car', methods=['GET']) -def get_car(): - if not request.is_json: - return jsonify({'error': 'Request must contain JSON data'}), 400 - data = request.get_json() - - if 'name' not in data or not data['name']: - return jsonify({'error': 'Request must contain name field'}), 400 - - query = f'SELECT * from car WHERE name = %s' - output = database.query(query_str=query, params=(data['name'],)) - if not output: - return jsonify({"message": "No car by that name exist"}), 404 - car = output[0] - if len(car) != 3: - return jsonify({'error': 'Car data is corrupted'}), 500 - - response = { - "name": car[0], - "car": car[1], - "freeCarSpaces": car[2] - } - return jsonify(response), 200 - -@app.route('/car', methods=['POST']) -def add_car(): - if not request.is_json: - return jsonify({'error': 'Request must contain JSON data'}), 400 - - data = request.get_json() - if not data['name'] or not data['car']: - return jsonify({'error': 'JSON must contain car and name fields'}), 400 - - query = f'SELECT * from car WHERE name = %s' - output = database.query(query_str=query, params=(data['name'],)) - if output: - return jsonify({'error': 'A car with specified name already exists'}), 409 - - query = f'INSERT into car (Name, Car) VALUES (%s, %s)' - output = database.query(query_str=query, params=(data['name'],data['car'])) - - database.commit() - return jsonify({"message": "car added", "car": {"car": data['car'], "name": data['name']}}), 200 - -@app.route('/car', methods=['UPDATE']) -def update_car(): - return jsonify({"hello": "user"}), 200 - -@app.route('/car', methods=['DELETE']) -def delete_car(): - return jsonify({"hello": "user"}), 200 if __name__ == "__main__": app.run(debug=True) \ No newline at end of file diff --git a/database/.gitignore b/database/.gitignore deleted file mode 100644 index 4f509e5..0000000 --- a/database/.gitignore +++ /dev/null @@ -1 +0,0 @@ -*.env \ No newline at end of file diff --git a/database/docker-compose.yaml b/database/docker-compose.yaml deleted file mode 100644 index 69866f5..0000000 --- a/database/docker-compose.yaml +++ /dev/null @@ -1,14 +0,0 @@ -services: - db: - image: mysql:9.4.0 - container_name: nyi-db - ports: - - "${DB_SERVER}:${DB_PORT}:3306" - environment: - - MYSQL_ROOT_PASSWORD=${ROOT_PWD} - - MYSQL_DATABASE=${DB_NAME} - volumes: - - nyi_db_volume:/var/lib/mysql - -volumes: - nyi_db_volume: \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..5e1fbc8 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,23 @@ +services: + backend: + build: + context: backend + dockerfile: Dockerfile + ports: + - "2027:5000" + container_name: "${CONTAINER_NAME:-nyi-backend}" + depends_on: + - db # Ensure backend waits for db to start + db: + image: mysql:9.4.0 + container_name: nyi-db + ports: + - "${DB_SERVER}:${DB_PORT}:3306" + environment: + - MYSQL_ROOT_PASSWORD=${ROOT_PWD} + - MYSQL_DATABASE=${DB_NAME} + volumes: + - nyi_db_volume:/var/lib/mysql + +volumes: + nyi_db_volume: \ No newline at end of file