def add_car(): """ Endpoint to add car to db Returns: car added to db, or 400 """ schema = CarSchema(exclude=['id']) try: new_car = schema.loads(request.get_json()) except ValidationError as ve: return abort(400, description=ve.messages) if Car.query.filter_by(reg_number=new_car.reg_number).first() is not None: return abort(403, description='Conflict, reg number exists!') db.session.add(new_car) handle_db_operation(db.session.commit) return jsonify(schema.dumps(new_car)), 201
def car(id: int): """ Get, update, or delete car Args: id (int): car id Returns: Error or 200 """ car = Car.query.get(id) if car is None: return abort(404, description='Car not found') schema = CarSchema() if request.method == 'GET': # Dump to dict, then add issue text car_data = schema.dump(car) if car.issue is not None: car_data['issue'] = car.issue.issue return jsonify(json.dumps(car_data)), 200 elif request.method == 'PUT': try: new_car = schema.loads(request.get_json()) except ValidationError as ve: return abort(400, description=ve.messages) car.reg_number = new_car.reg_number car.car_manufacturer = new_car.car_manufacturer car.car_colour = new_car.car_colour car.car_type = new_car.car_type car.seats = new_car.seats car.hour_rate = new_car.hour_rate car.longitude = new_car.longitude car.latitude = new_car.latitude handle_db_operation(db.session.commit) return jsonify('Car updated'), 200 elif request.method == 'DELETE': Car.query.filter_by(id=id).delete() handle_db_operation(db.session.commit) return jsonify('Car deleted'), 200