backend: add car POST
This commit is contained in:
parent
b464ea04fd
commit
7bf2a02d9d
@ -66,6 +66,7 @@ class DBClient:
|
||||
|
||||
self.cursor = self.connection.cursor()
|
||||
self.initialize_database()
|
||||
self.commit()
|
||||
|
||||
def create_database(self, db_name):
|
||||
query = f"CREATE DATABASE IF NOT EXISTS `{db_name}`;"
|
||||
@ -89,6 +90,10 @@ class DBClient:
|
||||
return []
|
||||
return self.cursor.fetchall()
|
||||
|
||||
def commit(self):
|
||||
self.info('Commiting actions to DB')
|
||||
self.connection.commit()
|
||||
|
||||
def close(self):
|
||||
self.cursor.close()
|
||||
self.connection.close()
|
||||
|
||||
@ -47,7 +47,23 @@ def get_car():
|
||||
|
||||
@app.route('/car', methods=['POST'])
|
||||
def add_car():
|
||||
return jsonify({"hello": "user"}), 200
|
||||
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():
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user