backend: add CRUD for car
This commit is contained in:
parent
7bf2a02d9d
commit
42635e2281
@ -1,8 +0,0 @@
|
||||
services:
|
||||
backend:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
ports:
|
||||
- "2027:5000"
|
||||
container_name: "${CONTAINER_NAME:-nyi-backend}"
|
||||
93
backend/src/car.py
Normal file
93
backend/src/car.py
Normal file
@ -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
|
||||
@ -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)
|
||||
1
database/.gitignore
vendored
1
database/.gitignore
vendored
@ -1 +0,0 @@
|
||||
*.env
|
||||
@ -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:
|
||||
23
docker-compose.yaml
Normal file
23
docker-compose.yaml
Normal file
@ -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:
|
||||
Loading…
x
Reference in New Issue
Block a user