From 181c6f10af267653d4de8ca6882b7e6b64c2d689 Mon Sep 17 00:00:00 2001 From: pi3c Date: Sun, 4 Feb 2024 02:49:06 +0300 Subject: [PATCH] service.dish typehint --- fastfood/routers/dish.py | 2 +- fastfood/service/dish.py | 22 ++++++++++++---------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/fastfood/routers/dish.py b/fastfood/routers/dish.py index b7fd910..b23ac1b 100644 --- a/fastfood/routers/dish.py +++ b/fastfood/routers/dish.py @@ -17,7 +17,7 @@ async def get_dishes( submenu_id: UUID, dish: DishService = Depends(), background_tasks: BackgroundTasks = BackgroundTasks(), -): +) -> list[Dish]: result = await dish.read_dishes(menu_id, submenu_id) return result diff --git a/fastfood/service/dish.py b/fastfood/service/dish.py index ef7766b..675f805 100644 --- a/fastfood/service/dish.py +++ b/fastfood/service/dish.py @@ -6,7 +6,7 @@ from fastapi import BackgroundTasks, Depends from fastfood.dbase import get_async_redis_client from fastfood.repository.dish import DishRepository from fastfood.repository.redis import RedisRepository -from fastfood.schemas import Dish_db, DishBase +from fastfood.schemas import Dish, Dish_db, DishBase class DishService: @@ -20,13 +20,13 @@ class DishService: self.cache_client = RedisRepository(redis_client) self.background_tasks = background_tasks - async def read_dishes(self, menu_id: UUID, submenu_id: UUID) -> list[dict]: + async def read_dishes(self, menu_id: UUID, submenu_id: UUID) -> list[Dish]: data = await self.dish_repo.get_dishes(menu_id, submenu_id) response = [] for row in data: dish = row.__dict__ dish['price'] = str(dish['price']) - response.append(dish) + response.append(Dish(**dish)) return response async def create_dish( @@ -34,7 +34,7 @@ class DishService: menu_id: UUID, submenu_id: UUID, dish_data: DishBase, - ) -> dict: + ) -> Dish: dish = Dish_db(**dish_data.model_dump()) data = await self.dish_repo.create_dish_item( menu_id, @@ -43,24 +43,26 @@ class DishService: ) response = data.__dict__ response['price'] = str(response['price']) - return response + return Dish(**response) - async def read_dish(self, menu_id: UUID, submenu_id: UUID, dish_id: UUID) -> dict: + async def read_dish( + self, menu_id: UUID, submenu_id: UUID, dish_id: UUID + ) -> Dish | None: data = await self.dish_repo.get_dish_item(menu_id, submenu_id, dish_id) if data is None: - return {} + return None response = data.__dict__ response['price'] = str(response['price']) - return response + return Dish(**response) async def update_dish( self, menu_id: UUID, submenu_id: UUID, dish_id, dish_data: DishBase - ) -> dict: + ) -> Dish: dish = Dish_db(**dish_data.model_dump()) data = await self.dish_repo.update_dish_item(menu_id, submenu_id, dish_id, dish) response = data.__dict__ response['price'] = str(response['price']) - return response + return Dish(**response) async def del_dish(self, menu_id: UUID, submenu_id: UUID, dish_id: UUID) -> int: response = await self.dish_repo.delete_dish_item(