service.dish typehint

develop
Сергей Ванюшкин 2024-02-04 02:49:06 +03:00
parent 015a0bcc87
commit 181c6f10af
2 changed files with 13 additions and 11 deletions

View File

@ -17,7 +17,7 @@ async def get_dishes(
submenu_id: UUID, submenu_id: UUID,
dish: DishService = Depends(), dish: DishService = Depends(),
background_tasks: BackgroundTasks = BackgroundTasks(), background_tasks: BackgroundTasks = BackgroundTasks(),
): ) -> list[Dish]:
result = await dish.read_dishes(menu_id, submenu_id) result = await dish.read_dishes(menu_id, submenu_id)
return result return result

View File

@ -6,7 +6,7 @@ from fastapi import BackgroundTasks, Depends
from fastfood.dbase import get_async_redis_client from fastfood.dbase import get_async_redis_client
from fastfood.repository.dish import DishRepository from fastfood.repository.dish import DishRepository
from fastfood.repository.redis import RedisRepository from fastfood.repository.redis import RedisRepository
from fastfood.schemas import Dish_db, DishBase from fastfood.schemas import Dish, Dish_db, DishBase
class DishService: class DishService:
@ -20,13 +20,13 @@ class DishService:
self.cache_client = RedisRepository(redis_client) self.cache_client = RedisRepository(redis_client)
self.background_tasks = background_tasks 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) data = await self.dish_repo.get_dishes(menu_id, submenu_id)
response = [] response = []
for row in data: for row in data:
dish = row.__dict__ dish = row.__dict__
dish['price'] = str(dish['price']) dish['price'] = str(dish['price'])
response.append(dish) response.append(Dish(**dish))
return response return response
async def create_dish( async def create_dish(
@ -34,7 +34,7 @@ class DishService:
menu_id: UUID, menu_id: UUID,
submenu_id: UUID, submenu_id: UUID,
dish_data: DishBase, dish_data: DishBase,
) -> dict: ) -> Dish:
dish = Dish_db(**dish_data.model_dump()) dish = Dish_db(**dish_data.model_dump())
data = await self.dish_repo.create_dish_item( data = await self.dish_repo.create_dish_item(
menu_id, menu_id,
@ -43,24 +43,26 @@ class DishService:
) )
response = data.__dict__ response = data.__dict__
response['price'] = str(response['price']) 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) data = await self.dish_repo.get_dish_item(menu_id, submenu_id, dish_id)
if data is None: if data is None:
return {} return None
response = data.__dict__ response = data.__dict__
response['price'] = str(response['price']) response['price'] = str(response['price'])
return response return Dish(**response)
async def update_dish( async def update_dish(
self, menu_id: UUID, submenu_id: UUID, dish_id, dish_data: DishBase self, menu_id: UUID, submenu_id: UUID, dish_id, dish_data: DishBase
) -> dict: ) -> Dish:
dish = Dish_db(**dish_data.model_dump()) dish = Dish_db(**dish_data.model_dump())
data = await self.dish_repo.update_dish_item(menu_id, submenu_id, dish_id, dish) data = await self.dish_repo.update_dish_item(menu_id, submenu_id, dish_id, dish)
response = data.__dict__ response = data.__dict__
response['price'] = str(response['price']) 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: async def del_dish(self, menu_id: UUID, submenu_id: UUID, dish_id: UUID) -> int:
response = await self.dish_repo.delete_dish_item( response = await self.dish_repo.delete_dish_item(