service.menu typehint

develop
Сергей Ванюшкин 2024-02-04 18:26:17 +03:00
parent 5173fcd36c
commit 09d0627d70
2 changed files with 55 additions and 14 deletions

View File

@ -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(),

View File

@ -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