86 lines
3.7 KiB
Python
86 lines
3.7 KiB
Python
from typing import List, Optional
|
|
from models.Album import Album
|
|
from models.Generation import Generation
|
|
from repos.dao import DAO
|
|
|
|
class AlbumService:
|
|
def __init__(self, dao: DAO):
|
|
self.dao = dao
|
|
|
|
async def create_album(self, name: str, description: Optional[str] = None) -> Album:
|
|
album = Album(name=name, description=description)
|
|
album_id = await self.dao.albums.create_album(album)
|
|
album.id = album_id
|
|
return album
|
|
|
|
async def get_albums(self, limit: int = 10, offset: int = 0) -> List[Album]:
|
|
return await self.dao.albums.get_albums(limit=limit, offset=offset)
|
|
|
|
async def get_album(self, album_id: str) -> Optional[Album]:
|
|
return await self.dao.albums.get_album(album_id)
|
|
|
|
async def update_album(self, album_id: str, name: Optional[str] = None, description: Optional[str] = None) -> Optional[Album]:
|
|
album = await self.dao.albums.get_album(album_id)
|
|
if not album:
|
|
return None
|
|
|
|
if name:
|
|
album.name = name
|
|
if description is not None:
|
|
album.description = description
|
|
|
|
await self.dao.albums.update_album(album_id, album)
|
|
return album
|
|
|
|
async def delete_album(self, album_id: str) -> bool:
|
|
return await self.dao.albums.delete_album(album_id)
|
|
|
|
async def add_generation_to_album(self, album_id: str, generation_id: str) -> bool:
|
|
# Verify album exists
|
|
album = await self.dao.albums.get_album(album_id)
|
|
if not album:
|
|
return False
|
|
|
|
# Verify generation exists (optional but good practice)
|
|
gen = await self.dao.generations.get_generation(generation_id)
|
|
if not gen:
|
|
return False
|
|
if album.cover_asset_id is None and gen.status == 'done':
|
|
album.cover_asset_id = gen.result_list[0]
|
|
return await self.dao.albums.add_generation(album_id, generation_id, album.cover_asset_id)
|
|
|
|
async def remove_generation_from_album(self, album_id: str, generation_id: str) -> bool:
|
|
return await self.dao.albums.remove_generation(album_id, generation_id)
|
|
|
|
async def get_generations_by_album(self, album_id: str, limit: int = 10, offset: int = 0) -> List[Generation]:
|
|
album = await self.dao.albums.get_album(album_id)
|
|
if not album or not album.generation_ids:
|
|
return []
|
|
|
|
# Slice the generation IDs (simple pagination on ID list)
|
|
# Note: This pagination is on IDs, then we fetch objects.
|
|
# Ideally, fetch only slice.
|
|
|
|
# Reverse to show newest first? Or just follow list order?
|
|
# Assuming list order is insertion order (which usually is what we want for manual sorting or chronological if always appended).
|
|
# Let's assume user wants same order as in list.
|
|
|
|
sliced_ids = album.generation_ids[offset : offset + limit]
|
|
if not sliced_ids:
|
|
return []
|
|
|
|
# Fetch generations by IDs
|
|
# We need a method in GenerationRepo to fetch by IDs.
|
|
# Currently we only have get_generations with filters.
|
|
# We can add get_generations_by_ids to GenerationRepo or use loop (inefficient).
|
|
# Let's add get_generations_by_ids to GenerationRepo.
|
|
|
|
# For now, I will use a loop if I can't modify Repo immediately,
|
|
# but I SHOULD modify GenerationRepo.
|
|
|
|
# Or I can use get_generations(filter={"_id": {"$in": [ObjectId(id) for id in sliced_ids]}})
|
|
# But get_generations doesn't support generic filter passing.
|
|
|
|
# I'll update GenerationRepo to support fetching by IDs.
|
|
return await self.dao.generations.get_generations_by_ids(sliced_ids)
|