2025-08-30 10:06:47 +02:00
|
|
|
|
|
|
|
|
from typing import Annotated
|
|
|
|
|
|
|
|
|
|
from fastapi import Path, HTTPException
|
|
|
|
|
from sqlalchemy import select
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def make_primary_annotation(name=str):
|
2025-08-31 07:53:17 +02:00
|
|
|
return Annotated[str, Path(description=f'{name}, either id (int) or code')]
|
2025-08-30 10:06:47 +02:00
|
|
|
|
|
|
|
|
|
2025-08-31 07:53:17 +02:00
|
|
|
def get_single_record(session, cls, code: str):
|
|
|
|
|
result = session.get(cls, code)
|
2025-08-30 10:06:47 +02:00
|
|
|
if result is None:
|
2025-08-31 07:53:17 +02:00
|
|
|
result = session.scalar(select(cls).where(cls.code == code))
|
2025-08-30 10:06:47 +02:00
|
|
|
if result is None:
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=404,
|
2025-08-31 07:53:17 +02:00
|
|
|
detail=f"{cls.__class__.__name__} {code!r} not found.")
|
2025-08-30 10:06:47 +02:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def set_item_status(session, current_user, item, status: str):
|
|
|
|
|
item.status_id = status
|
|
|
|
|
item._user__id = current_user.id
|
2025-08-31 07:53:17 +02:00
|
|
|
try:
|
|
|
|
|
session.commit()
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
raise HTTPException(status_code=422,
|
|
|
|
|
detail=[dict(msg=', '.join(exc.args),
|
|
|
|
|
type="Database Error")])
|
2025-08-30 10:06:47 +02:00
|
|
|
session.refresh(item)
|
|
|
|
|
return item
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def update_item(session, current_user, item, data):
|
|
|
|
|
for k, v in data.model_dump(exclude_unset=True).items():
|
2025-08-31 07:53:17 +02:00
|
|
|
if (k == 'code' and
|
|
|
|
|
session.scalar(select(item.__class__).where(item.__class__.code == v)) != item):
|
|
|
|
|
raise HTTPException(status_code=422,
|
|
|
|
|
detail=[dict(msg=f"{item.__class__.__name__} {k!r} already exists",
|
|
|
|
|
type="Integrity Error")])
|
2025-08-30 10:06:47 +02:00
|
|
|
setattr(item, k, v)
|
|
|
|
|
item._user__id = current_user.id
|
2025-08-31 07:53:17 +02:00
|
|
|
try:
|
|
|
|
|
session.commit()
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
raise HTTPException(status_code=422,
|
|
|
|
|
detail=[dict(msg=', '.join(exc.args),
|
|
|
|
|
type="Database Error")])
|
2025-08-30 10:06:47 +02:00
|
|
|
session.refresh(item)
|
|
|
|
|
return item
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_item(session, cls, current_user, data):
|
|
|
|
|
item = cls(**data.model_dump())
|
2025-08-31 07:53:17 +02:00
|
|
|
if session.scalar(select(cls).where(cls.code == item.code)):
|
|
|
|
|
raise HTTPException(status_code=422,
|
|
|
|
|
detail=[dict(msg=f"{cls.__class__.__name__} {item.code} already exists",
|
|
|
|
|
type="Integrity Error")])
|
2025-08-30 10:06:47 +02:00
|
|
|
item._user__id = current_user.id
|
|
|
|
|
session.add(item)
|
2025-08-31 07:53:17 +02:00
|
|
|
try:
|
|
|
|
|
session.commit()
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
raise HTTPException(status_code=422,
|
|
|
|
|
detail=[dict(msg=', '.join(exc.args),
|
|
|
|
|
type="Database Error")])
|
2025-08-30 10:06:47 +02:00
|
|
|
session.refresh(item)
|
|
|
|
|
return item
|