from uuid import UUID from sqlalchemy import select from sqlalchemy.orm import selectinload from sqlalchemy.ext.asyncio import AsyncSession from app.crud.base import CRUDBase from app.models.bed import Bed from app.schemas.bed import BedCreate, BedUpdate class CRUDBed(CRUDBase[Bed, BedCreate, BedUpdate]): async def get(self, db: AsyncSession, *, id: UUID) -> Bed | None: result = await db.execute( select(Bed) .options( selectinload(Bed.plantings).selectinload( __import__("app.models.planting", fromlist=["BedPlanting"]).BedPlanting.plant ).selectinload( __import__("app.models.plant", fromlist=["Plant"]).Plant.family ) ) .where(Bed.id == id) ) return result.scalar_one_or_none() async def get_multi_for_tenant( self, db: AsyncSession, *, tenant_id: UUID, skip: int = 0, limit: int = 100 ) -> list[Bed]: result = await db.execute( select(Bed) .where(Bed.tenant_id == tenant_id, Bed.is_active == True) # noqa: E712 .order_by(Bed.name) .offset(skip) .limit(limit) ) return list(result.scalars().all()) async def get_with_plantings(self, db: AsyncSession, *, id: UUID) -> Bed | None: from app.models.planting import BedPlanting from app.models.plant import Plant result = await db.execute( select(Bed) .options( selectinload(Bed.plantings) .selectinload(BedPlanting.plant) .selectinload(Plant.family) ) .where(Bed.id == id) ) return result.scalar_one_or_none() async def create_for_tenant( self, db: AsyncSession, *, obj_in: BedCreate, tenant_id: UUID ) -> Bed: return await self.create(db, obj_in=obj_in, tenant_id=tenant_id) crud_bed = CRUDBed(Bed)