Adding sub-parts

This commit is contained in:
Ian Roddis committed 2026-06-25 15:14:37 -03:00
1 parent 45bc5bf8f6
commit c8696e67b5
14 files changed
+436 -2

No files matched your search

+53
View File
@@ -0,0 +1,53 @@
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.sub_part_type import SubPartType
async def get(db: AsyncSession, sub_part_type_id: int) -> SubPartType | None:
return await db.get(SubPartType, sub_part_type_id)
async def get_by_name_and_part_type(
db: AsyncSession, name: str, part_type_id: int
) -> SubPartType | None:
result = await db.execute(
select(SubPartType).where(
SubPartType.name == name, SubPartType.part_type_id == part_type_id
)
)
return result.scalar_one_or_none()
async def list_all(
db: AsyncSession, skip: int = 0, limit: int = 100
) -> list[SubPartType]:
result = await db.execute(
select(SubPartType).order_by(SubPartType.name).offset(skip).limit(limit)
)
return list(result.scalars().all())
async def create(
db: AsyncSession, *, name: str, part_type_id: int
) -> SubPartType:
spt = SubPartType(name=name, part_type_id=part_type_id)
db.add(spt)
await db.commit()
await db.refresh(spt, attribute_names=["part_type"])
return spt
async def update(
db: AsyncSession, sub_part_type: SubPartType, data: dict
) -> SubPartType:
for field, value in data.items():
setattr(sub_part_type, field, value)
await db.commit()
await db.refresh(sub_part_type, attribute_names=["part_type"])
return sub_part_type
async def delete(db: AsyncSession, sub_part_type: SubPartType) -> None:
await db.delete(sub_part_type)
await db.commit()