189 lines
5.7 KiB
Python
189 lines
5.7 KiB
Python
import uuid
|
|
from typing import Optional
|
|
|
|
from fastapi import APIRouter, HTTPException, status, Depends, Security
|
|
from sqlalchemy.exc import SQLAlchemyError
|
|
from sqlalchemy.orm import Session
|
|
|
|
from ..schemas.auth import UserToken
|
|
import barker.schemas.master as schemas
|
|
from ..core.security import get_current_active_user as get_user
|
|
from ..db.session import SessionLocal
|
|
from ..models.master import Product, MenuCategory
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
# Dependency
|
|
def get_db():
|
|
try:
|
|
db = SessionLocal()
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
@router.post("", response_model=schemas.MenuCategory)
|
|
def save(
|
|
data: schemas.MenuCategoryIn, db: Session = Depends(get_db), user: UserToken = Security(get_user, scopes=["products"]),
|
|
):
|
|
try:
|
|
item = MenuCategory(name=data.name, discount_limit=data.discount_limit, is_active=data.is_active, sort_order=0)
|
|
db.add(item)
|
|
db.commit()
|
|
return menu_category_info(item)
|
|
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.MenuCategory)
|
|
def update(
|
|
id_: uuid.UUID,
|
|
data: schemas.MenuCategoryIn,
|
|
db: Session = Depends(get_db),
|
|
user: UserToken = Security(get_user, scopes=["products"]),
|
|
):
|
|
try:
|
|
item: MenuCategory = db.query(MenuCategory).filter(MenuCategory.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
|
|
item.discount_limit = data.discount_limit
|
|
item.is_active = data.discount_limit
|
|
db.commit()
|
|
return menu_category_info(item)
|
|
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=["products"]),
|
|
):
|
|
try:
|
|
item: MenuCategory = db.query(MenuCategory).filter(MenuCategory.id == id_).first()
|
|
if item is None:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND, detail="Menu Category 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="Menu Category deletion not implemented",
|
|
)
|
|
except Exception:
|
|
db.rollback()
|
|
raise
|
|
|
|
|
|
@router.get("")
|
|
def show_blank(
|
|
db: Session = Depends(get_db), user: UserToken = Security(get_user, scopes=["products"]),
|
|
):
|
|
return menu_category_info(None)
|
|
|
|
|
|
@router.get("/list")
|
|
def show_list(db: Session = Depends(get_db), user: UserToken = Depends(get_user)):
|
|
return [menu_category_info(item) for item in db.query(MenuCategory).order_by(MenuCategory.sort_order).order_by(MenuCategory.name).all()]
|
|
|
|
|
|
@router.get("/sale-list")
|
|
async def sale_list(
|
|
db: Session = Depends(get_db),
|
|
current_user: UserToken = Depends(get_user),
|
|
):
|
|
list_ = (
|
|
db.query(MenuCategory)
|
|
.filter(MenuCategory.is_active == True)
|
|
.order_by(MenuCategory.sort_order)
|
|
.order_by(MenuCategory.name)
|
|
.all()
|
|
)
|
|
menu_categories = []
|
|
for item in list_:
|
|
products = (
|
|
db.query(Product)
|
|
.filter(Product.menu_category_id == item.id)
|
|
.filter(Product.is_active == True)
|
|
.order_by(Product.sort_order)
|
|
.order_by(Product.name)
|
|
.all()
|
|
)
|
|
if len(products) == 0:
|
|
continue
|
|
pg = menu_category_info(item)
|
|
pg["products"] = [
|
|
{
|
|
"id": p.id,
|
|
"name": p.name,
|
|
"units": p.units,
|
|
"tat": {"id": p.vat.id, "name": p.vat.name, "rate": p.vat.rate},
|
|
"price": p.price,
|
|
"hasHappyHour": p.has_happy_hour,
|
|
"isActive": p.is_active,
|
|
"isNotAvailable": p.is_not_available,
|
|
"sortOrder": p.sort_order,
|
|
"quantity": p.quantity,
|
|
}
|
|
for p in products
|
|
]
|
|
menu_categories.append(pg)
|
|
return menu_categories
|
|
|
|
|
|
@router.get("/{id_}")
|
|
def show_id(
|
|
id_: uuid.UUID, db: Session = Depends(get_db), user: UserToken = Security(get_user, scopes=["products"]),
|
|
):
|
|
item: MenuCategory = db.query(MenuCategory).filter(MenuCategory.id == id_).first()
|
|
return menu_category_info(item)
|
|
|
|
|
|
# @view_config(
|
|
# request_method="POST", route_name="v1_menu_categories_list", renderer="json", permission="Products", trans=True,
|
|
# )
|
|
# def sort_order(request):
|
|
# json = request.json_body
|
|
# for index, item in enumerate(json):
|
|
# request.dbsession.query(MenuCategory).filter(MenuCategory.id == uuid.UUID(item["id"])).update(
|
|
# {MenuCategory.sort_order: index}
|
|
# )
|
|
# return True
|
|
|
|
|
|
def menu_category_info(item: Optional[MenuCategory]):
|
|
if item is None:
|
|
return {
|
|
"name": "",
|
|
"discountLimit": 0,
|
|
"isActive": True,
|
|
"isFixture": False,
|
|
"sortOrder": 0,
|
|
}
|
|
return {
|
|
"id": item.id,
|
|
"name": item.name,
|
|
"discountLimit": item.discount_limit * 100,
|
|
"isActive": item.is_active,
|
|
"isFixture": item.is_fixture,
|
|
"sortOrder": item.sort_order,
|
|
}
|