feat: add /select_choice route

This commit is contained in:
IgorVolochay
2025-03-04 17:25:03 +03:00
parent 62d085b898
commit 2c545dc961
7 changed files with 50 additions and 14 deletions
+29 -8
View File
@@ -2,13 +2,14 @@ import uvicorn
import asyncio
from schemas.api_schemas import *
from schemas.base_schemas import *
from mongo_worker import MongoWorker
from base_moderation import moderate_text
from tools.base_moderation import moderate_text
from fastapi import FastAPI, Depends, Response, status
app = FastAPI()
app: FastAPI = FastAPI()
@app.get("/check_user", status_code=200)
async def check_user(user_id: NonNegativeInt,
@@ -71,7 +72,7 @@ async def get_random_cards(user_id: NonNegativeInt,
response.status_code = status.HTTP_404_NOT_FOUND
return BaseResponse(result="No active cards", error=True)
else:
result = list()
result: list[Card] = list()
trys = 3
while len(result) < 10 and trys != 0:
random_cards = mongo.get_random_cards(10, True)
@@ -91,23 +92,43 @@ async def get_random_cards(user_id: NonNegativeInt,
else:
return BaseResponse(result=result)
@app.post("/add_card", status_code=201)
async def add_card(new_card: AddCardBody,
response: Response,
mongo: MongoWorker = Depends(MongoWorker)) -> BaseResponse:
if moderate_text(new_card.choice_A) and moderate_text(new_card.choice_B):
card = mongo.add_card(new_card.choice_A,
new_card.choice_B,
new_card.author_id)
card = mongo.add_card_by_api(new_card.choice_A,
new_card.choice_B,
new_card.author_id)
return BaseResponse(result=card)
else:
response.status_code = status.HTTP_400_BAD_REQUEST
return BaseResponse(result="Card has not passed base moderation", error=True)
@app.patch("/select_choice", status_code=200)
async def select_choice(choice_data: SelectChoice,
response: Response,
mongo: MongoWorker = Depends(MongoWorker)) -> BaseResponse:
check_visited = mongo.get_visited_cards(choice_data.user_id)
if check_visited.error:
response.status_code = status.HTTP_404_NOT_FOUND
return check_visited
elif not check_visited.error and choice_data.card_id in check_visited.result.cards_visited:
response.status_code = status.HTTP_403_FORBIDDEN
return BaseResponse(result="Card already visited!", error=True)
else:
select_choice_result = mongo.select_choice(choice_data.card_id, choice_data.choice)
if select_choice_result.error:
response.status_code = status.HTTP_404_NOT_FOUND
return select_choice_result
else:
update_visited_result = mongo.update_visited_cards(choice_data.user_id, choice_data.card_id)
return BaseResponse(result="Select choice complite!")
async def main():
config = uvicorn.Config("main:app", port=5000, log_level="info")
config = uvicorn.Config("main:app", port=5000, log_level="debug")
server = uvicorn.Server(config)
await server.serve()