-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
104 lines (76 loc) · 2.4 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS
from flask_marshmallow import Marshmallow
app = Flask(__name__)
cors = CORS(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database/todo.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
ma = Marshmallow(app)
class Todo(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(200), nullable=False)
description = db.Column(db.String(200), nullable=False)
done = db.Column(db.String(5), nullable=False)
def __init__(self, title, description, done):
self.title = title
self.description = description
self.done = done
class TodoSchema(ma.Schema):
class Meta:
fields = ['id', 'title', 'description', 'done']
todo_Schema = TodoSchema()
todos_Schema = TodoSchema(many=True)
""" Welcome Api Rest """
@app.route('/', methods=['GET'])
def index():
return jsonify({
"message": "Welcome to my API!!"
})
""" Get all tasks """
@app.route('/todos', methods=['GET'])
def getTodos():
allTodos = Todo.query.all()
results = todos_Schema.dump(allTodos)
return jsonify(results)
""" Create task """
@app.route('/create', methods=['POST'])
def createTodo():
title = request.json['title']
description = request.json['description']
done = request.json['done']
new_todo = Todo(title, description, done)
db.session.add(new_todo)
db.session.commit()
result = todo_Schema.jsonify(new_todo)
return result
""" Get single task """
@app.route('/todo/<id>', methods=['GET'])
def getTodo(id):
todo = Todo.query.get(id)
result = todo_Schema.jsonify(todo)
return result
""" Edit task """
@app.route('/edit/<id>', methods=['PUT'])
def editTodo(id):
edit_todo = Todo.query.get(id)
title = request.json['title']
description = request.json['description']
done = request.json['done']
edit_todo.title = title
edit_todo.description = description
edit_todo.done = done
db.session.commit()
result = todo_Schema.jsonify(edit_todo)
return result
""" Delete task """
@app.route('/delete/<id>', methods=['DELETE'])
def deleteTodo(id):
delete_todo = Todo.query.get(id)
db.session.delete(delete_todo)
db.session.commit()
result = todo_Schema.jsonify(delete_todo)
return result
if __name__ == '__main__':
app.run(debug=True)