Fix: The route to update sort order of Menu Categories was not written.

This commit is contained in:
2021-04-27 07:22:04 +05:30
parent 59bab991fd
commit f78ad68235
3 changed files with 22 additions and 3 deletions

View File

@ -7,7 +7,7 @@ from typing import List, Optional
import barker.schemas.menu_category as schemas
from fastapi import APIRouter, Depends, HTTPException, Security, status
from sqlalchemy import distinct, select
from sqlalchemy import distinct, select, update
from sqlalchemy.exc import SQLAlchemyError
from ..core.security import get_current_active_user as get_user
@ -21,6 +21,25 @@ from . import effective_date
router = APIRouter()
@router.post("/list", response_model=List[schemas.MenuCategory])
def sort_order(
data: List[schemas.MenuCategory],
user: UserToken = Security(get_user, scopes=["sections"]),
) -> List[schemas.MenuCategory]:
try:
with SessionFuture() as db:
for index, item in enumerate(data):
db.execute(update(MenuCategory).where(MenuCategory.id == item.id_).values(sort_order=index))
db.commit()
query = select(MenuCategory).order_by(MenuCategory.sort_order).order_by(MenuCategory.name)
return [menu_category_info(item) for item in db.execute(query).scalars().all()]
except SQLAlchemyError as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=str(e),
)
@router.post("", response_model=schemas.MenuCategory)
def save(
data: schemas.MenuCategoryIn,