128 lines
4.1 KiB
Python
128 lines
4.1 KiB
Python
import uuid
|
|
|
|
import brewman.schemas.product_group as schemas
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Security, status
|
|
from sqlalchemy import select
|
|
from sqlalchemy.exc import SQLAlchemyError
|
|
|
|
from ..core.security import get_current_active_user as get_user
|
|
from ..db.session import SessionFuture
|
|
from ..models.product_group import ProductGroup
|
|
from ..schemas.user import UserToken
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post("", response_model=schemas.ProductGroup)
|
|
def save(
|
|
data: schemas.ProductGroupIn,
|
|
user: UserToken = Security(get_user, scopes=["product-groups"]),
|
|
) -> schemas.ProductGroup:
|
|
try:
|
|
with SessionFuture() as db:
|
|
item = ProductGroup(name=data.name, nutritional=data.nutritional, ice_cream=data.ice_cream)
|
|
db.add(item)
|
|
db.commit()
|
|
return product_group_info(item)
|
|
except SQLAlchemyError as e:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=str(e),
|
|
)
|
|
|
|
|
|
@router.put("/{id_}", response_model=schemas.ProductGroup)
|
|
def update_route(
|
|
id_: uuid.UUID,
|
|
data: schemas.ProductGroupIn,
|
|
user: UserToken = Security(get_user, scopes=["product-groups"]),
|
|
) -> schemas.ProductGroup:
|
|
try:
|
|
with SessionFuture() as db:
|
|
item = db.execute(select(ProductGroup).where(ProductGroup.id == id_)).scalar_one()
|
|
if item.is_fixture:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_423_LOCKED,
|
|
detail=f"{item.name} is a fixture and cannot be edited or deleted.",
|
|
)
|
|
item.name = data.name
|
|
item.nutritional = data.nutritional
|
|
item.ice_cream = data.ice_cream
|
|
db.commit()
|
|
return product_group_info(item)
|
|
except SQLAlchemyError as e:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=str(e),
|
|
)
|
|
|
|
|
|
@router.delete("/{id_}", response_model=schemas.ProductGroupBlank)
|
|
def delete_route(
|
|
id_: uuid.UUID,
|
|
user: UserToken = Security(get_user, scopes=["product-groups"]),
|
|
) -> schemas.ProductGroupBlank:
|
|
with SessionFuture() as db:
|
|
item = db.execute(select(ProductGroup).where(ProductGroup.id == id_)).scalar_one()
|
|
|
|
if item is None:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Product Group not found",
|
|
)
|
|
elif item.is_fixture:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_423_LOCKED,
|
|
detail=f"{item.name} is a fixture and cannot be edited or deleted.",
|
|
)
|
|
else:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_501_NOT_IMPLEMENTED,
|
|
detail="Product Group deletion not implemented",
|
|
)
|
|
|
|
|
|
@router.get("", response_model=schemas.ProductGroupBlank)
|
|
def show_blank(
|
|
user: UserToken = Security(get_user, scopes=["product-groups"]),
|
|
) -> schemas.ProductGroupBlank:
|
|
return product_group_blank()
|
|
|
|
|
|
@router.get("/list", response_model=list[schemas.ProductGroup])
|
|
async def show_list(user: UserToken = Depends(get_user)) -> list[schemas.ProductGroup]:
|
|
with SessionFuture() as db:
|
|
return [
|
|
product_group_info(item)
|
|
for item in db.execute(select(ProductGroup).order_by(ProductGroup.name)).scalars().all()
|
|
]
|
|
|
|
|
|
@router.get("/{id_}", response_model=schemas.ProductGroup)
|
|
def show_id(
|
|
id_: uuid.UUID,
|
|
user: UserToken = Security(get_user, scopes=["product-groups"]),
|
|
) -> schemas.ProductGroup:
|
|
with SessionFuture() as db:
|
|
item = db.execute(select(ProductGroup).where(ProductGroup.id == id_)).scalar_one()
|
|
return product_group_info(item)
|
|
|
|
|
|
def product_group_info(item: ProductGroup) -> schemas.ProductGroup:
|
|
return schemas.ProductGroup(
|
|
id_=item.id,
|
|
name=item.name,
|
|
nutritional=item.nutritional,
|
|
ice_cream=item.ice_cream,
|
|
is_fixture=item.is_fixture,
|
|
)
|
|
|
|
|
|
def product_group_blank() -> schemas.ProductGroupBlank:
|
|
return schemas.ProductGroupBlank(
|
|
name="",
|
|
is_fixture=False,
|
|
)
|