示例#1
0
def checkout_cart(cart_id):
    body = request.get_json()

    required_params = ["payment_method"]
    try:
        check_parameters(params=body,
                         required=required_params,
                         possible=required_params)
    except exceptions.ParamsError as e:
        return {"message": e.message}, 400

    try:
        cart = cart_service.checkout_cart(
            body, cart_id, request.headers.environ.get('HTTP_AUTHORIZATION'))
    except exceptions.PaymentError:
        return {"message": "Payment error"}, 500
    except exceptions.CartNotFoundException:
        return "", 404
    except (exceptions.StorageServerError, exceptions.UserAuthServerError):
        return "", 500
    except exceptions.InvalidPaymentMethod:
        return {
            "message": "Invalid payment method " + body['payment_method']
        }, 400
    return cart_mapper.get_cart(cart), 200
示例#2
0
def register_user():
    body = request.get_json()

    possible_params = ['username', 'password', 'role']
    try:
        check_parameters(params=body, required=possible_params, possible=possible_params)
    except exceptions.ParamsError as e:
        return {"message": e.message}, 400
    try:
        user_service.register_user(body['username'], body['password'], body['role'])
    except exceptions.UsernameAlreadyRegistered:
        return {"message": "Username " + body['username'] + " is already in used."}, 400
    return "", 201
示例#3
0
def update_cart(cart_id):
    body = request.get_json()

    possible_params = ["status", "payment_method", "total_amount", "username"]
    try:
        check_parameters(params=body,
                         required=possible_params,
                         possible=possible_params)
    except exceptions.ParamsError as e:
        return {"message": e.message}, 400
    try:
        cart = cart_service.update_cart(cart_id, **body)
    except exceptions.CartNotFoundException:
        return "", 404
    return cart_mapper.get_cart(cart), 200
示例#4
0
def login_user():
    body = request.get_json()

    possible_params = ['username', 'password']
    try:
        check_parameters(params=body, required=possible_params, possible=possible_params)
    except exceptions.ParamsError as e:
        return {"message": e.message}, 400

    username = body['username']
    password = body['password']
    try:
        access_token = user_service.login_user(username, password)
    except exceptions.InvalidUsernameOrPassword:
        return {"message": "Invalid username or password"}, 400
    return {
            'token': access_token
           }, 200
示例#5
0
def login_user():
    body = request.get_json()

    possible_params = ['username', 'password']
    try:
        check_parameters(params=body,
                         required=possible_params,
                         possible=possible_params)
    except exceptions.ParamsError as e:
        return {"message": e.message}, 400

    try:
        token = user_service.login_user(body['username'], body['password'])
    except exceptions.InvalidUsernameOrPassword:
        return {"message": "Invalid username or password"}, 400
    except exceptions.UserAuthServerError:
        return "", 500
    return {'token': token}, 200
示例#6
0
def create_item():
    body = request.get_json()

    required_params = ["name", "description", "price", "supplier", "category"]
    try:
        check_parameters(params=body,
                         required=required_params,
                         possible=required_params)
    except exceptions.ParamsError as e:
        return {"message": e.message}, 400
    try:
        item_id = item_service.create_item(**body)
    except exceptions.ItemAlreadyExists:
        return {
            "message": "There is already an item with the name " + body['name']
        }, 400

    return {"item_id": str(item_id)}, 201
示例#7
0
def update_item(item_id):
    body = request.get_json()
    possible_params = ["name", "description", "price", "supplier", "category"]
    try:
        check_parameters(params=body, possible=possible_params)
    except exceptions.ParamsError as e:
        return {"message": e.message}, 400
    try:
        item_updated = item_service.update_item(item_id, **body)
    except exceptions.ItemNotFoundException:
        return 404
    except exceptions.NotAnAdmin:
        return {"message": "Only admin users can perform this operation"}, 401
    except exceptions.ItemAlreadyExists:
        return {
            "message": "There is already an item with the name " + body['name']
        }, 400

    return item_mapper.get_item(item_updated), 200
示例#8
0
def register_user():
    body = request.get_json()

    possible_params = ['username', 'password', 'role']
    try:
        check_parameters(params=body,
                         required=possible_params,
                         possible=possible_params)
    except exceptions.ParamsError as e:
        return {"message": e.message}, 400

    try:
        user_service.register_user(body['username'], body['password'],
                                   body['role'])
    except exceptions.BadRequest as e:
        return e.message, 400
    except exceptions.UserAuthServerError:
        return "", 500

    return "", 201
示例#9
0
def update_item(item_id):
    body = request.get_json()
    possible_params = ["name", "description", "price", "supplier", "category"]
    try:
        check_parameters(params=body, possible=possible_params)
    except exceptions.ParamsError as e:
        return {"message": e.message}, 400

    try:
        item_updated = item_service.update_item(
            item_id, request.headers.environ.get('HTTP_AUTHORIZATION'), **body)
    except exceptions.ItemNotFoundException:
        return "", 404
    except exceptions.NotAnAdmin:
        return {"message": "Only admin users can perform this operation"}, 401
    except exceptions.BadRequest as e:
        return e.message, 400
    except exceptions.StorageServerError:
        return "", 500
    except exceptions.UserAuthServerError:
        return "", 500

    return item_mapper.get_item(item_updated), 200