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

41 lines
1.4 KiB
Python

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):
user_id = query_data['user_id'] if query_data.get('user_id') else session['user_id']
try:
sort = query_data.get('sort', '-create_time')
current_date = query_data.get('current_date', None)
results = rpc.diet.get_all_by_user(user_id,
sort=sort,
current_date=current_date)
except Exception as e:
raise DietRecordNotFound(extra_data={'error_docs': str(e)})
return 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