-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
66 lines (48 loc) · 1.74 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
import os
app = Flask(__name__)
if __name__ == '__main__':
app.run(debug=True)
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL')
db = SQLAlchemy(app)
class Task(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80), unique=True, nullable=False)
description = db.Column(db.String(250), unique=True, nullable=False)
complete = db.Column(db.Boolean, nullable=False)
def __init__(self, title, description, complete):
self.title = title
self.description = description
self.complete = complete
db.create_all()
@app.route('/tasks/<id>', methods=['GET'])
def get_task(id):
task = Task.query.get(id)
del task.__dict__['_sa_instance_state']
return jsonify(task.__dict__)
@app.route('/tasks', methods=['GET'])
def get_tasks():
tasks = []
for task in db.session.query(Task).all():
del task.__dict__['_sa_instance_state']
tasks.append(task.__dict__)
return jsonify(tasks)
@app.route('/tasks', methods=['POST'])
def create_task():
body = request.get_json()
db.session.add(Task(body['title'], body['description'], body['completed']))
db.session.commit()
return "Task created"
@app.route('/tasks/<id>', methods=['PUT'])
def update_task(id):
body = request.get_json()
db.session.query(Task).filter_by(id=id).update(
dict(title=body['title'], description=body['description'], completed=body['completed']))
db.session.commit()
return "Task updated"
@app.route('/tasks/<id>', methods=['DELETE'])
def delete_task(id):
db.session.query(Task).filter_by(id=id).delete()
db.session.commit()
return "task deleted"