just for sync

develop
Сергей Ванюшкин 2024-01-29 09:34:11 +00:00
parent 08f3297297
commit 479a997844
6 changed files with 35 additions and 31 deletions

View File

@ -35,7 +35,7 @@ class MenuCrud:
select( select(
m, m,
func.count(distinct(s.id)).label("submenus_count"), func.count(distinct(s.id)).label("submenus_count"),
func.count(distinct(d.id)).label("dishes_count") func.count(distinct(d.id)).label("dishes_count"),
) )
.join(s, s.parent_menu == m.id, isouter=True) .join(s, s.parent_menu == m.id, isouter=True)
.join(d, d.parent_submenu == s.id, isouter=True) .join(d, d.parent_submenu == s.id, isouter=True)
@ -46,7 +46,7 @@ class MenuCrud:
menu = menu.scalars().one_or_none() menu = menu.scalars().one_or_none()
if menu is None: if menu is None:
return None return None
return menu return menu
@staticmethod @staticmethod
async def update_menu_item( async def update_menu_item(

View File

@ -1,7 +1,8 @@
from uuid import UUID from uuid import UUID
from sqlalchemy import delete, func, select, update from sqlalchemy import delete, distinct, func, select, update
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import aliased
from fastfood import models, schemas from fastfood import models, schemas
@ -12,7 +13,7 @@ class SubMenuCrud:
async with session: async with session:
query = select(models.SubMenu).where(models.SubMenu.parent_menu == menu_id) query = select(models.SubMenu).where(models.SubMenu.parent_menu == menu_id)
submenus = await session.execute(query) submenus = await session.execute(query)
return submenus.scalars().all() return submenus
@staticmethod @staticmethod
async def create_submenu_item( async def create_submenu_item(
@ -35,21 +36,19 @@ class SubMenuCrud:
session: AsyncSession, session: AsyncSession,
): ):
async with session: async with session:
query = select(models.SubMenu).where(models.SubMenu.id == submenu_id) s = aliased(models.SubMenu)
d = aliased(models.Dish)
query = (
select(s, func.count(distinct(d.id)))
.join(d, s.id == d.parent_submenu, isouter=True)
.group_by(s.id)
.where(s.id == submenu_id)
)
submenu = await session.execute(query) submenu = await session.execute(query)
submenu = submenu.scalars().one_or_none() submenu = submenu.scalars().one_or_none()
if submenu is None: if submenu is None:
return None return None
return submenu
dish_query = (
select(func.count(models.Dish.id))
.join(models.SubMenu)
.filter(models.Dish.parent_submenu == models.SubMenu.id)
)
dishes = await session.execute(dish_query)
submenu.dishes_count = dishes.scalars().one_or_none()
return submenu
@staticmethod @staticmethod
async def update_submenu_item( async def update_submenu_item(
@ -67,7 +66,7 @@ class SubMenuCrud:
await session.commit() await session.commit()
qr = select(models.SubMenu).where(models.SubMenu.id == submenu_id) qr = select(models.SubMenu).where(models.SubMenu.id == submenu_id)
updated_submenu = await session.execute(qr) updated_submenu = await session.execute(qr)
return updated_submenu.scalars().one() return updated_submenu
@staticmethod @staticmethod
async def delete_submenu_item(submenu_id: UUID, session: AsyncSession): async def delete_submenu_item(submenu_id: UUID, session: AsyncSession):

View File

@ -26,9 +26,9 @@ class Base(DeclarativeBase):
def __eq__(self, other): def __eq__(self, other):
classes_match = isinstance(other, self.__class__) classes_match = isinstance(other, self.__class__)
a, b = deepcopy(self.__dict__), deepcopy(other.__dict__) a, b = deepcopy(self.__dict__), deepcopy(other.__dict__)
a.pop('_sa_instance_state', None) a.pop("_sa_instance_state", None)
b.pop('_sa_instance_state', None) b.pop("_sa_instance_state", None)
attrs_match = (a == b) attrs_match = a == b
return classes_match and attrs_match return classes_match and attrs_match
def __ne__(self, other): def __ne__(self, other):

View File

@ -38,6 +38,7 @@ async def get_menu(
session: AsyncSession = Depends(get_async_session), session: AsyncSession = Depends(get_async_session),
): ):
result = await crud.get_menu_item(menu_id=menu_id, session=session) result = await crud.get_menu_item(menu_id=menu_id, session=session)
if not result: if not result:
raise HTTPException(status_code=404, detail="menu not found") raise HTTPException(status_code=404, detail="menu not found")
return result return result

View File

@ -18,7 +18,7 @@ async def get_submenus(
menu_id: UUID, session: AsyncSession = Depends(get_async_session) menu_id: UUID, session: AsyncSession = Depends(get_async_session)
): ):
result = await crud.get_submenus(menu_id=menu_id, session=session) result = await crud.get_submenus(menu_id=menu_id, session=session)
return result return result.scalars().all()
@router.post("/", status_code=201) @router.post("/", status_code=201)
@ -66,7 +66,7 @@ async def update_submenu(
submenu=submenu, submenu=submenu,
session=session, session=session,
) )
return result return result.scalars().one()
@router.delete("/{submenu_id}") @router.delete("/{submenu_id}")

View File

@ -1,13 +1,13 @@
from uuid import UUID from uuid import UUID
from sqlalchemy.ext.asyncio import AsyncSession
from fastfood.cruds.submenu import SubMenuCrud
from fastfood.models import Menu, SubMenu, Dish
from fastfood.cruds.menu import MenuCrud
from fastfood.schemas import Menu as menuschema
from fastfood.schemas import SubMenuRead as submenuschema
from fastfood.schemas import MenuBase as menubaseschema
import pytest import pytest
from sqlalchemy.ext.asyncio import AsyncSession
from fastfood.cruds.menu import MenuCrud
from fastfood.cruds.submenu import SubMenuCrud
from fastfood.models import Dish, Menu, SubMenu
from fastfood.schemas import Menu as menuschema
from fastfood.schemas import MenuBase as menubaseschema
@pytest.mark.asyncio @pytest.mark.asyncio
@ -24,7 +24,7 @@ async def test_menu(asession: AsyncSession) -> None:
assert menu == req_menu assert menu == req_menu
req_menus = await MenuCrud.get_menus(asession) req_menus = await MenuCrud.get_menus(asession)
# assert menu == req_menus.first() assert menu == req_menus.scalars().one()
menu.title = "updatedMenu" menu.title = "updatedMenu"
await MenuCrud.update_menu_item( await MenuCrud.update_menu_item(
@ -48,8 +48,12 @@ async def test_submenu(asession: AsyncSession) -> None:
await asession.refresh(menu) await asession.refresh(menu)
menu_id: UUID = menu.id menu_id: UUID = menu.id
submenu: SubMenu = SubMenu( submenu: SubMenu = SubMenu(
title="submenu", description="", parent_menu=menu_id, title="submenu",
description="",
parent_menu=menu_id,
) )
submenu = await SubMenuCrud.create_submenu_item( submenu = await SubMenuCrud.create_submenu_item(
menu_id, menubaseschema.model_validate(submenu), asession, menu_id,
menubaseschema.model_validate(submenu),
asession,
) )