service/repo.submenu typehint
parent
181c6f10af
commit
35659529b4
|
@ -5,40 +5,45 @@ from sqlalchemy import delete, distinct, func, select, update
|
|||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.orm import aliased
|
||||
|
||||
from fastfood import models, schemas
|
||||
from fastfood.dbase import get_async_session
|
||||
from fastfood.models import Dish, SubMenu
|
||||
from fastfood.schemas import MenuBase
|
||||
|
||||
|
||||
class SubMenuRepository:
|
||||
def __init__(self, session: AsyncSession = Depends(get_async_session)):
|
||||
self.db = session
|
||||
|
||||
async def get_submenus(self, menu_id: UUID):
|
||||
query = select(models.SubMenu).where(
|
||||
models.SubMenu.parent_menu == menu_id,
|
||||
async def get_submenus(self, menu_id: UUID) -> list[SubMenu]:
|
||||
query = select(SubMenu).where(
|
||||
SubMenu.parent_menu == menu_id,
|
||||
)
|
||||
submenus = await self.db.execute(query)
|
||||
return submenus
|
||||
return [x for x in submenus.scalars().all()]
|
||||
|
||||
async def create_submenu_item(
|
||||
self,
|
||||
menu_id: UUID,
|
||||
submenu: schemas.MenuBase,
|
||||
):
|
||||
new_submenu = models.SubMenu(**submenu.model_dump())
|
||||
submenu: MenuBase,
|
||||
) -> SubMenu:
|
||||
new_submenu = SubMenu(**submenu.model_dump())
|
||||
new_submenu.parent_menu = menu_id
|
||||
self.db.add(new_submenu)
|
||||
await self.db.commit()
|
||||
await self.db.refresh(new_submenu)
|
||||
return new_submenu
|
||||
|
||||
full_sub = await self.get_submenu_item(menu_id, new_submenu.id)
|
||||
if full_sub is None:
|
||||
raise TypeError
|
||||
return full_sub
|
||||
|
||||
async def get_submenu_item(
|
||||
self,
|
||||
menu_id: UUID,
|
||||
submenu_id: UUID,
|
||||
):
|
||||
s = aliased(models.SubMenu)
|
||||
d = aliased(models.Dish)
|
||||
) -> SubMenu | None:
|
||||
s = aliased(SubMenu)
|
||||
d = aliased(Dish)
|
||||
query = (
|
||||
select(s, func.count(distinct(d.id)).label('dishes_count'))
|
||||
.join(d, s.id == d.parent_submenu, isouter=True)
|
||||
|
@ -55,22 +60,23 @@ class SubMenuRepository:
|
|||
self,
|
||||
menu_id: UUID,
|
||||
submenu_id: UUID,
|
||||
submenu_data: schemas.MenuBase,
|
||||
):
|
||||
submenu_data: MenuBase,
|
||||
) -> SubMenu:
|
||||
query = (
|
||||
update(models.SubMenu)
|
||||
.where(models.SubMenu.id == submenu_id)
|
||||
update(SubMenu)
|
||||
.where(SubMenu.id == submenu_id)
|
||||
.values(**submenu_data.model_dump())
|
||||
)
|
||||
await self.db.execute(query)
|
||||
await self.db.commit()
|
||||
qr = select(models.SubMenu).where(models.SubMenu.id == submenu_id)
|
||||
qr = select(SubMenu).where(SubMenu.id == submenu_id)
|
||||
updated_submenu = await self.db.execute(qr)
|
||||
return updated_submenu
|
||||
return updated_submenu.scalar_one()
|
||||
|
||||
async def delete_submenu_item(self, menu_id: UUID, submenu_id: UUID):
|
||||
query = delete(models.SubMenu).where(
|
||||
models.SubMenu.id == submenu_id,
|
||||
async def delete_submenu_item(self, menu_id: UUID, submenu_id: UUID) -> int:
|
||||
query = delete(SubMenu).where(
|
||||
SubMenu.id == submenu_id,
|
||||
)
|
||||
await self.db.execute(query)
|
||||
await self.db.commit()
|
||||
return 200
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
from typing import Optional
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException
|
||||
|
@ -12,14 +11,14 @@ router = APIRouter(
|
|||
)
|
||||
|
||||
|
||||
@router.get('/', response_model=Optional[list[SubMenuRead]])
|
||||
@router.get('/', response_model=list[SubMenuRead])
|
||||
async def get_submenus(
|
||||
menu_id: UUID,
|
||||
submenu: SubmenuService = Depends(),
|
||||
background_tasks: BackgroundTasks = BackgroundTasks(),
|
||||
):
|
||||
result = await submenu.read_submenus(menu_id=menu_id)
|
||||
return result.scalars().all()
|
||||
return result
|
||||
|
||||
|
||||
@router.post('/', status_code=201, response_model=SubMenuRead)
|
||||
|
@ -54,7 +53,7 @@ async def get_submenu(
|
|||
|
||||
@router.patch(
|
||||
'/{submenu_id}',
|
||||
response_model=MenuBase,
|
||||
response_model=SubMenuRead,
|
||||
)
|
||||
async def update_submenu(
|
||||
menu_id: UUID,
|
||||
|
@ -68,7 +67,7 @@ async def update_submenu(
|
|||
submenu_id=submenu_id,
|
||||
submenu_data=submenu_data,
|
||||
)
|
||||
return result.scalars().one()
|
||||
return result
|
||||
|
||||
|
||||
@router.delete('/{submenu_id}')
|
||||
|
|
|
@ -6,7 +6,7 @@ from fastapi import BackgroundTasks, Depends
|
|||
from fastfood.dbase import get_async_redis_client
|
||||
from fastfood.repository.redis import RedisRepository
|
||||
from fastfood.repository.submenu import SubMenuRepository
|
||||
from fastfood.schemas import MenuBase
|
||||
from fastfood.schemas import MenuBase, SubMenuRead
|
||||
|
||||
|
||||
class SubmenuService:
|
||||
|
@ -15,14 +15,23 @@ class SubmenuService:
|
|||
submenu_repo: SubMenuRepository = Depends(),
|
||||
redis_client: redis.Redis = Depends(get_async_redis_client),
|
||||
background_tasks: BackgroundTasks = None,
|
||||
):
|
||||
) -> None:
|
||||
|
||||
self.submenu_repo = submenu_repo
|
||||
self.cache_client = RedisRepository(redis_client)
|
||||
self.background_tasks = background_tasks
|
||||
|
||||
async def read_submenus(self, menu_id: UUID):
|
||||
async def read_submenus(self, menu_id: UUID) -> list[SubMenuRead]:
|
||||
data = await self.submenu_repo.get_submenus(menu_id=menu_id)
|
||||
return data
|
||||
submenus = []
|
||||
for r in data:
|
||||
submenu = r.__dict__
|
||||
subq = await self.submenu_repo.get_submenu_item(menu_id, r.id)
|
||||
if subq is not None:
|
||||
submenu['dishes_count'] = len(subq.dishes)
|
||||
submenu = SubMenuRead(**submenu)
|
||||
submenus.append(submenu)
|
||||
return submenus
|
||||
|
||||
async def create_submenu(self, menu_id: UUID, submenu_data: MenuBase):
|
||||
data = await self.submenu_repo.create_submenu_item(
|
||||
|
@ -44,5 +53,4 @@ class SubmenuService:
|
|||
return data
|
||||
|
||||
async def del_menu(self, menu_id: UUID, submenu_id: UUID):
|
||||
data = await self.submenu_repo.delete_submenu_item(menu_id, submenu_id)
|
||||
return data
|
||||
return await self.submenu_repo.delete_submenu_item(menu_id, submenu_id)
|
||||
|
|
Loading…
Reference in New Issue