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

53 lines
1.7 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
from app.api.v1.exception.record import AddBodyRecordError, BodyRecordNotFound
2023-08-13 21:08:29 +08:00
from app.api.v1.schema.record import BodyRecordIn, BodyRecordOut, BodyRecordsOut, BodyRecordsIn
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
@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')
2023-08-13 21:08:29 +08:00
results = rpc.body_record.get_all_by_user(session['user_id'], sort)
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}