diff --git a/fastfood/repository/dish.py b/fastfood/repository/dish.py index 19c96a1..52090b4 100644 --- a/fastfood/repository/dish.py +++ b/fastfood/repository/dish.py @@ -6,7 +6,7 @@ from sqlalchemy.ext.asyncio import AsyncSession from fastfood import models from fastfood.dbase import get_async_session -from fastfood.schemas import DishBase +from fastfood.schemas import Dish_db class DishRepository: @@ -24,13 +24,13 @@ class DishRepository: self, menu_id: UUID, submenu_id: UUID, - dish_data: DishBase, + dish_data: Dish_db, ): new_dish = models.Dish(**dish_data.model_dump()) new_dish.parent_submenu = submenu_id self.db.add(new_dish) - await self.db.flush() await self.db.commit() + await self.db.refresh(new_dish) return new_dish async def get_dish_item( @@ -48,7 +48,7 @@ class DishRepository: menu_id: UUID, submenu_id: UUID, dish_id: UUID, - dish_data: DishBase, + dish_data: Dish_db, ): query = ( update(models.Dish) diff --git a/fastfood/routers/dish.py b/fastfood/routers/dish.py index 0c8fd33..b7fd910 100644 --- a/fastfood/routers/dish.py +++ b/fastfood/routers/dish.py @@ -2,9 +2,8 @@ from uuid import UUID from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException -from fastfood import schemas +from fastfood.schemas import Dish, DishBase from fastfood.service.dish import DishService -from fastfood.utils import price_converter router = APIRouter( prefix='/api/v1/menus/{menu_id}/submenus/{submenu_id}/dishes', @@ -12,7 +11,7 @@ router = APIRouter( ) -@router.get('/') +@router.get('/', response_model=list[Dish]) async def get_dishes( menu_id: UUID, submenu_id: UUID, @@ -23,23 +22,22 @@ async def get_dishes( return result -@router.post('/', status_code=201) +@router.post('/', status_code=201, response_model=Dish) async def create_dish( menu_id: UUID, submenu_id: UUID, - dish_data: schemas.DishBase, + dish_data: DishBase, dish: DishService = Depends(), background_tasks: BackgroundTasks = BackgroundTasks(), ): - result = await dish.create_dish( + return await dish.create_dish( menu_id, submenu_id, dish_data, ) - return price_converter(result) -@router.get('/{dish_id}') +@router.get('/{dish_id}', response_model=Dish) async def get_dish( menu_id: UUID, submenu_id: UUID, @@ -54,15 +52,15 @@ async def get_dish( ) if not result: raise HTTPException(status_code=404, detail='dish not found') - return price_converter(result) + return result -@router.patch('/{dish_id}') +@router.patch('/{dish_id}', response_model=Dish) async def update_dish( menu_id: UUID, submenu_id: UUID, dish_id: UUID, - dish_data: schemas.DishBase, + dish_data: DishBase, dish: DishService = Depends(), background_tasks: BackgroundTasks = BackgroundTasks(), ): @@ -72,7 +70,7 @@ async def update_dish( dish_id, dish_data, ) - return price_converter(result) + return result @router.delete('/{dish_id}') diff --git a/fastfood/routers/menu.py b/fastfood/routers/menu.py index 7f96f5b..b727d27 100644 --- a/fastfood/routers/menu.py +++ b/fastfood/routers/menu.py @@ -1,4 +1,3 @@ -from typing import Optional from uuid import UUID from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException @@ -12,7 +11,7 @@ router = APIRouter( ) -@router.get('/', response_model=Optional[list[Menu]]) +@router.get('/', response_model=list[Menu]) async def get_menus( menu: MenuService = Depends(), background_tasks: BackgroundTasks = BackgroundTasks(), diff --git a/fastfood/schemas.py b/fastfood/schemas.py index c14ff13..cf14ecb 100644 --- a/fastfood/schemas.py +++ b/fastfood/schemas.py @@ -25,8 +25,12 @@ class SubMenuRead(Menu): class DishBase(MenuBase): - price: float + price: str class Dish(DishBase, Menu): pass + + +class Dish_db(MenuBase): + price: float diff --git a/fastfood/service/dish.py b/fastfood/service/dish.py index 5dad1c4..d0c834b 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 DishBase +from fastfood.schemas import Dish_db, DishBase class DishService: @@ -22,6 +22,11 @@ class DishService: async def read_dishes(self, menu_id: UUID, submenu_id: UUID): 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) return data async def create_dish( @@ -30,24 +35,33 @@ class DishService: submenu_id: UUID, dish_data: DishBase, ): + dish = Dish_db(**dish_data.model_dump()) data = await self.dish_repo.create_dish_item( menu_id, submenu_id, - dish_data, + dish, ) - return data + response = data.__dict__ + response['price'] = str(response['price']) + return response async def read_dish(self, menu_id: UUID, submenu_id: UUID, dish_id: UUID): data = await self.dish_repo.get_dish_item(menu_id, submenu_id, dish_id) - return data + if data is None: + return + response = data.__dict__ + response['price'] = str(response['price']) + + return response async def update_dish( self, menu_id: UUID, submenu_id: UUID, dish_id, dish_data: DishBase ): - data = await self.dish_repo.update_dish_item( - menu_id, submenu_id, dish_id, dish_data - ) - return data + 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 async def del_dish(self, menu_id: UUID, submenu_id: UUID, dish_id: UUID): data = await self.dish_repo.delete_dish_item( diff --git a/fastfood/utils.py b/fastfood/utils.py deleted file mode 100644 index 7ef5890..0000000 --- a/fastfood/utils.py +++ /dev/null @@ -1,3 +0,0 @@ -def price_converter(dish: dict) -> dict: - dish['price'] = str(dish['price']) - return dish