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

84 lines
3.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from apiflask import APIBlueprint
from flask import session
from app import rpc
from app.api.v1.exception.record import AddBodyRecordError, BodyRecordNotFound, BodyRecordChartError
from app.api.v1.schema.record import BodyRecordIn, BodyRecordOut, BodyRecordsOut, BodyRecordsIn, BodyRecordChartIn, \
BodyRecordChartOut, BodyRecordIdIn
from app.util.auth import login_required
record = APIBlueprint('record', __name__)
@record.post('/')
@record.doc(summary='添加身体记录', description='添加身体记录')
@record.input(BodyRecordIn, location='json')
@login_required
def add_record(json_data):
try:
rpc.body_record.add(**{
'user_id': session['user_id'],
'height': json_data['height'],
'weight': json_data['weight'],
'bmi': json_data['bmi']
})
except Exception as e:
raise AddBodyRecordError(extra_data={'error_docs': str(e)})
return {'msg': 'add body record success'}
@record.get('/<record_id>')
@record.doc(summary='查询单条身体记录', description='查询单条身体记录')
@record.output(BodyRecordOut)
@login_required
def get_record(record_id):
try:
result = rpc.body_record.get_one(record_id)
except Exception as e:
raise BodyRecordNotFound(extra_data={'error_docs': str(e)})
return result
@record.delete('/<record_id>')
@record.doc(summary='删除单条身体记录', description='删除单条身体记录')
@login_required
def delete_record(record_id):
try:
rpc.body_record.delete_one(record_id)
except Exception as e:
raise BodyRecordNotFound(extra_data={'error_docs': str(e)})
return {'msg': ' delete body record success'}
@record.get('/list')
@record.doc(summary='查询身体记录列表', description='查询身体记录列表')
@record.input(BodyRecordsIn, location='query')
@record.output(BodyRecordsOut)
@login_required
def get_records(query_data):
try:
sort = query_data.get('sort', '-create_time')
current_date = query_data.get('current_date', None)
results = rpc.body_record.get_all_by_user(session['user_id'],
sort=sort,
current_date=current_date)
except Exception as e:
raise BodyRecordNotFound(extra_data={'error_docs': str(e)})
return {'records': results}
@record.get('/chart')
@record.doc(summary='查询身体记录图表', description='查询身体记录图表。注period 参数有 ["week", "day", "month"] ,如果不传,则默认是 week')
@record.input(BodyRecordChartIn, location='query')
@record.output(BodyRecordChartOut)
@login_required
def get_record_charts(query_data):
user_id = query_data['user_id'] if query_data.get('user_id') else session['user_id']
try:
period = query_data.get('period', 'week')
current_date = query_data['current_date']
result = rpc.body_record.get_record_charts(user_id, current_date, period)
except Exception as e:
raise BodyRecordChartError(extra_data={'error_docs': str(e)})
return result