def car_get(): """ Gets a record based on parameters supplied to the endpoint. Returns first suitable found object based on params Endpoint URL: /car/get :return: JSON of an object or exception status """ if request.method == "GET": if request.args is None: return jsonify({ "status_code": 400, "message": "Invalid request" }) try: params = {} # list of params that we will search by # Check if any of the parameters are being passed and then validate them if "id" in request.args.keys(): params['id'] = helpers.validate_int( request.args.get('id'), 'id') if "make" in request.args.keys(): params['make'] = helpers.validate_string( request.args.get('make'), 'make') if "model" in request.args.keys(): params['model'] = helpers.validate_string( request.args.get('model'), 'model') if "year" in request.args.keys(): params['year'] = helpers.validate_year( request.args.get('year')) if "assigned_type" in request.args.keys(): params['assigned_type'] = helpers.validate_int( request.args.get('assigned_type'), 'assigned_type') if "assigned_id" in request.args.keys(): params['assigned_id'] = helpers.validate_int( request.args.get('assigned_id'), 'assigned_id') # If no allowed params were passed on - invalidate the request if not params: raise Exception({ "status_code": 400, "message": "Invalid request" }) # Get the object based on the given parameters car = Car.get(params) if not car: raise Exception({ "status_code": 404, "message": "Car not found" }) return jsonify(car.serialize()) except Exception as e: return jsonify({ "status_code": e.args[0]['status_code'], "message": e.args[0]['message'] })
def car_delete(): """ Deletes a record based on the id Endpoint URL: /car/delete :return: JSON successful message or exception response """ if request.method == "DELETE": if request.args is None: return jsonify({ "status_code": 400, "message": "Invalid request" }) try: # Validate id parameter passed id = helpers.check_missing('args', request, 'id') id = helpers.validate_int(id, 'id') # Find the object to delete params = {"id": id} car = Car.get(params) # Return 404 if not found the object to delete if not car: return jsonify({ "status_code": 404, "message": "Car not found" }) # Delete object and return successful message car.delete() return jsonify({"status_code": 200, "message": "Car deleted"}) except Exception as e: return jsonify({ "status_code": e.args[0]['status_code'], "message": e.args[0]['message'] })
def car_update(): """ Updates a record based on id supplied Endpoint URL: /car/update :return: JSON successful message or exception response """ if request.method == "PUT": if request.data is None: return jsonify({ "status_code": 400, "message": "Invalid request" }) request_data = request.data try: # Validate id parameter passed id = helpers.check_missing('list', request_data, 'id') id = helpers.validate_int(id, 'id') # Find the object to update params = {"id": id} car = Car.get(params) # Return 404 if not found the object to update if not car: raise Exception({ "status_code": 404, "message": "Car not found" }) # Find and validate any allowed parameters if "make" in request_data.keys(): car.make = helpers.validate_string(request_data['make'], 'make') if "model" in request_data.keys(): car.model = helpers.validate_string( request_data['model'], 'model') if "year" in request.data.keys(): car.year = helpers.validate_year(request_data['year']) # Logic to enforce assigned type and id to be passed on and validated together if "assigned_type" in request_data.keys( ) and not "assigned_id" in request_data.keys(): raise Exception({ "status_code": 400, "message": "Missing assigned_id" }) elif "assigned_id" in request_data.keys( ) and not "assigned_type" in request_data.keys(): raise Exception({ "status_code": 400, "message": "Missing assigned_type" }) elif set( ("assigned_type", "assigned_id")) <= request_data.keys(): assigned_type, assigned_id = helpers.validate_assigning( request_data['assigned_type'], request_data['assigned_id']) car.assigned_type = assigned_type car.assigned_id = assigned_id # Save an object and return successful message car.save() return jsonify({ "status_code": 200, "message": "Car record was updated" }) except Exception as e: return jsonify({ "status_code": e.args[0]['status_code'], "message": e.args[0]['message'] })