+ api
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
from typing import Optional
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class Character(BaseModel):
|
||||
id: str | None
|
||||
name: str
|
||||
character_image_data: Optional[bytes] = None
|
||||
character_image_doc_tg_id: str
|
||||
character_image_tg_id: str | None
|
||||
character_bio: str
|
||||
|
||||
@@ -15,8 +15,11 @@ class CharacterRepo:
|
||||
character.id = op.inserted_id
|
||||
return character
|
||||
|
||||
async def get_character(self, character_id: str) -> Character | None:
|
||||
res = await self.collection.find_one({"_id": ObjectId(character_id)})
|
||||
async def get_character(self, character_id: str, with_image_data: bool = False) -> Character | None:
|
||||
args = {}
|
||||
if not with_image_data:
|
||||
args["character_image_data"] = 0
|
||||
res = await self.collection.find_one({"_id": ObjectId(character_id)}, args)
|
||||
if res is None:
|
||||
return None
|
||||
else:
|
||||
@@ -24,7 +27,7 @@ class CharacterRepo:
|
||||
return Character(**res)
|
||||
|
||||
async def get_all_characters(self) -> List[Character]:
|
||||
docs = await self.collection.find().to_list(None)
|
||||
docs = await self.collection.find({}, {"character_image_data": 0}).to_list(None)
|
||||
|
||||
characters = []
|
||||
for doc in docs:
|
||||
@@ -38,4 +41,3 @@ class CharacterRepo:
|
||||
|
||||
async def update_char(self, char_id: str, character: Character) -> None:
|
||||
await self.collection.update_one({"_id": ObjectId(char_id)}, {"$set": character.model_dump()})
|
||||
|
||||
|
||||
@@ -50,18 +50,19 @@ async def new_char_bio(message: Message, state: FSMContext, dao: DAO, bot: Bot):
|
||||
|
||||
try:
|
||||
# ВОТ ТУТ скачиваем файл (прямо перед сохранением)
|
||||
# file_io = await bot.download(file_id)
|
||||
file_io = await bot.download(file_id)
|
||||
# photo_bytes = file_io.getvalue() # Получаем байты
|
||||
|
||||
# Создаем модель
|
||||
char = Character(
|
||||
id=None,
|
||||
name=name,
|
||||
# character_image=photo_bytes,
|
||||
character_image_data=file_io.read(),
|
||||
character_image_tg_id=None,
|
||||
character_image_doc_tg_id=file_id,
|
||||
character_bio=bio
|
||||
)
|
||||
file_io.close()
|
||||
|
||||
# Сохраняем через DAO
|
||||
await dao.chars.add_character(char)
|
||||
|
||||
Reference in New Issue
Block a user