-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmonthly.py
More file actions
75 lines (61 loc) · 2.31 KB
/
monthly.py
File metadata and controls
75 lines (61 loc) · 2.31 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# monthly.py
from flask import Flask, request, jsonify
import pymysql
import os
from dotenv import load_dotenv
load_dotenv()
app = Flask(__name__)
# DB Connection
db_config = {
'host': os.getenv('DB_HOST'),
'user': os.getenv('DB_USER'),
'password': os.getenv('DB_PASSWORD'),
'database': os.getenv('DB_NAME')
}
@app.route('/api/food/monthly', methods=['GET'])
def get_monthly_food():
year = request.args.get('year')
month = request.args.get('month')
if not year or not month:
return jsonify({"error": "Year and month are required"}), 400
connection = pymysql.connect(**db_config)
try:
with connection.cursor() as cursor:
sql = """
SELECT DATE, FOOD_INDEX, FOOD_NAME, FOOD_PT, FOOD_FAT, FOOD_CH, FOOD_KCAL
FROM FOOD
WHERE YEAR(DATE) = %s AND MONTH(DATE) = %s
ORDER BY DATE
"""
cursor.execute(sql, (year, month))
results = cursor.fetchall()
monthly_data = {}
for row in results:
day = row[0].day
food_info = {
"food_index": row[1],
"food_name": row[2],
"protein": row[3],
"fat": row[4],
"carbohydrates": row[5],
"calories": row[6]
}
# Ensuring the output order
food_info_ordered = {
"food_index": food_info["food_index"],
"food_name": food_info["food_name"],
"protein": food_info["protein"],
"fat": food_info["fat"],
"carbohydrates": food_info["carbohydrates"],
"calories": food_info["calories"]
}
if day not in monthly_data:
monthly_data[day] = []
monthly_data[day].append(food_info_ordered)
# Create a list of 31 days, each day is a list of food items (which may be empty)
grouped_data = [monthly_data.get(day, []) for day in range(1, 32)]
return jsonify(grouped_data)
finally:
connection.close()
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5001) # 다른 포트로 실행하여 send.py와 충돌 방지