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):
|
2023-09-21 16:56:13 +08:00
|
|
|
user_id = query_data['user_id'] if query_data.get('user_id') else session['user_id']
|
2023-09-15 18:00:17 +08:00
|
|
|
try:
|
|
|
|
sort = query_data.get('sort', '-create_time')
|
|
|
|
current_date = query_data.get('current_date', None)
|
2023-09-21 16:56:13 +08:00
|
|
|
results = rpc.diet.get_all_by_user(user_id,
|
2023-09-15 18:00:17 +08:00
|
|
|
sort=sort,
|
|
|
|
current_date=current_date)
|
|
|
|
except Exception as e:
|
|
|
|
raise DietRecordNotFound(extra_data={'error_docs': str(e)})
|
2023-09-21 15:34:54 +08:00
|
|
|
return results
|
2023-09-15 18:00:17 +08:00
|
|
|
|
|
|
|
|
|
|
|
@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
|