Ejemplo n.º 1
0
def create(event, context):
    data = json.loads(event['body'])

    customer_id = data['customer_id']
    product_id = data['product_id']
    basket = None

    # check if the item is in the basket already, if so we're gonna just add to it
    try:
        basket = Basket.get(customer_id, product_id)

        if basket is not None:
            basket.quantity = basket.quantity + data['quantity']
    except:
        pass

    if basket is None:
        basket = Basket(customer_id=customer_id,
                        product_id=product_id,
                        quantity=data['quantity'],
                        price=data['price'])

    basket.save()

    return {"statusCode": 200, "body": basket.body()}
Ejemplo n.º 2
0
def get(event, context):
    customer_id = event['pathParameters']['id']
    item_id = event['pathParameters']['product_id']

    try:
        basket = Basket.get(customer_id, item_id)

        return {'statusCode': 200, 'body': basket.body()}
    except:
        raise Exception("Invalid request")
Ejemplo n.º 3
0
def update(event, context):
    #    if 'requestContext' in event:
    #        if 'identity' in event['requestContext']:
    #            if 'cognitoIdentityId' in event['requestContext']['identity']:
    #                id = event['requestContext']['identity']['cognitoIdentityId']
    #    else:
    id = event['pathParameters']['id']

    data = json.loads(event['body'])

    product_id = data['product_id']

    try:
        basket = Basket.get(id, product_id)

        basket.quantity = data['quantity']

        basket.save()

        return {'statusCode': 200, 'body': {basket.body()}}
    except Exception as ex:
        logger.error(ex)
        raise Exception("Invalid request")