+ api
This commit is contained in:
@@ -15,8 +15,8 @@ class AssetsRepo:
|
||||
asset.id = res.inserted_id
|
||||
return asset
|
||||
|
||||
async def get_assets(self, limit: int = 10, offset: int = 0) -> List[Asset]:
|
||||
res = await self.collection.find({},{"data":0}).sort("created_at", -1).skip(offset).limit(limit).to_list(None)
|
||||
async def get_assets(self, limit: int = 10, offset: int = 0) -> List[Asset]:
|
||||
res = await self.collection.find({}, {"data": 0}).sort("created_at", -1).skip(offset).limit(limit).to_list(None)
|
||||
assets = []
|
||||
for doc in res:
|
||||
# Конвертируем ObjectId в строку и кладем в поле id
|
||||
@@ -28,12 +28,26 @@ class AssetsRepo:
|
||||
return assets
|
||||
|
||||
async def get_asset(self, asset_id: str, with_data: bool = True) -> Asset:
|
||||
res = await self.collection.find_one({"_id": ObjectId(asset_id)}, {"data": 0 if not with_data else 1})
|
||||
res = await self.collection.find_one({"_id": ObjectId(asset_id)},
|
||||
{"_id": 1, "name": 1, "type": 1, "tg_doc_file_id": 1,
|
||||
"data": 0 if not with_data else 1})
|
||||
res["id"] = str(res.pop("_id"))
|
||||
return Asset(**res)
|
||||
|
||||
|
||||
async def update_asset(self, asset_id: str, asset: Asset):
|
||||
if not asset.id:
|
||||
raise Exception(f"Asset ID not found: {asset_id}")
|
||||
await self.collection.update_one({"_id": ObjectId(asset_id)}, {"$set": asset.model_dump()})
|
||||
|
||||
async def set_tg_photo_file_id(self, asset_id: str, tg_photo_file_id: str):
|
||||
await self.collection.update_one({"_id": ObjectId(asset_id)}, {"$set": {"tg_photo_file_id": tg_photo_file_id}})
|
||||
|
||||
async def get_assets_by_char_id(self, character_id: str, limit: int = 10, offset: int = 0) -> List[Asset]:
|
||||
docs = await self.collection.find({"linked_char_id": character_id},
|
||||
{"data": 0}, sort=[("created_at", -1)]).limit(limit).skip(offset).to_list(
|
||||
None)
|
||||
assets = []
|
||||
for doc in docs:
|
||||
doc["id"] = str(doc.pop("_id"))
|
||||
assets.append(Asset(**doc))
|
||||
return assets
|
||||
|
||||
Reference in New Issue
Block a user