146 lines
3.9 KiB
Python
146 lines
3.9 KiB
Python
import uuid
|
|
|
|
from typing import List, Optional
|
|
|
|
import brewman.schemas.master as schemas
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Security, status
|
|
from sqlalchemy.exc import SQLAlchemyError
|
|
from sqlalchemy.orm import Session
|
|
|
|
from ..core.security import get_current_active_user as get_user
|
|
from ..db.session import SessionLocal
|
|
from ..models.master import ProductGroup
|
|
from ..schemas.auth import UserToken
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
# Dependency
|
|
def get_db():
|
|
try:
|
|
db = SessionLocal()
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
@router.post("", response_model=schemas.ProductGroup)
|
|
def save(
|
|
data: schemas.ProductGroupIn,
|
|
db: Session = Depends(get_db),
|
|
user: UserToken = Security(get_user, scopes=["product-groups"]),
|
|
):
|
|
try:
|
|
item = ProductGroup(name=data.name)
|
|
db.add(item)
|
|
db.commit()
|
|
return product_group_info(item, db)
|
|
except SQLAlchemyError as e:
|
|
db.rollback()
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=str(e),
|
|
)
|
|
except Exception:
|
|
db.rollback()
|
|
raise
|
|
|
|
|
|
@router.put("/{id_}", response_model=schemas.ProductGroup)
|
|
def update(
|
|
id_: uuid.UUID,
|
|
data: schemas.ProductGroupIn,
|
|
db: Session = Depends(get_db),
|
|
user: UserToken = Security(get_user, scopes=["product-groups"]),
|
|
):
|
|
try:
|
|
item = db.query(ProductGroup).filter(ProductGroup.id == id_).first()
|
|
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
|
|
db.commit()
|
|
return product_group_info(item, db)
|
|
except SQLAlchemyError as e:
|
|
db.rollback()
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=str(e),
|
|
)
|
|
except Exception:
|
|
db.rollback()
|
|
raise
|
|
|
|
|
|
@router.delete("/{id_}")
|
|
def delete(
|
|
id_: uuid.UUID,
|
|
db: Session = Depends(get_db),
|
|
user: UserToken = Security(get_user, scopes=["product-groups"]),
|
|
):
|
|
try:
|
|
item = db.query(ProductGroup).filter(ProductGroup.id == id_).first()
|
|
|
|
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",
|
|
)
|
|
except Exception:
|
|
db.rollback()
|
|
raise
|
|
|
|
|
|
@router.get("")
|
|
def show_blank(
|
|
db: Session = Depends(get_db),
|
|
user: UserToken = Security(get_user, scopes=["product-groups"]),
|
|
):
|
|
return product_group_info(None, db)
|
|
|
|
|
|
@router.get("/list", response_model=List[schemas.ProductGroup])
|
|
async def show_list(db: Session = Depends(get_db), user: UserToken = Depends(get_user)):
|
|
return [
|
|
{"id": item.id, "name": item.name, "isFixture": item.is_fixture}
|
|
for item in db.query(ProductGroup).order_by(ProductGroup.name).all()
|
|
]
|
|
|
|
|
|
@router.get("/{id_}", response_model=schemas.ProductGroup)
|
|
def show_id(
|
|
id_: uuid.UUID,
|
|
db: Session = Depends(get_db),
|
|
user: UserToken = Security(get_user, scopes=["product-groups"]),
|
|
):
|
|
item = db.query(ProductGroup).filter(ProductGroup.id == id_).first()
|
|
return product_group_info(item, db)
|
|
|
|
|
|
def product_group_info(item: Optional[ProductGroup], db):
|
|
if item is None:
|
|
return {
|
|
"name": "",
|
|
"isFixture": False,
|
|
}
|
|
else:
|
|
return {
|
|
"id": item.id,
|
|
"name": item.name,
|
|
"isFixture": item.is_fixture,
|
|
}
|