Compare commits

...

2 Commits

7 changed files with 43 additions and 31 deletions

View File

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

View File

@ -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}')

View File

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

View File

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

View File

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

View File

@ -1,3 +0,0 @@
def price_converter(dish: dict) -> dict:
dish['price'] = str(dish['price'])
return dish

View File

@ -52,7 +52,7 @@ def app(event_loop) -> Generator[FastAPI, None, None]:
yield app
@pytest_asyncio.fixture()
@pytest_asyncio.fixture(scope='session')
async def client(app) -> AsyncGenerator[AsyncClient, None]:
async with AsyncClient(
app=app,