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

50 lines
1.5 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
from app.api.v1.schema.record import BodyRecordIn, BodyRecordOut, BodyRecordsIn, BodyRecordsOut
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('/')
@record.input(BodyRecordIn)
2023-08-12 20:23:19 +08:00
@record.doc(summary='添加身体记录', description='添加身体记录')
2023-08-12 20:00:38 +08:00
@login_required
def add_record(json_data):
try:
rpc.body_record.add(user_obj_id=session['user_id'],
height=json_data['height'],
weight=json_data['weight'],
bmi=json_data['bmi'])
except Exception as e:
raise AddBodyRecordError()
return {'msg': 'add body record success'}
@record.get('/<record_id>')
@record.output(BodyRecordOut)
2023-08-12 20:23:19 +08:00
@record.doc(summary='查询单条身体记录', description='查询单条身体记录')
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:
raise BodyRecordNotFound()
return result
@record.get('/list')
@record.input(BodyRecordsIn, location='query')
@record.output(BodyRecordsOut)
2023-08-12 20:23:19 +08:00
@record.doc(summary='查询身体记录列表', description='查询身体记录列表')
2023-08-12 20:00:38 +08:00
@login_required
2023-08-12 20:15:48 +08:00
def get_records(query_data):
2023-08-12 20:00:38 +08:00
try:
result = rpc.body_record.get_all_by_user(query_data['user_id'])
except Exception as e:
raise BodyRecordNotFound()
return result