from sqlalchemy import select from sqlalchemy.ext.asyncio import AsyncSession from app.models.attachment_point import AttachmentPoint from app.models.attachment_point_name import AttachmentPointName _RELATIONSHIPS = [ "attachment_point_name", "iso_thread", "unc_thread", "clamp_connection", "static_connection", ] async def get(db: AsyncSession, attachment_point_id: int) -> AttachmentPoint | None: return await db.get(AttachmentPoint, attachment_point_id) async def list_for_augmented_part( db: AsyncSession, augmented_part_id: int ) -> list[AttachmentPoint]: result = await db.execute( select(AttachmentPoint) .where(AttachmentPoint.augmented_part_id == augmented_part_id) .order_by(AttachmentPoint.id) ) return list(result.scalars().all()) async def list_by_augmented_parts_and_name( db: AsyncSession, augmented_part_ids: list[int], name: str ) -> list[AttachmentPoint]: """AttachmentPoints named ``name`` for the given augmented parts, fetched in one query.""" if not augmented_part_ids: return [] result = await db.execute( select(AttachmentPoint) .join(AttachmentPoint.attachment_point_name) .where( AttachmentPoint.augmented_part_id.in_(augmented_part_ids), AttachmentPointName.name == name, ) ) return list(result.scalars().all()) async def list_all( db: AsyncSession, skip: int = 0, limit: int = 100 ) -> list[AttachmentPoint]: result = await db.execute( select(AttachmentPoint) .order_by(AttachmentPoint.augmented_part_id, AttachmentPoint.id) .offset(skip) .limit(limit) ) return list(result.scalars().all()) async def create( db: AsyncSession, *, augmented_part_id: int, attachment_point_name_id: int, iso_thread_id: int | None = None, unc_thread_id: int | None = None, clamp_connection_id: int | None = None, static_connection_id: int | None = None, ) -> AttachmentPoint: ap = AttachmentPoint( augmented_part_id=augmented_part_id, attachment_point_name_id=attachment_point_name_id, iso_thread_id=iso_thread_id, unc_thread_id=unc_thread_id, clamp_connection_id=clamp_connection_id, static_connection_id=static_connection_id, ) db.add(ap) await db.commit() await db.refresh(ap, attribute_names=_RELATIONSHIPS) return ap async def update(db: AsyncSession, ap: AttachmentPoint, data: dict) -> AttachmentPoint: for field, value in data.items(): setattr(ap, field, value) await db.commit() await db.refresh(ap, attribute_names=_RELATIONSHIPS) return ap async def delete(db: AsyncSession, ap: AttachmentPoint) -> None: await db.delete(ap) await db.commit()