예제 #1
0
def delete_item(event, context):
    try:
        userId = json_func.get_username(event)
        [itemId] = json_func.get_body_args(event, {'itemId': int})
        item_table_name = os.environ['ITEM_TABLE']
        client = boto3.client('dynamodb')
        resp = client.delete_item(
            TableName=item_table_name,
            Key={
                'userId': {
                    'S': userId,
                },
                'itemId': {
                    'N': str(itemId)  #should it be entered as string type?
                }
            },
            ReturnValues='ALL_OLD')
        if "Attributes" in resp:  #deleted something
            return {
                "statusCode": 200,
                "body": "deleted item: " + json.dumps(resp['Attributes'])
            }
        else:
            return {
                "statusCode": 404,
                "body":
                "item with itemId = " + str(itemId) + " does not exist."
            }
    except Exception as err:
        return json_func.errorMessage(err)
예제 #2
0
def get_user_items(event, context):
    try:
        userId = json_func.get_username(event)
        table = itemTableResource()
        resp = table.query(KeyConditionExpression=Key('userId').eq(userId))
        return {
            "statusCode": 200,
            "body": json.dumps(resp, cls=json_func.DecimalEncoder)
        }
    except Exception as err:
        return json_func.errorMessage(err)
예제 #3
0
def add_item(event, context):
    try:
        userId = json_func.get_username(event)
        [value] = json_func.get_body_args(event, {'value': str})
        itemNum = user_lambdas.nextItemNum(userId)
        table = itemTableResource()
        resp = table.put_item(Item={
            "userId": userId,
            "itemId": itemNum,
            "value": value
        })
        return {"statusCode": 201, "body": "added item"}
    except Exception as err:
        return json_func.errorMessage(err)
예제 #4
0
def get_item(event, context):
    try:
        userId = json_func.get_username(event)
        [itemId] = json_func.get_querystring_args(event, {'itemId': int})
        table = itemTableResource()
        resp = table.get_item(Key={"userId": userId, "itemId": itemId})
        if 'Item' not in resp:
            raise FileNotFoundError("item with itemId = " + str(itemId) +
                                    " does not exist")
        return {
            "statusCode": 200,
            "body": json.dumps(resp['Item'], cls=json_func.DecimalEncoder)
        }
    except Exception as err:
        return json_func.errorMessage(err)
예제 #5
0
def edit_item_field(event, context):
    try:
        userId = json_func.get_username(event)
        itemId, attrName, attrValue = json_func.get_body_args(
            event, {
                'itemId': int,
                'attrName': str,
                'attrValue': str
            })
        if not existingItem(userId, itemId):
            return {
                "statusCode": 404,
                "body":
                "item with itemId = " + str(itemId) + " does not exist."
            }
        editResp = itemTableResource().update_item(
            Key={
                "userId": userId,
                "itemId": itemId
            },
            ExpressionAttributeNames={
                "#attrName": attrName,
            },
            ExpressionAttributeValues={
                ":attrValue": attrValue,
            },
            UpdateExpression="SET #attrName = :attrValue",
        )

        #TODO check the edit response for success, or return the response
        return {
            "statusCode": 200,
            "body": "set attribute '" + attrName + "' to " + repr(attrValue)
        }
    except Exception as err:
        return json_func.errorMessage(err)