def create_auto(request: Request): try: car_data = request.json_body car = Car.from_dict(car_data) except: return Response(status=400, body='Could not parse your post as JSON.') # TODO: Validate try: car = Repository.add_car(car) return Response(status=201, json_body=car) except: return Response(status=400, body='Could not save car.')
def update_auto(request: Request): car_id = request.matchdict.get('car_id') car = Repository.car_by_id(car_id) if car_id == '__first__': car_id = Repository.all_cars()[0].id if not car: msg = "The car with id '{}' was not found.".format(car_id) return Response(status=404, json_body={'error': msg}) try: car_data = request.json_body car = Car.from_dict(car_data) except: return Response(status=400, body='Could not parse your post as JSON.') # TODO: Validate try: Repository.update_car(car) return Response(status=204, body='Car updated successfully.') except: return Response(status=400, body='Could not update car.')