Compare commits

..

No commits in common. "f807bdd2751172229194e84720f9dcd779f20b61" and "45dd8dc73e75fa94efcc21585d325d15a16ff300" have entirely different histories.

7 changed files with 31 additions and 43 deletions

View File

@ -6,7 +6,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
from fastfood import models from fastfood import models
from fastfood.dbase import get_async_session from fastfood.dbase import get_async_session
from fastfood.schemas import Dish_db from fastfood.schemas import DishBase
class DishRepository: class DishRepository:
@ -24,13 +24,13 @@ class DishRepository:
self, self,
menu_id: UUID, menu_id: UUID,
submenu_id: UUID, submenu_id: UUID,
dish_data: Dish_db, dish_data: DishBase,
): ):
new_dish = models.Dish(**dish_data.model_dump()) new_dish = models.Dish(**dish_data.model_dump())
new_dish.parent_submenu = submenu_id new_dish.parent_submenu = submenu_id
self.db.add(new_dish) self.db.add(new_dish)
await self.db.flush()
await self.db.commit() await self.db.commit()
await self.db.refresh(new_dish)
return new_dish return new_dish
async def get_dish_item( async def get_dish_item(
@ -48,7 +48,7 @@ class DishRepository:
menu_id: UUID, menu_id: UUID,
submenu_id: UUID, submenu_id: UUID,
dish_id: UUID, dish_id: UUID,
dish_data: Dish_db, dish_data: DishBase,
): ):
query = ( query = (
update(models.Dish) update(models.Dish)

View File

@ -2,8 +2,9 @@ from uuid import UUID
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException
from fastfood.schemas import Dish, DishBase from fastfood import schemas
from fastfood.service.dish import DishService from fastfood.service.dish import DishService
from fastfood.utils import price_converter
router = APIRouter( router = APIRouter(
prefix='/api/v1/menus/{menu_id}/submenus/{submenu_id}/dishes', prefix='/api/v1/menus/{menu_id}/submenus/{submenu_id}/dishes',
@ -11,7 +12,7 @@ router = APIRouter(
) )
@router.get('/', response_model=list[Dish]) @router.get('/')
async def get_dishes( async def get_dishes(
menu_id: UUID, menu_id: UUID,
submenu_id: UUID, submenu_id: UUID,
@ -22,22 +23,23 @@ async def get_dishes(
return result return result
@router.post('/', status_code=201, response_model=Dish) @router.post('/', status_code=201)
async def create_dish( async def create_dish(
menu_id: UUID, menu_id: UUID,
submenu_id: UUID, submenu_id: UUID,
dish_data: DishBase, dish_data: schemas.DishBase,
dish: DishService = Depends(), dish: DishService = Depends(),
background_tasks: BackgroundTasks = BackgroundTasks(), background_tasks: BackgroundTasks = BackgroundTasks(),
): ):
return await dish.create_dish( result = await dish.create_dish(
menu_id, menu_id,
submenu_id, submenu_id,
dish_data, dish_data,
) )
return price_converter(result)
@router.get('/{dish_id}', response_model=Dish) @router.get('/{dish_id}')
async def get_dish( async def get_dish(
menu_id: UUID, menu_id: UUID,
submenu_id: UUID, submenu_id: UUID,
@ -52,15 +54,15 @@ async def get_dish(
) )
if not result: if not result:
raise HTTPException(status_code=404, detail='dish not found') raise HTTPException(status_code=404, detail='dish not found')
return result return price_converter(result)
@router.patch('/{dish_id}', response_model=Dish) @router.patch('/{dish_id}')
async def update_dish( async def update_dish(
menu_id: UUID, menu_id: UUID,
submenu_id: UUID, submenu_id: UUID,
dish_id: UUID, dish_id: UUID,
dish_data: DishBase, dish_data: schemas.DishBase,
dish: DishService = Depends(), dish: DishService = Depends(),
background_tasks: BackgroundTasks = BackgroundTasks(), background_tasks: BackgroundTasks = BackgroundTasks(),
): ):
@ -70,7 +72,7 @@ async def update_dish(
dish_id, dish_id,
dish_data, dish_data,
) )
return result return price_converter(result)
@router.delete('/{dish_id}') @router.delete('/{dish_id}')

View File

@ -1,3 +1,4 @@
from typing import Optional
from uuid import UUID from uuid import UUID
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException
@ -11,7 +12,7 @@ router = APIRouter(
) )
@router.get('/', response_model=list[Menu]) @router.get('/', response_model=Optional[list[Menu]])
async def get_menus( async def get_menus(
menu: MenuService = Depends(), menu: MenuService = Depends(),
background_tasks: BackgroundTasks = BackgroundTasks(), background_tasks: BackgroundTasks = BackgroundTasks(),

View File

@ -25,12 +25,8 @@ class SubMenuRead(Menu):
class DishBase(MenuBase): class DishBase(MenuBase):
price: str price: float
class Dish(DishBase, Menu): class Dish(DishBase, Menu):
pass 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.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 DishBase
class DishService: class DishService:
@ -22,11 +22,6 @@ class DishService:
async def read_dishes(self, menu_id: UUID, submenu_id: UUID): async def read_dishes(self, menu_id: UUID, submenu_id: UUID):
data = await self.dish_repo.get_dishes(menu_id, submenu_id) 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 return data
async def create_dish( async def create_dish(
@ -35,33 +30,24 @@ class DishService:
submenu_id: UUID, submenu_id: UUID,
dish_data: DishBase, dish_data: DishBase,
): ):
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,
submenu_id, submenu_id,
dish, dish_data,
) )
response = data.__dict__ return data
response['price'] = str(response['price'])
return response
async def read_dish(self, menu_id: UUID, submenu_id: UUID, dish_id: UUID): 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) data = await self.dish_repo.get_dish_item(menu_id, submenu_id, dish_id)
if data is None: return data
return
response = data.__dict__
response['price'] = str(response['price'])
return 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
): ):
dish = Dish_db(**dish_data.model_dump()) data = await self.dish_repo.update_dish_item(
data = await self.dish_repo.update_dish_item(menu_id, submenu_id, dish_id, dish) menu_id, submenu_id, dish_id, dish_data
response = data.__dict__ )
response['price'] = str(response['price']) return data
return response
async def del_dish(self, menu_id: UUID, submenu_id: UUID, dish_id: UUID): async def del_dish(self, menu_id: UUID, submenu_id: UUID, dish_id: UUID):
data = await self.dish_repo.delete_dish_item( data = await self.dish_repo.delete_dish_item(

3
fastfood/utils.py Normal file
View File

@ -0,0 +1,3 @@
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 yield app
@pytest_asyncio.fixture(scope='session') @pytest_asyncio.fixture()
async def client(app) -> AsyncGenerator[AsyncClient, None]: async def client(app) -> AsyncGenerator[AsyncClient, None]:
async with AsyncClient( async with AsyncClient(
app=app, app=app,