Feature: Sale Category also shows the products in it to make it easier to check for errors.
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
import uuid
|
||||
|
||||
from datetime import date, timedelta
|
||||
from typing import List
|
||||
from typing import List, Optional
|
||||
|
||||
import barker.schemas.product as schemas
|
||||
|
||||
@ -299,45 +299,106 @@ def product_list(date_: date, db: Session) -> List[schemas.Product]:
|
||||
|
||||
@router.get("/query")
|
||||
async def show_term(
|
||||
mc: uuid.UUID = None,
|
||||
mc: Optional[uuid.UUID] = None,
|
||||
sc: Optional[uuid.UUID] = None,
|
||||
date_: date = Depends(effective_date),
|
||||
current_user: UserToken = Depends(get_user),
|
||||
):
|
||||
list_ = []
|
||||
query = select(ProductVersion)
|
||||
if sc is not None:
|
||||
query = query.join(ProductVersion.menu_category)
|
||||
if mc is not None:
|
||||
query = query.join(ProductVersion.sale_category).join(SaleCategory.tax)
|
||||
query = query.where(
|
||||
and_(
|
||||
or_(
|
||||
ProductVersion.valid_from == None, # noqa: E711
|
||||
ProductVersion.valid_from <= date_,
|
||||
),
|
||||
or_(
|
||||
ProductVersion.valid_till == None, # noqa: E711
|
||||
ProductVersion.valid_till >= date_,
|
||||
),
|
||||
)
|
||||
)
|
||||
if mc is not None:
|
||||
query = query.where(ProductVersion.menu_category_id == mc).order_by(
|
||||
ProductVersion.sort_order, ProductVersion.name
|
||||
)
|
||||
if sc is not None:
|
||||
query = query.where(ProductVersion.sale_category_id == sc).order_by(
|
||||
MenuCategory.sort_order, ProductVersion.sort_order, ProductVersion.name
|
||||
)
|
||||
|
||||
if mc is not None:
|
||||
query = query.options(
|
||||
joinedload(ProductVersion.sale_category, innerjoin=True),
|
||||
joinedload(ProductVersion.sale_category, SaleCategory.tax, innerjoin=True),
|
||||
contains_eager(ProductVersion.sale_category),
|
||||
contains_eager(ProductVersion.sale_category, SaleCategory.tax),
|
||||
)
|
||||
|
||||
if sc is not None:
|
||||
query = query.options(
|
||||
joinedload(ProductVersion.menu_category, innerjoin=True),
|
||||
contains_eager(ProductVersion.menu_category),
|
||||
)
|
||||
|
||||
with SessionFuture() as db:
|
||||
for item in (
|
||||
db.execute(
|
||||
select(ProductVersion)
|
||||
.join(ProductVersion.sale_category)
|
||||
.join(SaleCategory.tax)
|
||||
.where(
|
||||
and_(
|
||||
ProductVersion.menu_category_id == mc,
|
||||
or_(
|
||||
ProductVersion.valid_from == None, # noqa: E711
|
||||
ProductVersion.valid_from <= date_,
|
||||
),
|
||||
or_(
|
||||
ProductVersion.valid_till == None, # noqa: E711
|
||||
ProductVersion.valid_till >= date_,
|
||||
),
|
||||
)
|
||||
for item in db.execute(query).scalars().all():
|
||||
if sc is not None:
|
||||
list_.append(
|
||||
{
|
||||
"id": item.product_id,
|
||||
"name": item.full_name,
|
||||
"menuCategory": {
|
||||
"id": item.menu_category_id,
|
||||
"name": item.menu_category.name,
|
||||
},
|
||||
"price": item.price,
|
||||
}
|
||||
)
|
||||
.order_by(ProductVersion.sort_order, ProductVersion.name)
|
||||
.options(
|
||||
joinedload(ProductVersion.sale_category, innerjoin=True),
|
||||
joinedload(ProductVersion.sale_category, SaleCategory.tax, innerjoin=True),
|
||||
contains_eager(ProductVersion.sale_category),
|
||||
contains_eager(ProductVersion.sale_category, SaleCategory.tax),
|
||||
if mc is not None:
|
||||
list_.append(query_product_info(item, False))
|
||||
if item.has_happy_hour:
|
||||
list_.append(query_product_info(item, True))
|
||||
return list_
|
||||
|
||||
|
||||
def product_list_of_sale_category(date_: date, db: Session) -> List[schemas.Product]:
|
||||
return [
|
||||
product_info(item)
|
||||
for item in db.execute(
|
||||
select(ProductVersion)
|
||||
.join(ProductVersion.menu_category)
|
||||
.join(ProductVersion.sale_category)
|
||||
.where(
|
||||
and_(
|
||||
or_(
|
||||
ProductVersion.valid_from == None, # noqa: E711
|
||||
ProductVersion.valid_from <= date_,
|
||||
),
|
||||
or_(
|
||||
ProductVersion.valid_till == None, # noqa: E711
|
||||
ProductVersion.valid_till >= date_,
|
||||
),
|
||||
)
|
||||
)
|
||||
.scalars()
|
||||
.all()
|
||||
):
|
||||
list_.append(query_product_info(item, False))
|
||||
if item.has_happy_hour:
|
||||
list_.append(query_product_info(item, True))
|
||||
return list_
|
||||
.order_by(MenuCategory.sort_order)
|
||||
.order_by(MenuCategory.name)
|
||||
.order_by(ProductVersion.sort_order)
|
||||
.order_by(ProductVersion.name)
|
||||
.options(
|
||||
joinedload(ProductVersion.menu_category, innerjoin=True),
|
||||
joinedload(ProductVersion.sale_category, innerjoin=True),
|
||||
contains_eager(ProductVersion.menu_category),
|
||||
contains_eager(ProductVersion.sale_category),
|
||||
)
|
||||
)
|
||||
.scalars()
|
||||
.all()
|
||||
]
|
||||
|
||||
|
||||
@router.get("/{id_}", response_model=schemas.Product)
|
||||
|
||||
Reference in New Issue
Block a user