diff --git a/app/api/v1/__init__.py b/app/api/v1/__init__.py index f2a9bd8..ef2ebd7 100644 --- a/app/api/v1/__init__.py +++ b/app/api/v1/__init__.py @@ -5,6 +5,7 @@ from app.api.v1.auth import auth from app.api.v1.record import record from app.api.v1.user import user from app.api.v1.diet import diet +from app.api.v1.interaction import interaction def create_v1(): @@ -14,6 +15,7 @@ def create_v1(): bp_v1.register_blueprint(record, url_prefix='/record') bp_v1.register_blueprint(user, url_prefix='/user') bp_v1.register_blueprint(diet, url_prefix='/diet') + bp_v1.register_blueprint(interaction, url_prefix='/interaction') return bp_v1 diff --git a/app/api/v1/exception/interaction.py b/app/api/v1/exception/interaction.py new file mode 100644 index 0000000..27458e2 --- /dev/null +++ b/app/api/v1/exception/interaction.py @@ -0,0 +1,6 @@ +from apiflask import HTTPError + + +class AddInteractionError(HTTPError): + status_code = 500 + message = 'add interaction failed.' \ No newline at end of file diff --git a/app/api/v1/interaction.py b/app/api/v1/interaction.py new file mode 100644 index 0000000..4b67183 --- /dev/null +++ b/app/api/v1/interaction.py @@ -0,0 +1,28 @@ +from apiflask import APIBlueprint +from flask import session + +from app import rpc +from app.api.v1.exception.interaction import AddInteractionError +from app.api.v1.schema.interaction import InteractionIn + +from app.util.auth import login_required + +interaction = APIBlueprint('interaction', __name__) + + +@interaction.post('/') +@interaction.doc(summary='添加交互记录', description='添加交互记录,category 类型int,1:点赞,2:加油,3:评论') +@interaction.input(InteractionIn, location='json') +@login_required +def add_interaction(json_data): + try: + rpc.interaction.add(**{ + 'sender': session['user_id'], + 'receiver': json_data['receiver_id'], + 'category': json_data['category'], + 'related_party': json_data.get('related_party_id'), + 'comment': json_data.get('comment') + }) + except Exception as e: + raise AddInteractionError(extra_data={'error_docs': str(e)}) + return {'msg': 'add interaction successfully'} \ No newline at end of file diff --git a/app/api/v1/schema/interaction.py b/app/api/v1/schema/interaction.py new file mode 100644 index 0000000..6974a77 --- /dev/null +++ b/app/api/v1/schema/interaction.py @@ -0,0 +1,9 @@ +from apiflask import Schema +from apiflask import fields + + +class InteractionIn(Schema): + category = fields.Integer() + receiver_id = fields.String() + related_party_id = fields.String(allow_none=True) + comment = fields.String(allow_none=True) \ No newline at end of file