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