import uuid from datetime import datetime, timezone from sqlalchemy import select from sqlalchemy.ext.asyncio import AsyncSession from app.models.notiz import AkteNotiz async def erstelle_notiz( db: AsyncSession, *, entitaet_typ: str, entitaet_id: str, text: str, autor_id: int ) -> AkteNotiz: notiz = AkteNotiz( id=uuid.uuid4(), entitaet_typ=entitaet_typ, entitaet_id=entitaet_id, text=text, autor_id=autor_id, erstellt_am=datetime.now(timezone.utc), ) db.add(notiz) await db.commit() await db.refresh(notiz) return notiz async def liste_fuer_entitaet(db: AsyncSession, *, entitaet_typ: str, entitaet_id: str) -> list[AkteNotiz]: ergebnis = await db.execute( select(AkteNotiz) .where(AkteNotiz.entitaet_typ == entitaet_typ, AkteNotiz.entitaet_id == entitaet_id) .order_by(AkteNotiz.erstellt_am.desc()) ) return list(ergebnis.scalars().all()) async def loesche_notiz(db: AsyncSession, *, notiz: AkteNotiz) -> None: await db.delete(notiz) await db.commit()