feat: add /comment rout
This commit is contained in:
+18
@@ -148,6 +148,24 @@ async def dislike_card(dislike_data: ReactionCard,
|
|||||||
response.status_code = status.HTTP_404_NOT_FOUND
|
response.status_code = status.HTTP_404_NOT_FOUND
|
||||||
return result
|
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():
|
async def main():
|
||||||
config = uvicorn.Config("main:app", port=5000, log_level="debug")
|
config = uvicorn.Config("main:app", port=5000, log_level="debug")
|
||||||
|
|||||||
+25
-2
@@ -16,13 +16,13 @@ class MongoWorker:
|
|||||||
self.client = pymongo.MongoClient(host = os.getenv('MONGO_HOST'),
|
self.client = pymongo.MongoClient(host = os.getenv('MONGO_HOST'),
|
||||||
port = int(os.getenv('MONGO_PORT')),
|
port = int(os.getenv('MONGO_PORT')),
|
||||||
username = os.getenv('MONGO_USER'),
|
username = os.getenv('MONGO_USER'),
|
||||||
password = os.getenv('MONGO_PASS'),
|
password = os.getenv('MONGO_PASS'))
|
||||||
uuidRepresentation="standard")
|
|
||||||
self.db = self.client["data"]
|
self.db = self.client["data"]
|
||||||
self.users_data = self.db["users"]
|
self.users_data = self.db["users"]
|
||||||
self.visited_data = self.db["visited"]
|
self.visited_data = self.db["visited"]
|
||||||
self.counters = self.db["counters"]
|
self.counters = self.db["counters"]
|
||||||
self.game_data = self.db["cards"]
|
self.game_data = self.db["cards"]
|
||||||
|
self.comments_data = self.db["comments"]
|
||||||
|
|
||||||
|
|
||||||
def check_user(self, user_id: int) -> bool:
|
def check_user(self, user_id: int) -> bool:
|
||||||
@@ -190,6 +190,29 @@ class MongoWorker:
|
|||||||
else:
|
else:
|
||||||
return BaseResponse(result="User doesn't exist", error=True)
|
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__":
|
if __name__ == "__main__":
|
||||||
mongo = MongoWorker()
|
mongo = MongoWorker()
|
||||||
|
|||||||
@@ -30,3 +30,9 @@ class SelectChoice(BaseModel):
|
|||||||
class ReactionCard(BaseModel):
|
class ReactionCard(BaseModel):
|
||||||
user_id: NonNegativeInt
|
user_id: NonNegativeInt
|
||||||
card_id: NonNegativeInt
|
card_id: NonNegativeInt
|
||||||
|
|
||||||
|
class AddCommentBody(BaseModel):
|
||||||
|
author_id: NonNegativeInt
|
||||||
|
card_id: NonNegativeInt
|
||||||
|
|
||||||
|
comment_text: str
|
||||||
@@ -37,3 +37,12 @@ class Card(BaseModel):
|
|||||||
creation_date: str
|
creation_date: str
|
||||||
moderation_date: str = "Not moderated"
|
moderation_date: str = "Not moderated"
|
||||||
active_status: bool = False
|
active_status: bool = False
|
||||||
|
|
||||||
|
class Comment(BaseModel):
|
||||||
|
comment_id: int
|
||||||
|
|
||||||
|
author_id: int
|
||||||
|
card_id: int
|
||||||
|
commet_text: str
|
||||||
|
|
||||||
|
creation_date: str
|
||||||
Reference in New Issue
Block a user