Esempio n. 1
0
def update(username):
    user = User.query.filter_by(username=username).first()
    if not user:
        return jsonify({'error': 'User does not exist'}), 404

    request_body = request.json
    if type(request_body) != list or len(request_body) == 0:
        return jsonify({
            'error':
            'The request body must be an array with at least one task'
        }), 400

    for task in user.todos:
        task.query.delete()

    for task in request_body:
        todo = ToDo()
        todo.label = task['label']
        todo.done = task['done']
        todo.user_id = user.id
        db.session.add(todo)

    db.session.commit()

    return jsonify({
        "result":
        "A list with " + str(len(user.todos)) + " todos was succesfully saved"
    })
Esempio n. 2
0
def create(username):
    request_body = request.json
    if request_body != []:
        return jsonify({'error': 'Request body must be an empty array'}), 400

    user = User.query.filter_by(username=username).first()
    if user:
        return jsonify({'error': 'User already exists'}), 422

    user = User()
    user.username = username

    db.session.add(user)
    db.session.commit()

    todo = ToDo()
    todo.label = 'sample task'
    todo.done = False
    todo.user_id = user.id

    db.session.add(todo)
    db.session.commit()

    return jsonify({'result': 'ok'}), 201