forked from vubon/python-rest-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.py
More file actions
executable file
·25 lines (21 loc) · 977 Bytes
/
query.py
File metadata and controls
executable file
·25 lines (21 loc) · 977 Bytes
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
import json
from db.database_connection import db
def fetch_all_recipes():
# db.execute(
# "SELECT recipes.id, name, pre_time, difficulty, vegetarian, created_at, ROUND(AVG(rated),2) FROM recipes INNER JOIN recipe_rating ON recipe_id=id GROUP BY recipes.id")
db.execute(
"SELECT recipes.id, name, pre_time, difficulty, vegetarian, created_at, ROUND(AVG(rated),2) "
"FROM recipes LEFT JOIN recipe_rating ON recipe_id=id GROUP BY recipes.id ORDER BY id ASC")
recipes = []
for item in db.fetchall():
data = dict()
data['id'] = item[0]
data['name'] = item[1]
data['pre_time'] = item[2]
data['difficulty'] = item[3]
data['vegetarian'] = item[4]
data['created_at'] = item[5].strftime('%Y-%m-%dT%H:%M:%S')
data['average_rating'] = str(item[6])
recipes.append(data)
# serializer = json.dumps(db.fetchall(), indent=4, sort_keys=True, default=str)
return recipes