Added endpoints for getting list of materials, etc

This commit is contained in:
Donkie
2023-08-26 11:08:57 +02:00
parent 9d51ee49ed
commit 557ea82171
4 changed files with 171 additions and 2 deletions

View File

@@ -1,5 +1,6 @@
"""Helper functions for interacting with filament database objects."""
import logging
from typing import Optional
from sqlalchemy import select
@@ -143,3 +144,26 @@ async def delete(db: AsyncSession, filament_id: int) -> None:
except IntegrityError as exc:
await db.rollback()
raise ItemDeleteError("Failed to delete filament.") from exc
logger = logging.getLogger(__name__)
async def find_materials(
*,
db: AsyncSession,
) -> list[str]:
"""Find a list of filament materials by searching for distinct values in the filament table."""
stmt = select(models.Filament.material).distinct()
rows = await db.execute(stmt)
return [row[0] for row in rows.all() if row[0] is not None]
async def find_article_numbers(
*,
db: AsyncSession,
) -> list[str]:
"""Find a list of filament article numbers by searching for distinct values in the filament table."""
stmt = select(models.Filament.article_number).distinct()
rows = await db.execute(stmt)
return [row[0] for row in rows.all() if row[0] is not None]

View File

@@ -257,3 +257,23 @@ async def use_length(db: AsyncSession, spool_id: int, length: float) -> models.S
await db.commit()
return spool
async def find_locations(
*,
db: AsyncSession,
) -> list[str]:
"""Find a list of spool locations by searching for distinct values in the spool table."""
stmt = sqlalchemy.select(models.Spool.location).distinct()
rows = await db.execute(stmt)
return [row[0] for row in rows.all() if row[0] is not None]
async def find_lot_numbers(
*,
db: AsyncSession,
) -> list[str]:
"""Find a list of spool lot numbers by searching for distinct values in the spool table."""
stmt = sqlalchemy.select(models.Spool.lot_nr).distinct()
rows = await db.execute(stmt)
return [row[0] for row in rows.all() if row[0] is not None]