+ gen mode

This commit is contained in:
xds
2026-02-02 23:08:47 +03:00
parent e593366c53
commit 4f533edf50
9 changed files with 576 additions and 106 deletions

View File

@@ -1,3 +1,6 @@
from typing import List
from bson import ObjectId
from motor.motor_asyncio import AsyncIOMotorClient
from models.Character import Character
@@ -12,5 +15,23 @@ class CharacterRepo:
character.id = op.inserted_id
return character
async def get_character(self, character_id: int) -> Character:
return await self.collection.find_one({"id": character_id})
async def get_character(self, character_id: str) -> Character | None:
res = await self.collection.find_one({"_id": ObjectId(character_id)})
if res is None:
return None
else:
res["id"] = str(res.pop("_id"))
return Character(**res)
async def get_all_characters(self) -> List[Character]:
docs = await self.collection.find().to_list(None)
characters = []
for doc in docs:
# Конвертируем ObjectId в строку и кладем в поле id
doc["id"] = str(doc.pop("_id"))
# Создаем объект
characters.append(Character(**doc))
return characters