diff --git a/fastfood/routers/menu.py b/fastfood/routers/menu.py index cb1baf8..5c638e7 100644 --- a/fastfood/routers/menu.py +++ b/fastfood/routers/menu.py @@ -2,7 +2,7 @@ from uuid import UUID from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException -from fastfood.schemas import Menu, MenuBase, MenuRead +from fastfood.schemas import MenuBase, MenuRead from fastfood.service.menu import MenuService router = APIRouter( @@ -11,7 +11,7 @@ router = APIRouter( ) -@router.get('/', response_model=list[Menu]) +@router.get('/', response_model=list[MenuRead]) async def get_menus( menu: MenuService = Depends(), background_tasks: BackgroundTasks = BackgroundTasks(), @@ -19,7 +19,7 @@ async def get_menus( return await menu.read_menus() -@router.post('/', status_code=201, response_model=Menu) +@router.post('/', status_code=201, response_model=MenuRead) async def add_menu( menu: MenuBase, responce: MenuService = Depends(), diff --git a/fastfood/service/menu.py b/fastfood/service/menu.py index c3eb13f..e743afa 100644 --- a/fastfood/service/menu.py +++ b/fastfood/service/menu.py @@ -6,7 +6,7 @@ from fastapi import BackgroundTasks, Depends from fastfood.dbase import get_async_redis_client from fastfood.repository.menu import MenuRepository from fastfood.repository.redis import RedisRepository -from fastfood.schemas import MenuBase +from fastfood.schemas import MenuBase, MenuRead class MenuService: @@ -15,27 +15,68 @@ class MenuService: menu_repo: MenuRepository = Depends(), redis_client: redis.Redis = Depends(get_async_redis_client), background_tasks: BackgroundTasks = None, - ): + ) -> None: self.menu_repo = menu_repo self.cache_client = RedisRepository(redis_client) self.background_tasks = background_tasks - async def read_menus(self): + async def read_menus(self) -> list[MenuRead]: data = await self.menu_repo.get_menus() - return data + menus = [] + for r in data: + menu = r.__dict__ + menu = {k: v for k, v in menu.items() if not k.startswith('_')} + dishes_conter = 0 + for sub in r.submenus: + dishes_conter += len(sub.dishes) - async def create_menu(self, menu_data: MenuBase): + menu['submenus_count'] = len(menu.pop('submenus')) + menu['dishes_count'] = dishes_conter + menu = MenuRead(**menu) + menus.append(menu) + return menus + + async def create_menu(self, menu_data: MenuBase) -> MenuRead: data = await self.menu_repo.create_menu_item(menu_data) - return data + menu = data.__dict__ + menu = {k: v for k, v in menu.items() if not k.startswith('_')} + dishes_conter = 0 - async def read_menu(self, menu_id: UUID): + for sub in data.submenus: + dishes_conter += len(sub.dishes) + menu['submenus_count'] = len(menu.pop('submenus')) + menu['dishes_count'] = dishes_conter + + return MenuRead(**menu) + + async def read_menu(self, menu_id: UUID) -> MenuRead | None: data = await self.menu_repo.get_menu_item(menu_id) - return data + if data is None: + return None + menu = data.__dict__ + menu = {k: v for k, v in menu.items() if not k.startswith('_')} + dishes_conter = 0 - async def update_menu(self, menu_id: UUID, menu_data): + for sub in data.submenus: + dishes_conter += len(sub.dishes) + menu['submenus_count'] = len(menu.pop('submenus')) + menu['dishes_count'] = dishes_conter + + return MenuRead(**menu) + + async def update_menu(self, menu_id: UUID, menu_data) -> MenuRead: data = await self.menu_repo.update_menu_item(menu_id, menu_data) - return data + menu = data.__dict__ + menu = {k: v for k, v in menu.items() if not k.startswith('_')} + dishes_conter = 0 - async def del_menu(self, menu_id: UUID): + for sub in data.submenus: + dishes_conter += len(sub.dishes) + menu['submenus_count'] = len(menu.pop('submenus')) + menu['dishes_count'] = dishes_conter + + return MenuRead(**menu) + + async def del_menu(self, menu_id: UUID) -> int: data = await self.menu_repo.delete_menu_item(menu_id) return data