Beispiel #1
0
def schema(event, context):
    create_todos = query.create_class({
        'name': 'todos'
    })

    create_all_todos = query.create_index({
        'name': 'all_todos',
        'source': TODOS
    })

    client.query(query.if_expr(
        query.exists(TODOS),
        query.get(TODOS),
        create_todos
    ))

    client.query(query.if_expr(
        query.exists(ALL_TODOS),
        query.get(ALL_TODOS),
        create_all_todos
    ))

    # create a response
    response = {
        "statusCode": 200
    }

    return response
Beispiel #2
0
def delete(event, context):
    # delete the todo from the database
    client.query(query.delete(Ref(TODOS, event['pathParameters']['id'])))

    # create a response
    response = {
        "statusCode": 200
    }

    return response
Beispiel #3
0
def get(event, context):
    # fetch todo from the database
    ref = Ref(TODOS, event['pathParameters']['id'])
    fetched = client.query(query.get(ref))

    # create a response
    response = {"statusCode": 200, "body": json.dumps(make_result(fetched))}

    return response
Beispiel #4
0
def schema(event, context):
    create_todos = query.create_class({'name': 'todos'})

    create_all_todos = query.create_index({
        'name': 'all_todos',
        'source': TODOS
    })

    client.query(
        query.if_expr(query.exists(TODOS), query.get(TODOS), create_todos))

    client.query(
        query.if_expr(query.exists(ALL_TODOS), query.get(ALL_TODOS),
                      create_all_todos))

    # create a response
    response = {"statusCode": 200}

    return response
Beispiel #5
0
def get(event, context):
    # fetch todo from the database
    ref = Ref(TODOS, event['pathParameters']['id'])
    fetched = client.query(query.get(ref))

    # create a response
    response = {
        "statusCode": 200,
        "body": json.dumps(make_result(fetched))
    }

    return response
Beispiel #6
0
def list(event, context):
    # fetch all todos from the database
    results = client.query(
        query.map_expr(lambda ref: query.get(ref),
                       query.paginate(query.match(ALL_TODOS))))

    # create a response
    response = {
        "statusCode": 200,
        "body": json.dumps(map(make_result, results['data']))
    }

    return response
Beispiel #7
0
def list(event, context):
    # fetch all todos from the database
    results = client.query(
        query.map_expr(lambda ref: query.get(ref),
                       query.paginate(query.match(ALL_TODOS))))

    # create a response
    response = {
        "statusCode": 200,
        "body": json.dumps(map(make_result, results['data']))
    }

    return response
Beispiel #8
0
def create(event, context):
    data = json.loads(event['body'])
    if 'text' not in data:
        logging.error("Validation Failed")
        raise Exception("Couldn't create the todo item.")

    data = {
        'text': data['text'],
        'checked': False,
        'createdAt': query.time('now'),
        'updatedAt': query.time('now')
    }

    # write the todo to the database
    created = client.query(query.create(TODOS, {'data': data}))

    # create a response
    response = {"statusCode": 200, "body": json.dumps(make_result(created))}

    return response
Beispiel #9
0
def update(event, context):
    data = json.loads(event['body'])
    if 'text' not in data or 'checked' not in data:
        logging.error("Validation Failed")
        raise Exception("Couldn't update the todo item.")

    data = {
        'text': data['text'],
        'checked': data['checked'],
        'updatedAt': query.time('now')
    }

    # update the todo in the database
    ref = Ref(TODOS, event['pathParameters']['id'])
    updated = client.query(query.update(ref, {'data': data}))

    # create a response
    response = {"statusCode": 200, "body": json.dumps(make_result(updated))}

    return response
Beispiel #10
0
def update(event, context):
    data = json.loads(event['body'])
    if 'text' not in data or 'checked' not in data:
        logging.error("Validation Failed")
        raise Exception("Couldn't update the todo item.")

    data = {
        'text': data['text'],
        'checked': data['checked'],
        'updatedAt': query.time('now')
    }

    # update the todo in the database
    ref = Ref(TODOS, event['pathParameters']['id'])
    updated = client.query(query.update(ref, {'data': data}))

    # create a response
    response = {
        "statusCode": 200,
        "body": json.dumps(make_result(updated))
    }

    return response
Beispiel #11
0
def create(event, context):
    data = json.loads(event['body'])
    if 'text' not in data:
        logging.error("Validation Failed")
        raise Exception("Couldn't create the todo item.")

    data = {
        'text': data['text'],
        'checked': False,
        'createdAt': query.time('now'),
        'updatedAt': query.time('now')
    }

    # write the todo to the database
    created = client.query(query.create(TODOS, {'data': data}))

    # create a response
    response = {
        "statusCode": 200,
        "body": json.dumps(make_result(created))
    }

    return response