Files
gartenmanager/backend/app/crud/plant.py

55 lines
1.9 KiB
Python
Raw Normal View History

from uuid import UUID
from sqlalchemy import or_, select
from sqlalchemy.orm import selectinload
from sqlalchemy.ext.asyncio import AsyncSession
from app.crud.base import CRUDBase
from app.models.plant import Plant, PlantCompatibility, PlantFamily
from app.schemas.plant import PlantCreate, PlantUpdate
class CRUDPlant(CRUDBase[Plant, PlantCreate, PlantUpdate]):
async def get(self, db: AsyncSession, *, id: UUID) -> Plant | None:
result = await db.execute(
select(Plant)
.options(selectinload(Plant.family))
.where(Plant.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 = 200
) -> list[Plant]:
result = await db.execute(
select(Plant)
.options(selectinload(Plant.family))
.where(
Plant.is_active == True, # noqa: E712
or_(Plant.tenant_id == None, Plant.tenant_id == tenant_id), # noqa: E711
)
.order_by(Plant.name)
.offset(skip)
.limit(limit)
)
return list(result.scalars().all())
async def create_for_tenant(
self, db: AsyncSession, *, obj_in: PlantCreate, tenant_id: UUID
) -> Plant:
return await self.create(db, obj_in=obj_in, tenant_id=tenant_id)
class CRUDPlantFamily(CRUDBase[PlantFamily, PlantFamily, PlantFamily]):
async def get_all(self, db: AsyncSession) -> list[PlantFamily]:
result = await db.execute(select(PlantFamily).order_by(PlantFamily.name))
return list(result.scalars().all())
async def get_by_name(self, db: AsyncSession, *, name: str) -> PlantFamily | None:
result = await db.execute(select(PlantFamily).where(PlantFamily.name == name))
return result.scalar_one_or_none()
crud_plant = CRUDPlant(Plant)
crud_plant_family = CRUDPlantFamily(PlantFamily)