From a55e4b97d655eaaa747bf9fdaaa2ab51944ccd0f Mon Sep 17 00:00:00 2001 From: BryantHe Date: Fri, 18 Aug 2023 11:26:33 +0800 Subject: [PATCH] feat: add api of body record chart --- app/api/v1/exception/record.py | 7 ++++++- app/api/v1/record.py | 20 ++++++++++++++++++-- app/api/v1/schema/record.py | 19 ++++++++++++++++++- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/app/api/v1/exception/record.py b/app/api/v1/exception/record.py index 2ebc554..7c0d90d 100644 --- a/app/api/v1/exception/record.py +++ b/app/api/v1/exception/record.py @@ -8,4 +8,9 @@ class AddBodyRecordError(HTTPError): class BodyRecordNotFound(HTTPError): status_code = 404 - message = '找不到该记录' \ No newline at end of file + message = '找不到该记录' + + +class BodyRecordChartError(HTTPError): + status_code = 500 + message = '查询身体记录图表错误' \ No newline at end of file diff --git a/app/api/v1/record.py b/app/api/v1/record.py index b14f00f..11f9ea6 100644 --- a/app/api/v1/record.py +++ b/app/api/v1/record.py @@ -2,8 +2,9 @@ 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, BodyRecordsOut, BodyRecordsIn +from app.api.v1.exception.record import AddBodyRecordError, BodyRecordNotFound, BodyRecordChartError +from app.api.v1.schema.record import BodyRecordIn, BodyRecordOut, BodyRecordsOut, BodyRecordsIn, BodyRecordChartIn, \ + BodyRecordChartOut from app.util.auth import login_required record = APIBlueprint('record', __name__) @@ -50,3 +51,18 @@ def get_records(query_data): except Exception as e: raise BodyRecordNotFound(extra_data={'error_docs': str(e)}) return {'records': results} + + +@record.get('/chart') +@record.doc(summary='查询身体记录图表', description='查询身体记录图表') +@record.input(BodyRecordChartIn, location='query') +@record.output(BodyRecordChartOut) +@login_required +def get_record_charts(query_data): + try: + period = query_data.get('period', 'week') + current_date = query_data['current_date'] + result = rpc.body_record.get_record_charts(session['user_id'], current_date, period) + except Exception as e: + raise BodyRecordChartError(extra_data={'error_docs': str(e)}) + return result diff --git a/app/api/v1/schema/record.py b/app/api/v1/schema/record.py index ccc2404..80e1813 100644 --- a/app/api/v1/schema/record.py +++ b/app/api/v1/schema/record.py @@ -22,4 +22,21 @@ class BodyRecordsIn(Schema): class BodyRecordsOut(Schema): - records = fields.List(fields.Nested(BodyRecordOut)) \ No newline at end of file + records = fields.List(fields.Nested(BodyRecordOut)) + + +class BodyRecordChartIn(Schema): + current_date = fields.String(required=True) + period = fields.String() + + +class BodyRecordChartNestedOut(Schema): + weights = fields.List(fields.Float()) + bmis = fields.List(fields.Float()) + x_axis = fields.List(fields.Float()) + + +class BodyRecordChartOut(Schema): + chart_data = fields.Nested(BodyRecordChartNestedOut) + previous_date = fields.String() + next_date = fields.String()