backend: implment GET for car by name
This commit is contained in:
parent
7118bb2c5b
commit
b464ea04fd
@ -82,9 +82,9 @@ class DBClient:
|
||||
self.query(STARTUP_TABLE_CREATION_QUERIES['food'])
|
||||
self.query(STARTUP_TABLE_CREATION_QUERIES['passengers'])
|
||||
|
||||
def query(self, query_str, quiet=False):
|
||||
def query(self, query_str, quiet=False, params=None):
|
||||
self.info(f'Executing query: {query_str}')
|
||||
self.cursor.execute(query_str)
|
||||
self.cursor.execute(query_str, params)
|
||||
if quiet:
|
||||
return []
|
||||
return self.cursor.fetchall()
|
||||
|
||||
@ -19,6 +19,43 @@ def login():
|
||||
return jsonify({"hello": "user"}), 200
|
||||
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():
|
||||
return jsonify({"hello": "user"}), 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)
|
||||
Loading…
x
Reference in New Issue
Block a user