feat: add /select_choice route
This commit is contained in:
+29
-8
@@ -2,13 +2,14 @@ import uvicorn
|
|||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
from schemas.api_schemas import *
|
from schemas.api_schemas import *
|
||||||
|
from schemas.base_schemas import *
|
||||||
from mongo_worker import MongoWorker
|
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
|
from fastapi import FastAPI, Depends, Response, status
|
||||||
|
|
||||||
|
|
||||||
app = FastAPI()
|
app: FastAPI = FastAPI()
|
||||||
|
|
||||||
@app.get("/check_user", status_code=200)
|
@app.get("/check_user", status_code=200)
|
||||||
async def check_user(user_id: NonNegativeInt,
|
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
|
response.status_code = status.HTTP_404_NOT_FOUND
|
||||||
return BaseResponse(result="No active cards", error=True)
|
return BaseResponse(result="No active cards", error=True)
|
||||||
else:
|
else:
|
||||||
result = list()
|
result: list[Card] = list()
|
||||||
trys = 3
|
trys = 3
|
||||||
while len(result) < 10 and trys != 0:
|
while len(result) < 10 and trys != 0:
|
||||||
random_cards = mongo.get_random_cards(10, True)
|
random_cards = mongo.get_random_cards(10, True)
|
||||||
@@ -91,23 +92,43 @@ async def get_random_cards(user_id: NonNegativeInt,
|
|||||||
else:
|
else:
|
||||||
return BaseResponse(result=result)
|
return BaseResponse(result=result)
|
||||||
|
|
||||||
|
|
||||||
@app.post("/add_card", status_code=201)
|
@app.post("/add_card", status_code=201)
|
||||||
async def add_card(new_card: AddCardBody,
|
async def add_card(new_card: AddCardBody,
|
||||||
response: Response,
|
response: Response,
|
||||||
mongo: MongoWorker = Depends(MongoWorker)) -> BaseResponse:
|
mongo: MongoWorker = Depends(MongoWorker)) -> BaseResponse:
|
||||||
if moderate_text(new_card.choice_A) and moderate_text(new_card.choice_B):
|
if moderate_text(new_card.choice_A) and moderate_text(new_card.choice_B):
|
||||||
card = mongo.add_card(new_card.choice_A,
|
card = mongo.add_card_by_api(new_card.choice_A,
|
||||||
new_card.choice_B,
|
new_card.choice_B,
|
||||||
new_card.author_id)
|
new_card.author_id)
|
||||||
return BaseResponse(result=card)
|
return BaseResponse(result=card)
|
||||||
else:
|
else:
|
||||||
response.status_code = status.HTTP_400_BAD_REQUEST
|
response.status_code = status.HTTP_400_BAD_REQUEST
|
||||||
return BaseResponse(result="Card has not passed base moderation", error=True)
|
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():
|
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)
|
server = uvicorn.Server(config)
|
||||||
await server.serve()
|
await server.serve()
|
||||||
|
|
||||||
|
|||||||
+10
-1
@@ -83,7 +83,7 @@ class MongoWorker:
|
|||||||
return Visited.model_validate(update_visited)
|
return Visited.model_validate(update_visited)
|
||||||
|
|
||||||
|
|
||||||
def add_card(self, choice_A: str, choice_B: str, author_id: int) -> Card:
|
def add_card_by_api(self, choice_A: str, choice_B: str, author_id: int) -> Card:
|
||||||
new_card = Card(card_id=self.get_and_update_counter(counter_name="card"),
|
new_card = Card(card_id=self.get_and_update_counter(counter_name="card"),
|
||||||
choice_A=choice_A,
|
choice_A=choice_A,
|
||||||
choice_B=choice_B,
|
choice_B=choice_B,
|
||||||
@@ -96,6 +96,15 @@ class MongoWorker:
|
|||||||
print(exception)
|
print(exception)
|
||||||
return new_card
|
return new_card
|
||||||
|
|
||||||
|
def add_card_by_base_model(self, new_card: Card) -> Card:
|
||||||
|
new_card.card_id = self.get_and_update_counter(counter_name="card")
|
||||||
|
try:
|
||||||
|
self.game_data.insert_one(new_card.model_dump())
|
||||||
|
return new_card
|
||||||
|
except Exception as exception:
|
||||||
|
print(exception)
|
||||||
|
return new_card
|
||||||
|
|
||||||
def get_card(self, card_id: int) -> Optional[Card]:
|
def get_card(self, card_id: int) -> Optional[Card]:
|
||||||
document = self.game_data.find_one({"card_id": card_id})
|
document = self.game_data.find_one({"card_id": card_id})
|
||||||
if document:
|
if document:
|
||||||
|
|||||||
@@ -19,4 +19,10 @@ class AddCardBody(BaseModel):
|
|||||||
choice_A: str
|
choice_A: str
|
||||||
choice_B: str
|
choice_B: str
|
||||||
|
|
||||||
author_id: NonNegativeInt
|
author_id: NonNegativeInt
|
||||||
|
|
||||||
|
class SelectChoice(BaseModel):
|
||||||
|
user_id: NonNegativeInt
|
||||||
|
card_id: NonNegativeInt
|
||||||
|
|
||||||
|
choice: typing.Literal["A", "B"]
|
||||||
@@ -4,7 +4,7 @@ import json
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
sys.path.append('..')
|
#sys.path.append('..')
|
||||||
from mongo_worker import MongoWorker
|
from mongo_worker import MongoWorker
|
||||||
from schemas.base_schemas import Card
|
from schemas.base_schemas import Card
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import re
|
import re
|
||||||
|
|
||||||
from dirty_words import dirty_words_set
|
from tools.data.dirty_words import dirty_words_set
|
||||||
|
|
||||||
def is_not_empty(text: str) -> bool:
|
def is_not_empty(text: str) -> bool:
|
||||||
"""Checks that the text is not empty."""
|
"""Checks that the text is not empty."""
|
||||||
@@ -31,8 +31,8 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"card_id": 3,
|
"card_id": 3,
|
||||||
"choice_A": "Поездка в горы",
|
"choice_A": "Отпуск в горах",
|
||||||
"choice_B": "Поездка на море",
|
"choice_B": "Отпуск на море",
|
||||||
"count_choice_A": 0,
|
"count_choice_A": 0,
|
||||||
"count_choice_B": 0,
|
"count_choice_B": 0,
|
||||||
"count_total": 0,
|
"count_total": 0,
|
||||||
|
|||||||
Reference in New Issue
Block a user