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

84 lines
3.0 KiB
Python
Raw Normal View History

2023-08-12 20:00:38 +08:00
from apiflask import APIBlueprint
from flask import session
from app import rpc
2023-08-18 11:26:33 +08:00
from app.api.v1.exception.record import AddBodyRecordError, BodyRecordNotFound, BodyRecordChartError
from app.api.v1.schema.record import BodyRecordIn, BodyRecordOut, BodyRecordsOut, BodyRecordsIn, BodyRecordChartIn, \
2023-09-15 18:00:17 +08:00
BodyRecordChartOut
2023-08-12 20:15:48 +08:00
from app.util.auth import login_required
2023-08-12 20:00:38 +08:00
record = APIBlueprint('record', __name__)
@record.post('/')
2023-08-12 20:23:19 +08:00
@record.doc(summary='添加身体记录', description='添加身体记录')
2023-08-12 22:22:22 +08:00
@record.input(BodyRecordIn, location='json')
2023-08-12 20:00:38 +08:00
@login_required
def add_record(json_data):
try:
2023-08-12 22:22:22 +08:00
rpc.body_record.add(**{
'user_id': session['user_id'],
'height': json_data['height'],
'weight': json_data['weight'],
'bmi': json_data['bmi']
})
2023-08-12 20:00:38 +08:00
except Exception as e:
2023-08-12 20:59:00 +08:00
raise AddBodyRecordError(extra_data={'error_docs': str(e)})
2023-08-12 20:00:38 +08:00
return {'msg': 'add body record success'}
@record.get('/<record_id>')
2023-08-12 20:23:19 +08:00
@record.doc(summary='查询单条身体记录', description='查询单条身体记录')
2023-08-12 22:22:22 +08:00
@record.output(BodyRecordOut)
2023-08-12 20:00:38 +08:00
@login_required
def get_record(record_id):
try:
result = rpc.body_record.get_one(record_id)
except Exception as e:
2023-08-14 15:58:48 +08:00
raise BodyRecordNotFound(extra_data={'error_docs': str(e)})
2023-08-12 20:00:38 +08:00
return result
2023-08-26 11:05:45 +08:00
@record.delete('/<record_id>')
@record.doc(summary='删除单条身体记录', description='删除单条身体记录')
@login_required
2023-08-26 11:14:22 +08:00
def delete_record(record_id):
2023-08-26 11:05:45 +08:00
try:
2023-08-26 11:14:22 +08:00
rpc.body_record.delete_one(record_id)
2023-08-26 11:05:45 +08:00
except Exception as e:
raise BodyRecordNotFound(extra_data={'error_docs': str(e)})
return {'msg': ' delete body record success'}
2023-08-12 20:00:38 +08:00
@record.get('/list')
2023-08-12 20:23:19 +08:00
@record.doc(summary='查询身体记录列表', description='查询身体记录列表')
2023-08-13 21:08:29 +08:00
@record.input(BodyRecordsIn, location='query')
2023-08-12 22:22:22 +08:00
@record.output(BodyRecordsOut)
2023-08-12 20:00:38 +08:00
@login_required
2023-08-13 21:08:29 +08:00
def get_records(query_data):
2023-08-12 20:00:38 +08:00
try:
2023-08-13 21:16:19 +08:00
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)
2023-08-12 20:00:38 +08:00
except Exception as e:
2023-08-14 15:58:48 +08:00
raise BodyRecordNotFound(extra_data={'error_docs': str(e)})
2023-08-12 20:34:25 +08:00
return {'records': results}
2023-08-18 11:26:33 +08:00
@record.get('/chart')
2023-08-25 11:57:02 +08:00
@record.doc(summary='查询身体记录图表', description='查询身体记录图表。注period 参数有 ["week", "day", "month"] ,如果不传,则默认是 week')
2023-08-18 11:26:33 +08:00
@record.input(BodyRecordChartIn, location='query')
@record.output(BodyRecordChartOut)
@login_required
def get_record_charts(query_data):
2023-08-23 11:11:15 +08:00
user_id = query_data['user_id'] if query_data.get('user_id') else session['user_id']
2023-08-18 11:26:33 +08:00
try:
period = query_data.get('period', 'week')
current_date = query_data['current_date']
2023-08-23 11:11:15 +08:00
result = rpc.body_record.get_record_charts(user_id, current_date, period)
2023-08-18 11:26:33 +08:00
except Exception as e:
raise BodyRecordChartError(extra_data={'error_docs': str(e)})
return result