From f092e189c262e3487b7da77970fc5c2717b8ca1c Mon Sep 17 00:00:00 2001 From: IgorVolochay Date: Thu, 6 Mar 2025 16:28:09 +0300 Subject: [PATCH] feat: add /comment rout --- app/main.py | 18 ++++++++++++++++++ app/mongo_worker.py | 27 +++++++++++++++++++++++++-- app/schemas/api_schemas.py | 8 +++++++- app/schemas/base_schemas.py | 11 ++++++++++- 4 files changed, 60 insertions(+), 4 deletions(-) diff --git a/app/main.py b/app/main.py index 21ead3c..c8d6da3 100644 --- a/app/main.py +++ b/app/main.py @@ -148,6 +148,24 @@ async def dislike_card(dislike_data: ReactionCard, response.status_code = status.HTTP_404_NOT_FOUND return result +@app.post("/comment", status_code=201) +async def comment(comment_info: AddCommentBody, + response: Response, + mongo: MongoWorker = Depends(MongoWorker)) -> BaseResponse: + if not moderate_text(comment_info.comment_text): + response.status_code = status.HTTP_400_BAD_REQUEST + return BaseResponse(result="Comment has not passed base moderation", error=True) + + result = mongo.add_comment(comment_info.author_id, comment_info.card_id, comment_info.comment_text) + if result.error and result.result in ["User doesn't exist", "Card doesn't exist"]: + response.status_code = status.HTTP_404_NOT_FOUND + return result + elif result.error: + response.status_code = status.HTTP_400_BAD_REQUEST + return result + else: + return result + async def main(): config = uvicorn.Config("main:app", port=5000, log_level="debug") diff --git a/app/mongo_worker.py b/app/mongo_worker.py index 5ffeadc..f6496ef 100644 --- a/app/mongo_worker.py +++ b/app/mongo_worker.py @@ -16,13 +16,13 @@ class MongoWorker: self.client = pymongo.MongoClient(host = os.getenv('MONGO_HOST'), port = int(os.getenv('MONGO_PORT')), username = os.getenv('MONGO_USER'), - password = os.getenv('MONGO_PASS'), - uuidRepresentation="standard") + password = os.getenv('MONGO_PASS')) self.db = self.client["data"] self.users_data = self.db["users"] self.visited_data = self.db["visited"] self.counters = self.db["counters"] self.game_data = self.db["cards"] + self.comments_data = self.db["comments"] def check_user(self, user_id: int) -> bool: @@ -189,6 +189,29 @@ class MongoWorker: return BaseResponse(result=True, error=False) else: return BaseResponse(result="User doesn't exist", error=True) + + def add_comment(self, user_id: int, card_id: int, comment_text: str) -> BaseResponse: + if self.check_user(user_id): + if self.get_card(card_id): + new_comment = Comment(comment_id=self.get_and_update_counter(counter_name="comment"), + author_id=user_id, + card_id=card_id, + commet_text=comment_text, + creation_date=datetime.now().isoformat()) + result = self.comments_data.insert_one(new_comment.model_dump()) + if result: + update_user_comments = self.users_data.find_one_and_update({"user_id": user_id}, + {"$addToSet": {"comments_ids": new_comment.comment_id}}) + if update_user_comments: + return BaseResponse(result=new_comment) + else: + return BaseResponse(result="Difficulty adding comment_id to user", error=True) + else: + return BaseResponse(result="Add comment error", error=True) + else: + return BaseResponse(result="Card doesn't exist", error=True) + else: + return BaseResponse(result="User doesn't exist", error=True) if __name__ == "__main__": diff --git a/app/schemas/api_schemas.py b/app/schemas/api_schemas.py index 0cccd80..745d791 100644 --- a/app/schemas/api_schemas.py +++ b/app/schemas/api_schemas.py @@ -29,4 +29,10 @@ class SelectChoice(BaseModel): class ReactionCard(BaseModel): user_id: NonNegativeInt - card_id: NonNegativeInt \ No newline at end of file + card_id: NonNegativeInt + +class AddCommentBody(BaseModel): + author_id: NonNegativeInt + card_id: NonNegativeInt + + comment_text: str \ No newline at end of file diff --git a/app/schemas/base_schemas.py b/app/schemas/base_schemas.py index eb6bb71..52f5464 100644 --- a/app/schemas/base_schemas.py +++ b/app/schemas/base_schemas.py @@ -36,4 +36,13 @@ class Card(BaseModel): author_id: int creation_date: str moderation_date: str = "Not moderated" - active_status: bool = False \ No newline at end of file + active_status: bool = False + +class Comment(BaseModel): + comment_id: int + + author_id: int + card_id: int + commet_text: str + + creation_date: str \ No newline at end of file