-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
59 lines (51 loc) · 1.62 KB
/
app.py
File metadata and controls
59 lines (51 loc) · 1.62 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
59
from flask import Flask,jsonify,request
app=Flask(__name__)
recipes = [
{"id": 1, "name": "Pasta", "ingredients": ["noodles", "sauce"], "time": 30},
{"id": 2, "name": "Pizza", "ingredients": ["dough", "cheese"], "time": 45}
]
@app.route("/")
def home():
return "Hello"
@app.route("/recipes",methods=["GET"])
def get_recipe():
return jsonify(recipes),200
@app.route("/recipes/<int:rec_id>",methods=["GET"])
def get_recipe_id(rec_id):
recipe=next((r for r in recipes if r['id']==rec_id),None )
if recipe:
return jsonify(recipe),200
return jsonify({"Error":"Recipe Not found"}),404
@app.route("/recipes",methods=["POST"])
def post_recipe():
data=request.get_json()
if recipes:
new_id=recipes[-1]['id']+1
else:
new_id=1
new_recipe={
"id":new_id,
"name":data["name"],
"ingredients":data["ingredients"],
"time":data["time"]
}
recipes.append(new_recipe)
return jsonify(new_recipe),201
@app.route("/recipes/<int:upd_id>",methods=["PUT"])
def update_recipe(upd_id):
data=request.get_json()
for recipe in recipes:
if recipe['id']==upd_id:
recipe.update(data)
return jsonify(recipes),200
return jsonify({"error":"Recipe not found"}),404
@app.route("/recipes/<int:del_id>",methods=["DELETE"])
def del_recipe(del_id):
global recipes
orginal_len=len(recipes)
recipes=[r for r in recipes if r["id"]!=del_id]
if orginal_len>len(recipes):
return jsonify({"Message":"Suceesfully deleted"})
return jsonify({"Error":"Not found"})
if __name__ == "__main__":
app.run(debug=True)