Skip to content

Commit e9961a9

Browse files
committed
Create favoriting endpoint and corresponding docs
1 parent 25cd893 commit e9961a9

File tree

6 files changed

+258
-1
lines changed

6 files changed

+258
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,5 @@ And send requests to http://$SERVER\_IP:5000
4747
* [Pantry](pantry.md) : `/v1/pantry/`
4848
* [Photos](photos.md) : `/v1/photos/`
4949
* [Exp](exp.md) : `/v1/exp/`
50+
* [Favorite](favorite.md) : `/v1/favorite/`
5051

favorite.md

+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
2+
# API Reference: Favorite
3+
4+
The Favorite API supports basic CRUD operations favorited recipes
5+
6+
## Register
7+
8+
Used to create a new favorite recipe
9+
10+
**URL** : `/v1/favorite/create/<username>`
11+
12+
**Methods** : `POST`
13+
14+
**Auth required** : YES
15+
16+
**Data constraints**
17+
18+
```json
19+
{
20+
"recipe_name": "recipe name",
21+
"recipe_id": "unique recipe id",
22+
"picture": "url of recipe picture"
23+
}
24+
```
25+
26+
**Data example**
27+
28+
```json
29+
{
30+
"recipe_name": "chicken",
31+
"recipe_id": "400",
32+
"picture": "google.com"
33+
}
34+
```
35+
36+
### Success Response
37+
38+
**Code** : `200 OK`
39+
40+
**Content example**
41+
42+
```json
43+
{
44+
"recipe_name": "chicken",
45+
"recipe_id": "400",
46+
"picture": "google.com"
47+
}
48+
```
49+
50+
### Error Response
51+
52+
**Condition** : If <username> can't be found
53+
54+
**Code** : `404 Not Found`
55+
56+
**Content** :
57+
58+
```json
59+
{
60+
"error": "requested user not found"
61+
}
62+
```
63+
64+
**Condition** : If favorite recipe couldn't be created or saved
65+
66+
**Code** : `400 BAD REQUEST`
67+
68+
**Content** :
69+
70+
```json
71+
{
72+
"error": "could not favorite requested recipe"
73+
}
74+
```
75+
76+
77+
## Read
78+
79+
Used to get all favorite recipes associated with a given user
80+
81+
**URL** : `/v1/favorite/<username>`
82+
83+
**Methods** : `GET`
84+
85+
**Auth required** : YES
86+
87+
### Success Response
88+
89+
**Code** : `200 OK`
90+
91+
**Content example**
92+
93+
```json
94+
{
95+
"favorites": [{
96+
"picture": "google.com",
97+
"recipe_id": 400,
98+
"recipe_name": "chicken"
99+
}, {
100+
"picture": "google.com",
101+
"recipe_id": 401,
102+
"recipe_name": "chicken"
103+
}, {
104+
"picture": "google.com",
105+
"recipe_id": 402,
106+
"recipe_name": "chicken"
107+
}]
108+
}
109+
```
110+
111+
### Error Response
112+
113+
**Condition** : If <username> can't be found
114+
115+
**Code** : `404 Not Found`
116+
117+
**Content** :
118+
119+
```json
120+
{
121+
"error": "requested user not found"
122+
}
123+
```
124+
125+
126+
## Delete
127+
128+
Removes favorited recipe from the database for a particular user
129+
130+
**URL** : `/v1/favorite/delete/<username>?recipe_id=<recipe_id>`
131+
132+
**Methods** : `DELETE`
133+
134+
**Auth required** : YES
135+
136+
### Success Response
137+
138+
**Code** : `200 OK`
139+
140+
**Content example**
141+
142+
```json
143+
{
144+
"deleted": "<recipe_id>"
145+
}
146+
```
147+
148+
### Error Response
149+
150+
**Condition** : If the username passed does not correspond to a user in the db
151+
152+
**Code** : `404 NOT FOUND`
153+
154+
**Content** :
155+
156+
```json
157+
{
158+
"error": "requested user not found"
159+
}
160+
```
161+
162+
**Condition** : The requested favorited recipe could not be deleted
163+
164+
**Code** : `400 BAD REQUEST`
165+
166+
**Content** :
167+
168+
```json
169+
{
170+
"error": "Deletion unsuccessful"
171+
}
172+
```

lastMeal/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,7 @@ def create_app(test_config=None):
4141
from lastMeal.api.v1 import recipe
4242
app.register_blueprint(recipe.bp)
4343

44+
from lastMeal.api.v1 import favorite
45+
app.register_blueprint(favorite.bp)
46+
4447
return app

lastMeal/api/v1/favorite.py

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
from flask import (
2+
Blueprint, flash, g, redirect, render_template, request, session, url_for, make_response, Response
3+
)
4+
import json
5+
from lastMeal.models.user import User
6+
from lastMeal.models.favorite import Favorite
7+
from bson.objectid import ObjectId
8+
9+
from flask_jwt_extended import get_jwt_identity
10+
from flask_jwt_extended import jwt_required
11+
import requests
12+
13+
bp = Blueprint('favorite', __name__, url_prefix='/v1/favorite')
14+
15+
@bp.route('/create/<username>', methods=['POST'])
16+
#@jwt_required()
17+
def create_favorite(username):
18+
# if (username != get_jwt_identity()):
19+
# return ({"error": "unauthorized"}, 401)
20+
21+
request_data = request.json
22+
user = User.objects(username=username).first()
23+
if user == None:
24+
return ({"error": "requested user not found"}, 404)
25+
recipe_id = request_data["recipe_id"]
26+
if Favorite.objects(recipe_id=recipe_id).first() != None:
27+
return ({"error": "can't favorite a recipe twice"}, 400)
28+
recipe_name = request_data["recipe_name"]
29+
picture = request_data["picture"]
30+
31+
try:
32+
my_favorite = Favorite(user=user, recipe_id=recipe_id, recipe_name=recipe_name, picture=picture)
33+
my_favorite.save()
34+
return ({"recipe_name": recipe_name, "recipe_id": recipe_id, "picture": picture}, 201)
35+
except Exception as e:
36+
print(e)
37+
return ({"error": "could not favorite requested recipe"}, 400)
38+
39+
@bp.route('/delete/<username>', methods=["DELETE"])
40+
#@jwt_required()
41+
def delete_favorite(username):
42+
# if (username != get_jwt_identity()):
43+
# return ({"error": "unauthorized"}, 401)
44+
45+
recipe_id = request.args.get("recipe_id")
46+
47+
user = User.objects(username=username).first()
48+
if user == None:
49+
return ({"error": "requested user not found"}, 404)
50+
51+
try:
52+
Favorite.objects(user=user, recipe_id=recipe_id).delete()
53+
return ({"deleted": recipe_id}, 200)
54+
except Exception as e:
55+
print(e)
56+
return ({"error": "Deletion unsuccessful"}, 400)
57+
58+
@bp.route('/<username>', methods=["GET"])
59+
#@jwt_required()
60+
def read_favorite(username):
61+
# if (username != get_jwt_identity()):
62+
# return ({"error": "unauthorized"}, 401)
63+
user = User.objects(username=username).first()
64+
if user == None:
65+
return ({"error": "requested user not found"}, 404)
66+
favorites = Favorite.objects(user=user)
67+
info_list = []
68+
for item in favorites:
69+
info_list.append({"picture": item.picture, "recipe_id": int(item.recipe_id), "recipe_name": item.recipe_name})
70+
return ({"favorites": info_list}, 200)

lastMeal/models/favorite.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from mongoengine import *
2+
from mongoengine.errors import ValidationError
3+
from lastMeal.models.user import User
4+
5+
connect('lastMeal')
6+
7+
class Favorite(Document):
8+
recipe_id = DecimalField(required=True, unique=True)
9+
recipe_name = StringField(max_length=100, required=True)
10+
picture = StringField(max_length=100, required=True)
11+
user = ReferenceField(User, required=True)

pantry.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ Used to change the contents of an ingredient item
176176

177177
Removes ingredient from the database
178178

179-
**URL** : `/v1/pantry/delete/<ingredient_id>`
179+
**URL** : `/v1/pantry/delete/<username>?ingredient=<ingredient_id>`
180180

181181
**Methods** : `DELETE`
182182

0 commit comments

Comments
 (0)