-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
58 lines (46 loc) · 1.72 KB
/
app.py
File metadata and controls
58 lines (46 loc) · 1.72 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import json
from flask import Flask, request
from db import db, Meal
app = Flask(__name__)
db_filename = 'todo.db'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///%s' % db_filename
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_ECHO'] = True
db.init_app(app)
with app.app_context():
db.create_all()
@app.route('/')
def Hell():
return "links for requests: \n To get all meals: '/api/meals/' \n To post a meal: post body - {title : String} - '/api/meal' \n To get a specific meal by id: '/api/meal/{id}'"
@app.route('/meals/')
def getMeals():
allMeals = Meal.query.all()
res = [meal.serialize() for meal in allMeals]
return json.dumps(res), 200
@app.route('/meal/', methods=["POST"])
def createMeal():
meal = json.loads(request.data)
title = meal['title']
data = Meal(title = title)
db.session.add(data)
db.session.commit()
res = data.serialize()
return json.dumps(res), 201
@app.route('/meal/<int:meal_id>/', methods=["POST"])
def checkMeal(meal_id):
meal = Meal.query.filter_by(id=meal_id).first()
if meal is None:
return (json.dumps({'success': False, 'data': 'Not valid ID'})), 404
meal.completed = (not meal.completed)
db.session.commit()
return json.dumps({'success': True, 'data': meal.serialize()}), 200
@app.route('/meal/<int:meal_id>/', methods=["DELETE"])
def deleteMeal(meal_id):
meal = Meal.query.filter_by(id=meal_id).first()
if meal is None:
return (json.dumps({'success': False, 'data': 'Not valid ID'})), 404
db.session.delete(meal)
db.session.commit()
return json.dumps({'success': True, 'data': meal.serialize()}), 200
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)