pzx-web-api/app/api/v1/diet.py

40 lines
1.3 KiB
Python
Raw Normal View History

2023-09-15 18:00:17 +08:00
from apiflask import APIBlueprint
from flask import session
from app import rpc
from app.api.v1.exception.diet import DietRecordNotFound
from app.api.v1.schema.diet import DietRecordsIn, DietRecordsOut, DietRecordOut
from app.util.auth import login_required
diet = APIBlueprint('diet', __name__)
@diet.get('/list')
@diet.doc(summary='查询食物记录列表', description='查询食物记录列表')
@diet.input(DietRecordsIn, location='query')
@diet.output(DietRecordsOut)
@login_required
def get_diet_records(query_data):
try:
sort = query_data.get('sort', '-create_time')
current_date = query_data.get('current_date', None)
results = rpc.diet.get_all_by_user(session['user_id'],
sort=sort,
current_date=current_date)
except Exception as e:
raise DietRecordNotFound(extra_data={'error_docs': str(e)})
return {'records': results}
@diet.get('/<diet_id>')
@diet.doc(summary='查询单条食物记录', description='查询单条食物记录')
@diet.output(DietRecordOut)
@login_required
def get_diet_record(diet_id):
try:
result = rpc.diet.get_one(diet_id)
except Exception as e:
raise DietRecordNotFound(extra_data={'error_docs': str(e)})
return result