-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroutes.py
More file actions
42 lines (35 loc) · 1.13 KB
/
Copy pathroutes.py
File metadata and controls
42 lines (35 loc) · 1.13 KB
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
from flask import jsonify, request
from app import app, db
from models import Task, task_schema, tasks_schema
@app.route('/tasks', methods=['POST'])
def create_task():
title = request.json['title']
description = request.json['description']
new_task = Task(title, description)
db.session.add(new_task)
db.session.commit()
return task_schema.jsonify(new_task)
@app.route('/tasks', methods=['GET'])
def get_tasks():
all_tasks = Task.query.all()
result = tasks_schema.dump(all_tasks)
return jsonify(result)
@app.route('/tasks/<id>', methods=['GET'])
def get_task(id):
task = Task.query.get(id)
return task_schema.jsonify(task)
@app.route('/tasks/<id>', methods=['PUT'])
def update_task(id):
task = Task.query.get(id)
title = request.json['title']
description = request.json['description']
task.title = title
task.description = description
db.session.commit()
return task_schema.jsonify(task)
@app.route('/tasks/<id>', methods=['DELETE'])
def delete_task(id):
task = Task.query.get(id)
db.session.delete(task)
db.session.commit()
return task_schema.jsonify(task)