Пример #1
0
def handler(event, context):
    global dynamodb
    if not dynamodb:
        dynamodb = utilities.get_dynamodb_client(event)

    print(event)
    body = json.loads(event['body'])
    table = dynamodb.Table(VENDORS_TABLE)

    response = table.get_item(Key={V_EMAIL_COLUMN: body['v_email']})

    if response.get('Item') is None:
        response = table.put_item(
            Item={
                V_EMAIL_COLUMN: body['v_email'],
                V_NAME_COLUMN: body['name'],
                V_MOBILE_COLUMN: int(body['mobile']),
                PASSWORD: body['password']
            })

    return {
        'statusCode': 200,
        'headers': {
            'Content-Type': 'application/json',
            "Access-Control-Allow-Origin": "*"
        }
    }
Пример #2
0
def handler(event, context):
    global dynamodb
    if not dynamodb:
        dynamodb = utilities.get_dynamodb_client(event)

    table = dynamodb.Table(ORDERS_TABLE)

    response = None
    type = event[PARAMS]['type']
    if type == 'vendor':
        response = table.scan(
            FilterExpression=Key(V_EMAIL_COLUMN).eq(event[PARAMS]['v_email']))
    else:
        response = table.scan(
            FilterExpression=Key(U_EMAIL_COLUMN).eq(event[PARAMS]['u_email']))

    data = response['Items']

    return {
        'statusCode': 200,
        'body': json.dumps(data, default=utilities.decimal_default),
        'headers': {
            'Content-Type': 'application/json',
            "Access-Control-Allow-Origin": "*"
        }
    }
Пример #3
0
def handler(event, context):
    global dynamodb
    if not dynamodb:
        dynamodb = utilities.get_dynamodb_client(event)

    # table = dynamodb.Table(LOCATIONS_TABLE)
    # response = table.scan()
    #
    # items = response['Items']
    # divisions = set()
    # for item in items:
    #     divisions.add(item[DIVISION_COLUMN])

    divisions = {
        "divisions": [
            CENTRAL1_COLUMN, CENTRAL2_COLUMN, EAST1_COLUMN, EAST2_COLUMN,
            NORTH1_COLUMN, NORTH2_COLUMN, NORTH3_COLUMN, NORTHEAST1_COLUMN,
            NORTHEAST2_COLUMN, SOUTH1_COLUMN, SOUTH2_COLUMN, SOUTH3_COLUMN,
            WEST1_COLUMN, WEST2_COLUMN
        ]
    }
    return {
        'statusCode': 200,
        'body': json.dumps(divisions, default=utilities.set_default),
        'headers': {
            'Content-Type': 'application/json',
            "Access-Control-Allow-Origin": "*"
        }
    }
Пример #4
0
def handler(event, context):
    global dynamodb
    if not dynamodb:
        dynamodb = utilities.get_dynamodb_client(event)

    table = dynamodb.Table(ORDERS_TABLE)

    response = None

    if event[PARAMS].get('v_email'):
        response = table.scan(
            FilterExpression=Key(V_EMAIL_COLUMN).eq(event[PARAMS]['v_email']))
    else:
        response = table.scan(
            FilterExpression=Key(U_EMAIL_COLUMN).eq(event[PARAMS]['u_email']))

    new_orders = []
    for order in response['Items']:
        new_order = {}
        for k, v in order.items():
            new_order[k.lower()] = v
        new_orders.append(new_order)

    orders = {"orders": new_orders}

    return {
        'statusCode': 200,
        'body': json.dumps(orders, default=utilities.decimal_default),
        'headers': {
            'Content-Type': 'application/json',
            "Access-Control-Allow-Origin": "*"
        }
    }
Пример #5
0
def handler(event, context):
    global dynamodb
    if not dynamodb:
        dynamodb = utilities.get_dynamodb_client(event)

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

    table = dynamodb.Table(ORDERS_TABLE)
    orderid = request[ORDER_ID_KEY]
    orderstatus = request[ORDER_STATUS_KEY]

    response = table.update_item(
        Key={
            ID_COLUMN: orderid
        },
        UpdateExpression="set " + STATUS_COLUMN + " = :s",
        ExpressionAttributeValues={
            ':s': orderstatus
        },

        ReturnValues="UPDATED_NEW"
    )

    return {'statusCode': 200,
            'headers': {'Content-Type': 'application/json',"Access-Control-Allow-Origin": "*"}}
Пример #6
0
def handler(event, context):
    global dynamodb
    if not dynamodb:
        dynamodb = utilities.get_dynamodb_client(event)

    table = dynamodb.Table(LOCATIONS_TABLE)
    response = table.get_item(
        Key={
            PINCODE_COLUMN: int(event[PARAMS]['pincode'])
        }
    )

    if response.get('Item') is None:
        return {'statusCode': 404, 'headers': {'Content-Type': 'application/json'}}
    else:
        item = response['Item']

        result = {}

        result['brnm'] = item[BRNM_COLUMN]
        result['area'] = item[AREA_COLUMN]

        return {'statusCode': 200,
                'body': json.dumps(result),
                'headers': {'Content-Type': 'application/json',"Access-Control-Allow-Origin": "*"}}
Пример #7
0
def handler(event, context):
    global dynamodb
    if not dynamodb:
        dynamodb = utilities.get_dynamodb_client(event)

    table = dynamodb.Table(PRICES_TABLE)
    response = table.scan()
    data = response['Items']

    # If source and destination divisions are passed
    # then return the current least shipment cost for given
    # source and destination divisions.
    if event[PARAMS]:
        source_division = event[PARAMS][SOURCE_DIVISION_KEY]
        destination_division = event[PARAMS][DESTINATION_DIVISION_KEY]
        for item in data:
            if item['SourceDivision'] == source_division:
                data = {'least_price': item[destination_division]['Price']}
                break
    else:
        data = {'least_price': list(data)}

    return {
        'statusCode': 200,
        'body': json.dumps(data, default=utilities.decimal_default),
        'headers': {
            'Content-Type': 'application/json',
            "Access-Control-Allow-Origin": "*"
        }
    }
Пример #8
0
def handler(event, context):
    global dynamodb
    if not dynamodb:
        dynamodb = utilities.get_dynamodb_client(event)

    print(event)
    body = json.loads(event['body'])
    table = dynamodb.Table(USERS_TABLE)

    response = table.get_item(
        Key={
            U_EMAIL_COLUMN: body['u_email']
        }
    )

    if response.get('Item') is None:
        response = table.put_item(
            Item ={
                U_EMAIL_COLUMN: body['u_email'],
                FIRST_NAME_COLUMN: body['first_name'],
                LAST_NAME_COLUMN: body['last_name']
            }
        )

    return {'statusCode': 200,
            'headers': {'Content-Type': 'application/json',"Access-Control-Allow-Origin": "*"}}
Пример #9
0
def handler(event, context):
    global dynamodb
    if not dynamodb:
        dynamodb = utilities.get_dynamodb_client(event)

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

    price_and_vendor = utilities.get_least_price_and_vendor(
        dynamodb, body['origin'], body['destination'])
    id = int(round(time.time() * 1000))
    price = int(price_and_vendor[PRICE_COLUMN] * int(body['weight']))

    result = {
        'price': price,
        'id': id,
        'v_email': price_and_vendor[V_EMAIL_COLUMN],
        'status': 'Accepted'
    }

    table = dynamodb.Table(VENDORS_TABLE)

    response = table.get_item(
        Key={V_EMAIL_COLUMN: price_and_vendor[V_EMAIL_COLUMN]})

    result['v_name'] = response['Item'][V_NAME_COLUMN]
    result['v_mobile'] = response['Item'][V_MOBILE_COLUMN]

    table = dynamodb.Table(ORDERS_TABLE)

    response = table.put_item(
        Item={
            ID_COLUMN: int(id),
            U_EMAIL_COLUMN: body['u_email'],
            ORIGIN_COLUMN: body['origin'],
            DESTINATION_COLUMN: body['destination'],
            O_ADDRESS_COLUMN: body['o_address'],
            D_ADDRESS_COLUMN: body['d_address'],
            PRICE_COLUMN: int(price),
            V_EMAIL_COLUMN: price_and_vendor[V_EMAIL_COLUMN],
            WEIGHT_COLUMN: int(body['weight']),
            O_MOBILE_COLUMN: int(body['o_mobile']),
            D_MOBILE_COLUMN: int(body['d_mobile']),
            O_DATE_COLUMN: body['o_date'],
            P_DATE_COLUMN: body['p_date']
            #            STATUS_COLUMN: 'Accepted'
        })

    return {
        'statusCode': 200,
        'body': json.dumps(result, default=utilities.decimal_default),
        'headers': {
            'Content-Type': 'application/json',
            "Access-Control-Allow-Origin": "*"
        }
    }
Пример #10
0
def handler(event, context):
    global dynamodb
    if not dynamodb:
        dynamodb = utilities.get_dynamodb_client(event)

    price_and_vendor = utilities.get_least_price_and_vendor(dynamodb, event[PARAMS]['origin'], event[PARAMS]['destination'])

    result = {'price': str(int(price_and_vendor[PRICE_COLUMN] * int(event[PARAMS]['weight'])))}

    return {'statusCode': 200,
            'body': json.dumps(result),
            'headers': {'Content-Type': 'application/json',"Access-Control-Allow-Origin": "*"}}
Пример #11
0
def handler(event, context):
    global dynamodb
    if not dynamodb:
        dynamodb = utilities.get_dynamodb_client(event)

    #print(event)
    request = json.loads(event['body'])

    table = dynamodb.Table(PRICES_TABLE)
    source = request[SOURCE_DIVISION_KEY]
    destination = request[DESTINATION_DIVISION_KEY]
    price = request['price']
    vendor_email = request['v_email']

    table = dynamodb.Table(PRICES_TABLE)
    response = table.get_item(
        Key={SOURCE_DIVISION_COLUMN: request[SOURCE_DIVISION_KEY]})

    print(response)
    oldprice = response['Item'][destination]['Price']

    if int(oldprice) > int(price):
        response = table.update_item(
            Key={SOURCE_DIVISION_COLUMN: source},
            UpdateExpression="set " + destination + " = :p",
            ExpressionAttributeValues={
                ':p': {
                    PRICE_COLUMN: decimal.Decimal(price),
                    V_EMAIL_COLUMN: vendor_email
                }
            },
            ReturnValues="UPDATED_NEW")

        return {
            'statusCode': 200,
            'headers': {
                'Content-Type': 'application/json',
                "Access-Control-Allow-Origin": "*"
            }
        }
    else:
        return {
            'statusCode': 201,
            'headers': {
                'Content-Type': 'application/json',
                "Access-Control-Allow-Origin": "*"
            }
        }
Пример #12
0
def handler(event, context):
    global dynamodb
    if not dynamodb:
        dynamodb = utilities.get_dynamodb_client(event)

    table = dynamodb.Table(LOCATIONS_TABLE)
    response = table.scan()

    items = response['Items']
    divisions = set()
    for item in items:
        divisions.add(item[DIVISION_COLUMN])

    divisions_list = {"divisions": list(divisions)}

    return {'statusCode': 200,
            'body': json.dumps(divisions_list, default=utilities.set_default),
            'headers': {'Content-Type': 'application/json',"Access-Control-Allow-Origin": "*"}}
def handler(event, context):
    global dynamodb
    if not dynamodb:
        dynamodb = utilities.get_dynamodb_client(event)

    table = dynamodb.Table(PRICES_TABLE)
    response = table.scan()
    data = response['Items']
    print(data)

    return {
        'statusCode': 200,
        'body': json.dumps(data, default=utilities.decimal_default),
        'headers': {
            'Content-Type': 'application/json',
            "Access-Control-Allow-Origin": "*"
        }
    }
Пример #14
0
def handler(event, context):
    global dynamodb
    if not dynamodb:
        dynamodb = utilities.get_dynamodb_client(event)

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

    response = None
    if body.get('customer'):
        table = dynamodb.Table(USERS_TABLE)

        response = table.get_item(Key={U_EMAIL_COLUMN: body['email']})
    else:
        table = dynamodb.Table(VENDORS_TABLE)

        response = table.get_item(Key={V_EMAIL_COLUMN: body['email']})

    if response.get('Item') is None:
        return {
            'statusCode': 201,
            'headers': {
                'Content-Type': 'application/json',
                "Access-Control-Allow-Origin": "*"
            }
        }
    else:
        if response.get('Item').get(PASSWORD) == body['password']:
            return {
                'statusCode': 200,
                'headers': {
                    'Content-Type': 'application/json',
                    "Access-Control-Allow-Origin": "*"
                }
            }
        else:
            return {
                'statusCode': 201,
                'headers': {
                    'Content-Type': 'application/json',
                    "Access-Control-Allow-Origin": "*"
                }
            }
def handler(event, context):
    global dynamodb
    if not dynamodb:
        dynamodb = utilities.get_dynamodb_client(event)


    table = dynamodb.Table(ORDERS_TABLE)

    response = table.scan(
        #FilterExpression= Key(U_EMAIL_COLUMN).eq(event[PARAMS]['u_email']) & Key(ID_COLUMN).eq(int(event[PARAMS]['id']))
        FilterExpression=Key(ID_COLUMN).eq(int(event[PARAMS]['id']))
    )
    print(response)
    data =response['Items'][0]

    data = {k.lower(): v for k, v in data.items()}

    return {'statusCode': 200,
            'body': json.dumps(data, default=utilities.decimal_default),
            'headers': {'Content-Type': 'application/json',"Access-Control-Allow-Origin": "*"}}