def getAllTasks(): db = get_db() user_id = get_jwt_identity() tasks = db.execute('SELECT * FROM task WHERE user_id = ?', (user_id, )).fetchall() task_list = list() for task in tasks: task_list.append(dict(task)) return jsonify({"tasks": task_list}), 200
def get_item(id): item = get_db().execute( 'SELECT id, title, body, created' ' FROM todo' ' WHERE id = ?', (id, )).fetchone() if item is None: abort(404, "Item id {0} doesn't exist.".format(id)) return item
def getTask(id): db = get_db() user_id = get_jwt_identity() error = None task = db.execute('SELECT * FROM task WHERE user_id = ? AND id = ?', (user_id, id)).fetchone() if task is None: error = "This task doesn't exist" if error is None: return jsonify(dict(task)), 200 return jsonify({'error': True, 'message': error}), 404
def deleteTask(id): db = get_db() user_id = get_jwt_identity() error = None task = db.execute('SELECT * FROM task WHERE user_id = ? AND id = ?', (user_id, id)).fetchone() if task is None: error = "This task doesn't exist" if error is None: db.execute('DELETE FROM task WHERE user_id = ? AND id = ?', (user_id, id)) db.commit() return jsonify({"success": True}), 200 return jsonify({'error': True, 'message': error}), 404
def createTask(): db = get_db() user_id = get_jwt_identity() if not request.json['title']: error = 'Title is required.' elif not request.json['body']: error = 'Body is required.' db.execute('INSERT INTO task (user_id, title, body) VALUES (?, ?, ?)', (user_id, request.json['title'], request.json['body'])) db.commit() last_row_id = db.execute('SELECT last_insert_rowid()').fetchone() task = db.execute('SELECT * FROM task WHERE id = ?', (last_row_id["last_insert_rowid()"], )).fetchone() print(task) return jsonify(dict(task)), 200
def login(): db = get_db() error = None username = request.json['username'] password = request.json['password'] user = db.execute( 'SELECT * FROM user WHERE username = ?', (username,) ).fetchone() if user is None: error = 'Incorrect username.' elif not check_password_hash(user['password'], password): error = 'Incorrect password.' if error is None: token = create_access_token(user['id'], expires_delta=False) return jsonify({'token': token}), 200 return jsonify({'error': True, 'message': error}), 400
def create(): if request.method == 'POST': title = request.form['title'] body = request.form['body'] error = None if not title: error = 'Title is required.' if error is not None: flash(error) else: db = get_db() db.execute('INSERT INTO todo (title, body)' ' VALUES (?, ?)', (title, body)) db.commit() return redirect(url_for('.index')) return render_template('todos/create.html')
def update(id): item = get_item(id) if request.method == 'POST': title = request.form['title'] body = request.form['body'] error = None if not title: error = 'Title is required.' if error is not None: flash(error) else: db = get_db() db.execute('UPDATE todo SET title = ?, body = ?' ' WHERE id = ?', (title, body, id)) db.commit() return redirect(url_for('todos.index')) return render_template('todos/update.html', item=item)
def editTask(id): db = get_db() user_id = get_jwt_identity() error = None if not request.json['title']: error = 'Title is required.' elif not request.json['body']: error = 'Body is required.' task = db.execute('SELECT * FROM task WHERE user_id = ? AND id = ?', (user_id, id)).fetchone() if task is None: error = "This task doesn't exist" if error is None: db.execute( 'UPDATE task SET title = ?, body = ?WHERE user_id = ? AND id = ?', (request.json['title'], request.json['body'], user_id, id)) db.commit() task = db.execute('SELECT * FROM task WHERE user_id = ? AND id = ?', (user_id, id)).fetchone() return jsonify(dict(task)), 200 return jsonify({'error': True, 'message': error}), 404
def register(): db = get_db() error = None if not request.json['username']: error = 'Username is required.' elif not request.json['password']: error = 'Password is required.' elif db.execute( 'SELECT id FROM user WHERE username = ?', (request.json['username'],) ).fetchone() is not None: error = 'User {} is already registered.'.format(request.json['username']) if error is None: db.execute( 'INSERT INTO user (username, password) VALUES (?, ?)', (request.json['username'], generate_password_hash(request.json['password'])) ) db.commit() last_row_id = db.execute('SELECT last_insert_rowid()').fetchone() request.json['id'] = last_row_id['last_insert_rowid()'] return jsonify(request.json), 200 return jsonify({'error': True, 'message': error}), 400
def delete(id): get_item(id) db = get_db() db.execute('DELETE FROM todo WHERE id = ?', (id, )) db.commit() return redirect(url_for('todos.index'))
def index(): db = get_db() items = db.execute('SELECT id, title, body, created ' ' FROM todo' ' ORDER by created DESC').fetchall() return render_template('todos/index.html', items=items)