Exemplo n.º 1
0
def create_auto(request: Request):
    log.info("The user {} is creating a car".format(request.api_user.name))

    try:
        car_data = request.json_body
    except Exception as x:
        log.warn("The submitted JSON body couldn't be parsed for {}".format(
            request.api_user.name))
        return Response(status=400,
                        body='Could not parse your post as JSON.' + str(x))

    vm = CreateAutoViewModel(car_data)
    vm.compute_details()
    if vm.errors:
        log.warn("The submitted invalid car by {}, reasons: {}".format(
            request.api_user.name, '; '.join(vm.errors)))
        return Response(status=400, body=vm.error_msg)

    try:
        car = Repository.add_car(vm.car)
        log.info("{} has successfully created car {}".format(
            request.api_user.name, car.id))
        return Response(status=201, json_body=car.to_dict())
    except Exception as x:
        log.error("Failed to save car: {}".format(x))
        return Response(status=500, body='Could not save car. ' + str(x))
def create_auto(request: Request):
    try:
        car_data = request.json_body
    except:
        return Response(status=400, body='Could not parse your post as JSON.')

    # TODO: Validate

    try:
        car_data = Repository.add_car(car_data)
        return Response(status=201, json_body=car_data)
    except:
        return Response(status=400, body='Could not save car.')
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.to_dict())
    except Exception as x:
        return Response(status=400, body='Could not save car: {}'.format(x))
Exemplo n.º 4
0
def create_auto(request: Request):
    try:
        car_data = request.json_body
    except Exception as x:
        return Response(status=400, body='Could not parse your post as JSON.' + str(x))

    vm = CreateAutoViewModel(car_data)
    vm.compute_details()
    if vm.errors:
        return Response(status=400, body=vm.error_msg)

    try:
        car = Repository.add_car(vm.car)
        return Response(status=201, json_body=car.to_dict())
    except Exception as x:
        return Response(status=500, body='Could not save car. ' + str(x))