Compare commits
5 Commits
a2ed5a6732
...
ee709a489e
Author | SHA1 | Date |
---|---|---|
Сергей Ванюшкин | ee709a489e | |
Сергей Ванюшкин | f8cca4b861 | |
Сергей Ванюшкин | 7d4c4d9be3 | |
Сергей Ванюшкин | 095ab07ebb | |
Сергей Ванюшкин | f72c6fe4d7 |
|
@ -6,43 +6,20 @@ from fastfood.routers.dish import router as dish_router
|
||||||
from fastfood.routers.menu import router as menu_router
|
from fastfood.routers.menu import router as menu_router
|
||||||
from fastfood.routers.submenu import router as submenu_router
|
from fastfood.routers.submenu import router as submenu_router
|
||||||
|
|
||||||
tags_metadata = [
|
|
||||||
{
|
|
||||||
'name': 'menu',
|
|
||||||
'description': 'Операции с меню.',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
'name': 'submenu',
|
|
||||||
'description': 'Подменю и работа с ним',
|
|
||||||
},
|
|
||||||
{'name': 'dish', 'description': 'Блюда и работа с ними'},
|
|
||||||
]
|
|
||||||
|
|
||||||
|
def create_app() -> FastAPI:
|
||||||
def create_app(redis=None) -> FastAPI:
|
|
||||||
"""
|
"""
|
||||||
Фабрика FastAPI.
|
Фабрика FastAPI.
|
||||||
"""
|
"""
|
||||||
with open('openapi.json') as f:
|
app = FastAPI()
|
||||||
js = json.load(f)
|
|
||||||
|
|
||||||
app = FastAPI(
|
|
||||||
title=js['info']['title'],
|
|
||||||
description=js['info']['description'],
|
|
||||||
version=js['info']['version'],
|
|
||||||
contact={
|
|
||||||
'name': 'Sergey Vanyushkin',
|
|
||||||
'url': 'http://pi3c.ru',
|
|
||||||
'email': 'pi3c@yandex.ru',
|
|
||||||
},
|
|
||||||
license_info={
|
|
||||||
'name': 'MIT license',
|
|
||||||
'url': 'https://mit-license.org/',
|
|
||||||
},
|
|
||||||
openapi_tags=tags_metadata,
|
|
||||||
)
|
|
||||||
app.include_router(menu_router)
|
app.include_router(menu_router)
|
||||||
app.include_router(submenu_router)
|
app.include_router(submenu_router)
|
||||||
app.include_router(dish_router)
|
app.include_router(dish_router)
|
||||||
|
|
||||||
|
def custom_openapi():
|
||||||
|
with open('openapi.json') as openapi:
|
||||||
|
return json.load(openapi)
|
||||||
|
|
||||||
|
app.openapi = custom_openapi
|
||||||
|
|
||||||
return app
|
return app
|
||||||
|
|
|
@ -10,7 +10,7 @@ from fastfood.schemas import Dish_db
|
||||||
|
|
||||||
|
|
||||||
class DishRepository:
|
class DishRepository:
|
||||||
def __init__(self, session: AsyncSession = Depends(get_async_session)):
|
def __init__(self, session: AsyncSession = Depends(get_async_session)) -> None:
|
||||||
self.db = session
|
self.db = session
|
||||||
|
|
||||||
async def get_dishes(self, menu_id: UUID, submenu_id: UUID) -> list[Dish]:
|
async def get_dishes(self, menu_id: UUID, submenu_id: UUID) -> list[Dish]:
|
||||||
|
@ -49,21 +49,20 @@ class DishRepository:
|
||||||
submenu_id: UUID,
|
submenu_id: UUID,
|
||||||
dish_id: UUID,
|
dish_id: UUID,
|
||||||
dish_data: Dish_db,
|
dish_data: Dish_db,
|
||||||
) -> Dish:
|
) -> Dish | None:
|
||||||
query = update(Dish).where(Dish.id == dish_id).values(**dish_data.model_dump())
|
query = update(Dish).where(Dish.id == dish_id).values(**dish_data.model_dump())
|
||||||
await self.db.execute(query)
|
await self.db.execute(query)
|
||||||
await self.db.commit()
|
await self.db.commit()
|
||||||
qr = select(Dish).where(Dish.id == dish_id)
|
qr = select(Dish).where(Dish.id == dish_id)
|
||||||
updated_submenu = await self.db.execute(qr)
|
updated_submenu = await self.db.execute(qr)
|
||||||
return updated_submenu.scalars().one()
|
return updated_submenu.scalar_one_or_none()
|
||||||
|
|
||||||
async def delete_dish_item(
|
async def delete_dish_item(
|
||||||
self,
|
self,
|
||||||
menu_id: UUID,
|
menu_id: UUID,
|
||||||
submenu_id: UUID,
|
submenu_id: UUID,
|
||||||
dish_id: UUID,
|
dish_id: UUID,
|
||||||
) -> int:
|
) -> None:
|
||||||
query = delete(Dish).where(Dish.id == dish_id)
|
query = delete(Dish).where(Dish.id == dish_id)
|
||||||
await self.db.execute(query)
|
await self.db.execute(query)
|
||||||
await self.db.commit()
|
await self.db.commit()
|
||||||
return 200
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ from fastfood.models import Dish, Menu, SubMenu
|
||||||
|
|
||||||
|
|
||||||
class MenuRepository:
|
class MenuRepository:
|
||||||
def __init__(self, session: AsyncSession = Depends(get_async_session)):
|
def __init__(self, session: AsyncSession = Depends(get_async_session)) -> None:
|
||||||
self.db = session
|
self.db = session
|
||||||
|
|
||||||
async def get_menus(self) -> list[Menu]:
|
async def get_menus(self) -> list[Menu]:
|
||||||
|
@ -44,23 +44,21 @@ class MenuRepository:
|
||||||
)
|
)
|
||||||
menu = await self.db.execute(query)
|
menu = await self.db.execute(query)
|
||||||
menu = menu.scalars().one_or_none()
|
menu = menu.scalars().one_or_none()
|
||||||
if menu is None:
|
|
||||||
return None
|
|
||||||
return menu
|
return menu
|
||||||
|
|
||||||
async def update_menu_item(
|
async def update_menu_item(
|
||||||
self,
|
self,
|
||||||
menu_id: UUID,
|
menu_id: UUID,
|
||||||
menu: schemas.MenuBase,
|
menu: schemas.MenuBase,
|
||||||
) -> Menu:
|
) -> Menu | None:
|
||||||
query = update(Menu).where(Menu.id == menu_id).values(**menu.model_dump())
|
query = update(Menu).where(Menu.id == menu_id).values(**menu.model_dump())
|
||||||
await self.db.execute(query)
|
await self.db.execute(query)
|
||||||
await self.db.commit()
|
await self.db.commit()
|
||||||
qr = select(Menu).where(Menu.id == menu_id)
|
qr = select(Menu).where(Menu.id == menu_id)
|
||||||
updated_menu = await self.db.execute(qr)
|
updated_menu = await self.db.execute(qr)
|
||||||
return updated_menu.scalar_one()
|
return updated_menu.scalar_one_or_none()
|
||||||
|
|
||||||
async def delete_menu_item(self, menu_id: UUID):
|
async def delete_menu_item(self, menu_id: UUID) -> None:
|
||||||
query = delete(Menu).where(Menu.id == menu_id)
|
query = delete(Menu).where(Menu.id == menu_id)
|
||||||
await self.db.execute(query)
|
await self.db.execute(query)
|
||||||
await self.db.commit()
|
await self.db.commit()
|
||||||
|
|
|
@ -11,7 +11,7 @@ from fastfood.schemas import MenuBase
|
||||||
|
|
||||||
|
|
||||||
class SubMenuRepository:
|
class SubMenuRepository:
|
||||||
def __init__(self, session: AsyncSession = Depends(get_async_session)):
|
def __init__(self, session: AsyncSession = Depends(get_async_session)) -> None:
|
||||||
self.db = session
|
self.db = session
|
||||||
|
|
||||||
async def get_submenus(self, menu_id: UUID) -> list[SubMenu]:
|
async def get_submenus(self, menu_id: UUID) -> list[SubMenu]:
|
||||||
|
@ -52,8 +52,6 @@ class SubMenuRepository:
|
||||||
)
|
)
|
||||||
submenu = await self.db.execute(query)
|
submenu = await self.db.execute(query)
|
||||||
submenu = submenu.scalars().one_or_none()
|
submenu = submenu.scalars().one_or_none()
|
||||||
if submenu is None:
|
|
||||||
return None
|
|
||||||
return submenu
|
return submenu
|
||||||
|
|
||||||
async def update_submenu_item(
|
async def update_submenu_item(
|
||||||
|
@ -61,7 +59,7 @@ class SubMenuRepository:
|
||||||
menu_id: UUID,
|
menu_id: UUID,
|
||||||
submenu_id: UUID,
|
submenu_id: UUID,
|
||||||
submenu_data: MenuBase,
|
submenu_data: MenuBase,
|
||||||
) -> SubMenu:
|
) -> SubMenu | None:
|
||||||
query = (
|
query = (
|
||||||
update(SubMenu)
|
update(SubMenu)
|
||||||
.where(SubMenu.id == submenu_id)
|
.where(SubMenu.id == submenu_id)
|
||||||
|
@ -71,12 +69,11 @@ class SubMenuRepository:
|
||||||
await self.db.commit()
|
await self.db.commit()
|
||||||
qr = select(SubMenu).where(SubMenu.id == submenu_id)
|
qr = select(SubMenu).where(SubMenu.id == submenu_id)
|
||||||
updated_submenu = await self.db.execute(qr)
|
updated_submenu = await self.db.execute(qr)
|
||||||
return updated_submenu.scalar_one()
|
return updated_submenu.scalar_one_or_none()
|
||||||
|
|
||||||
async def delete_submenu_item(self, menu_id: UUID, submenu_id: UUID) -> int:
|
async def delete_submenu_item(self, menu_id: UUID, submenu_id: UUID) -> None:
|
||||||
query = delete(SubMenu).where(
|
query = delete(SubMenu).where(
|
||||||
SubMenu.id == submenu_id,
|
SubMenu.id == submenu_id,
|
||||||
)
|
)
|
||||||
await self.db.execute(query)
|
await self.db.execute(query)
|
||||||
await self.db.commit()
|
await self.db.commit()
|
||||||
return 200
|
|
||||||
|
|
|
@ -11,7 +11,10 @@ router = APIRouter(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.get('/', response_model=list[Dish])
|
@router.get(
|
||||||
|
'/',
|
||||||
|
response_model=list[Dish],
|
||||||
|
)
|
||||||
async def get_dishes(
|
async def get_dishes(
|
||||||
menu_id: UUID,
|
menu_id: UUID,
|
||||||
submenu_id: UUID,
|
submenu_id: UUID,
|
||||||
|
@ -22,14 +25,18 @@ async def get_dishes(
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
@router.post('/', status_code=201, response_model=Dish)
|
@router.post(
|
||||||
|
'/',
|
||||||
|
status_code=201,
|
||||||
|
response_model=Dish,
|
||||||
|
)
|
||||||
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: DishBase,
|
||||||
dish: DishService = Depends(),
|
dish: DishService = Depends(),
|
||||||
background_tasks: BackgroundTasks = BackgroundTasks(),
|
background_tasks: BackgroundTasks = BackgroundTasks(),
|
||||||
):
|
) -> Dish:
|
||||||
return await dish.create_dish(
|
return await dish.create_dish(
|
||||||
menu_id,
|
menu_id,
|
||||||
submenu_id,
|
submenu_id,
|
||||||
|
@ -37,25 +44,34 @@ async def create_dish(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.get('/{dish_id}', response_model=Dish)
|
@router.get(
|
||||||
|
'/{dish_id}',
|
||||||
|
response_model=Dish,
|
||||||
|
)
|
||||||
async def get_dish(
|
async def get_dish(
|
||||||
menu_id: UUID,
|
menu_id: UUID,
|
||||||
submenu_id: UUID,
|
submenu_id: UUID,
|
||||||
dish_id: UUID,
|
dish_id: UUID,
|
||||||
dish: DishService = Depends(),
|
dish: DishService = Depends(),
|
||||||
background_tasks: BackgroundTasks = BackgroundTasks(),
|
background_tasks: BackgroundTasks = BackgroundTasks(),
|
||||||
):
|
) -> Dish | None:
|
||||||
result = await dish.read_dish(
|
result = await dish.read_dish(
|
||||||
menu_id,
|
menu_id,
|
||||||
submenu_id,
|
submenu_id,
|
||||||
dish_id,
|
dish_id,
|
||||||
)
|
)
|
||||||
if not result:
|
if not result:
|
||||||
raise HTTPException(status_code=404, detail='dish not found')
|
raise HTTPException(
|
||||||
|
status_code=404,
|
||||||
|
detail=f'Блюдо c UUID={dish_id} не существует, доступ невозможен',
|
||||||
|
)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
@router.patch('/{dish_id}', response_model=Dish)
|
@router.patch(
|
||||||
|
'/{dish_id}',
|
||||||
|
response_model=Dish,
|
||||||
|
)
|
||||||
async def update_dish(
|
async def update_dish(
|
||||||
menu_id: UUID,
|
menu_id: UUID,
|
||||||
submenu_id: UUID,
|
submenu_id: UUID,
|
||||||
|
@ -63,22 +79,29 @@ async def update_dish(
|
||||||
dish_data: DishBase,
|
dish_data: DishBase,
|
||||||
dish: DishService = Depends(),
|
dish: DishService = Depends(),
|
||||||
background_tasks: BackgroundTasks = BackgroundTasks(),
|
background_tasks: BackgroundTasks = BackgroundTasks(),
|
||||||
):
|
) -> Dish:
|
||||||
result = await dish.update_dish(
|
result = await dish.update_dish(
|
||||||
menu_id,
|
menu_id,
|
||||||
submenu_id,
|
submenu_id,
|
||||||
dish_id,
|
dish_id,
|
||||||
dish_data,
|
dish_data,
|
||||||
)
|
)
|
||||||
|
if not result:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=404,
|
||||||
|
detail=f'Блюдо c UUID={dish_id} не существует, обновление невозможно',
|
||||||
|
)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
@router.delete('/{dish_id}')
|
@router.delete(
|
||||||
|
'/{dish_id}',
|
||||||
|
)
|
||||||
async def delete_dish(
|
async def delete_dish(
|
||||||
menu_id: UUID,
|
menu_id: UUID,
|
||||||
submenu_id: UUID,
|
submenu_id: UUID,
|
||||||
dish_id: UUID,
|
dish_id: UUID,
|
||||||
dish: DishService = Depends(),
|
dish: DishService = Depends(),
|
||||||
background_tasks: BackgroundTasks = BackgroundTasks(),
|
background_tasks: BackgroundTasks = BackgroundTasks(),
|
||||||
):
|
) -> None:
|
||||||
await dish.del_dish(menu_id, submenu_id, dish_id)
|
await dish.del_dish(menu_id, submenu_id, dish_id)
|
||||||
|
|
|
@ -11,54 +11,80 @@ router = APIRouter(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.get('/', response_model=list[MenuRead])
|
@router.get(
|
||||||
|
'/',
|
||||||
|
status_code=200,
|
||||||
|
response_model=list[MenuRead],
|
||||||
|
)
|
||||||
async def get_menus(
|
async def get_menus(
|
||||||
menu: MenuService = Depends(),
|
menu: MenuService = Depends(),
|
||||||
background_tasks: BackgroundTasks = BackgroundTasks(),
|
background_tasks: BackgroundTasks = BackgroundTasks(),
|
||||||
):
|
) -> list[MenuRead]:
|
||||||
return await menu.read_menus()
|
return await menu.read_menus()
|
||||||
|
|
||||||
|
|
||||||
@router.post('/', status_code=201, response_model=MenuRead)
|
@router.post(
|
||||||
|
'/',
|
||||||
|
status_code=201,
|
||||||
|
response_model=MenuRead,
|
||||||
|
)
|
||||||
async def add_menu(
|
async def add_menu(
|
||||||
menu: MenuBase,
|
menu: MenuBase,
|
||||||
responce: MenuService = Depends(),
|
responce: MenuService = Depends(),
|
||||||
background_tasks: BackgroundTasks = BackgroundTasks(),
|
background_tasks: BackgroundTasks = BackgroundTasks(),
|
||||||
):
|
) -> MenuRead:
|
||||||
return await responce.create_menu(menu)
|
return await responce.create_menu(menu)
|
||||||
|
|
||||||
|
|
||||||
@router.get('/{menu_id}', response_model=MenuRead)
|
@router.get(
|
||||||
|
'/{menu_id}',
|
||||||
|
response_model=MenuRead,
|
||||||
|
)
|
||||||
async def get_menu(
|
async def get_menu(
|
||||||
menu_id: UUID,
|
menu_id: UUID,
|
||||||
responce: MenuService = Depends(),
|
responce: MenuService = Depends(),
|
||||||
background_tasks: BackgroundTasks = BackgroundTasks(),
|
background_tasks: BackgroundTasks = BackgroundTasks(),
|
||||||
):
|
) -> MenuRead:
|
||||||
result = await responce.read_menu(menu_id=menu_id)
|
result = await responce.read_menu(menu_id=menu_id)
|
||||||
|
|
||||||
if not result:
|
if not result:
|
||||||
raise HTTPException(status_code=404, detail='menu not found')
|
raise HTTPException(
|
||||||
|
status_code=404,
|
||||||
|
detail=f'Меню c UUID={menu_id} не существует, доступ невозможен',
|
||||||
|
)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
@router.patch('/{menu_id}', response_model=MenuRead)
|
@router.patch(
|
||||||
|
'/{menu_id}',
|
||||||
|
response_model=MenuRead,
|
||||||
|
)
|
||||||
async def update_menu(
|
async def update_menu(
|
||||||
menu_id: UUID,
|
menu_id: UUID,
|
||||||
menu: MenuBase,
|
menu: MenuBase,
|
||||||
responce: MenuService = Depends(),
|
responce: MenuService = Depends(),
|
||||||
background_tasks: BackgroundTasks = BackgroundTasks(),
|
background_tasks: BackgroundTasks = BackgroundTasks(),
|
||||||
):
|
) -> MenuRead:
|
||||||
result = await responce.update_menu(
|
result = await responce.update_menu(
|
||||||
menu_id=menu_id,
|
menu_id=menu_id,
|
||||||
menu_data=menu,
|
menu_data=menu,
|
||||||
)
|
)
|
||||||
|
if not result:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=404,
|
||||||
|
detail=f'Меню c UUID={menu_id} не существует, Обновление невозможно',
|
||||||
|
)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
@router.delete('/{menu_id}')
|
@router.delete(
|
||||||
|
'/{menu_id}',
|
||||||
|
status_code=200,
|
||||||
|
)
|
||||||
async def delete_menu(
|
async def delete_menu(
|
||||||
menu_id: UUID,
|
menu_id: UUID,
|
||||||
menu: MenuService = Depends(),
|
menu: MenuService = Depends(),
|
||||||
background_tasks: BackgroundTasks = BackgroundTasks(),
|
background_tasks: BackgroundTasks = BackgroundTasks(),
|
||||||
):
|
) -> None:
|
||||||
await menu.del_menu(menu_id)
|
await menu.del_menu(menu_id)
|
||||||
|
|
|
@ -11,23 +11,30 @@ router = APIRouter(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.get('/', response_model=list[SubMenuRead])
|
@router.get(
|
||||||
|
'/',
|
||||||
|
response_model=list[SubMenuRead],
|
||||||
|
)
|
||||||
async def get_submenus(
|
async def get_submenus(
|
||||||
menu_id: UUID,
|
menu_id: UUID,
|
||||||
submenu: SubmenuService = Depends(),
|
submenu: SubmenuService = Depends(),
|
||||||
background_tasks: BackgroundTasks = BackgroundTasks(),
|
background_tasks: BackgroundTasks = BackgroundTasks(),
|
||||||
):
|
) -> list[SubMenuRead]:
|
||||||
result = await submenu.read_submenus(menu_id=menu_id)
|
result = await submenu.read_submenus(menu_id=menu_id)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
@router.post('/', status_code=201, response_model=SubMenuRead)
|
@router.post(
|
||||||
|
'/',
|
||||||
|
status_code=201,
|
||||||
|
response_model=SubMenuRead,
|
||||||
|
)
|
||||||
async def create_submenu_item(
|
async def create_submenu_item(
|
||||||
menu_id: UUID,
|
menu_id: UUID,
|
||||||
submenu_data: MenuBase,
|
submenu_data: MenuBase,
|
||||||
submenu: SubmenuService = Depends(),
|
submenu: SubmenuService = Depends(),
|
||||||
background_tasks: BackgroundTasks = BackgroundTasks(),
|
background_tasks: BackgroundTasks = BackgroundTasks(),
|
||||||
):
|
) -> SubMenuRead:
|
||||||
result = await submenu.create_submenu(
|
result = await submenu.create_submenu(
|
||||||
menu_id=menu_id,
|
menu_id=menu_id,
|
||||||
submenu_data=submenu_data,
|
submenu_data=submenu_data,
|
||||||
|
@ -35,19 +42,25 @@ async def create_submenu_item(
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
@router.get('/{submenu_id}', response_model=SubMenuRead)
|
@router.get(
|
||||||
|
'/{submenu_id}',
|
||||||
|
response_model=SubMenuRead,
|
||||||
|
)
|
||||||
async def get_submenu(
|
async def get_submenu(
|
||||||
menu_id: UUID,
|
menu_id: UUID,
|
||||||
submenu_id: UUID,
|
submenu_id: UUID,
|
||||||
submenu: SubmenuService = Depends(),
|
submenu: SubmenuService = Depends(),
|
||||||
background_tasks: BackgroundTasks = BackgroundTasks(),
|
background_tasks: BackgroundTasks = BackgroundTasks(),
|
||||||
):
|
) -> SubMenuRead:
|
||||||
result = await submenu.read_menu(
|
result = await submenu.read_menu(
|
||||||
menu_id=menu_id,
|
menu_id=menu_id,
|
||||||
submenu_id=submenu_id,
|
submenu_id=submenu_id,
|
||||||
)
|
)
|
||||||
if not result:
|
if not result:
|
||||||
raise HTTPException(status_code=404, detail='submenu not found')
|
raise HTTPException(
|
||||||
|
status_code=404,
|
||||||
|
detail=f'Подменю c UUID={submenu_id} не существует, доступ невозможен',
|
||||||
|
)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
@ -61,20 +74,28 @@ async def update_submenu(
|
||||||
submenu_data: MenuBase,
|
submenu_data: MenuBase,
|
||||||
submenu: SubmenuService = Depends(),
|
submenu: SubmenuService = Depends(),
|
||||||
background_tasks: BackgroundTasks = BackgroundTasks(),
|
background_tasks: BackgroundTasks = BackgroundTasks(),
|
||||||
):
|
) -> SubMenuRead:
|
||||||
result = await submenu.update_submenu(
|
result = await submenu.update_submenu(
|
||||||
menu_id=menu_id,
|
menu_id=menu_id,
|
||||||
submenu_id=submenu_id,
|
submenu_id=submenu_id,
|
||||||
submenu_data=submenu_data,
|
submenu_data=submenu_data,
|
||||||
)
|
)
|
||||||
|
if not result:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=404,
|
||||||
|
detail=f'Gjlvеню c UUID={submenu_id} не существует, обновление невозможно',
|
||||||
|
)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
@router.delete('/{submenu_id}')
|
@router.delete(
|
||||||
|
'/{submenu_id}',
|
||||||
|
)
|
||||||
async def delete_submenu(
|
async def delete_submenu(
|
||||||
menu_id: UUID,
|
menu_id: UUID,
|
||||||
submenu_id: UUID,
|
submenu_id: UUID,
|
||||||
submenu: SubmenuService = Depends(),
|
submenu: SubmenuService = Depends(),
|
||||||
background_tasks: BackgroundTasks = BackgroundTasks(),
|
background_tasks: BackgroundTasks = BackgroundTasks(),
|
||||||
):
|
) -> None:
|
||||||
await submenu.del_menu(menu_id=menu_id, submenu_id=submenu_id)
|
await submenu.del_menu(menu_id=menu_id, submenu_id=submenu_id)
|
||||||
|
|
|
@ -103,14 +103,19 @@ class DishService:
|
||||||
|
|
||||||
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 | None:
|
||||||
dish_db = Dish_db(**dish_data.model_dump())
|
dish_db = 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_db
|
menu_id, submenu_id, dish_id, dish_db
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if data is None:
|
||||||
|
return None
|
||||||
|
|
||||||
dish = data.__dict__
|
dish = data.__dict__
|
||||||
dish['price'] = str(dish['price'])
|
dish['price'] = str(dish['price'])
|
||||||
dish = Dish(**dish)
|
dish = Dish(**dish)
|
||||||
|
|
||||||
await self.cache.set(
|
await self.cache.set(
|
||||||
self.key(
|
self.key(
|
||||||
'dish',
|
'dish',
|
||||||
|
@ -125,13 +130,11 @@ class DishService:
|
||||||
|
|
||||||
return dish
|
return dish
|
||||||
|
|
||||||
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) -> None:
|
||||||
response = await self.dish_repo.delete_dish_item(
|
await self.dish_repo.delete_dish_item(
|
||||||
menu_id,
|
menu_id,
|
||||||
submenu_id,
|
submenu_id,
|
||||||
dish_id,
|
dish_id,
|
||||||
)
|
)
|
||||||
await self.cache.delete(key=str(menu_id), bg_task=self.bg_tasks)
|
await self.cache.delete(key=str(menu_id), bg_task=self.bg_tasks)
|
||||||
await self.cache.invalidate(key=str(menu_id), bg_task=self.bg_tasks)
|
await self.cache.invalidate(key=str(menu_id), bg_task=self.bg_tasks)
|
||||||
|
|
||||||
return response
|
|
||||||
|
|
|
@ -87,8 +87,10 @@ class MenuService:
|
||||||
)
|
)
|
||||||
return menu
|
return menu
|
||||||
|
|
||||||
async def update_menu(self, menu_id: UUID, menu_data) -> MenuRead:
|
async def update_menu(self, menu_id: UUID, menu_data) -> MenuRead | None:
|
||||||
data = await self.menu_repo.update_menu_item(menu_id, menu_data)
|
data = await self.menu_repo.update_menu_item(menu_id, menu_data)
|
||||||
|
if data is None:
|
||||||
|
return None
|
||||||
menu = data.__dict__
|
menu = data.__dict__
|
||||||
menu = {k: v for k, v in menu.items() if not k.startswith('_')}
|
menu = {k: v for k, v in menu.items() if not k.startswith('_')}
|
||||||
dishes_conter = 0
|
dishes_conter = 0
|
||||||
|
@ -104,8 +106,7 @@ class MenuService:
|
||||||
await self.cache.invalidate(key=str(menu_id), bg_task=self.bg_tasks)
|
await self.cache.invalidate(key=str(menu_id), bg_task=self.bg_tasks)
|
||||||
return menu
|
return menu
|
||||||
|
|
||||||
async def del_menu(self, menu_id: UUID):
|
async def del_menu(self, menu_id: UUID) -> None:
|
||||||
data = await self.menu_repo.delete_menu_item(menu_id)
|
await self.menu_repo.delete_menu_item(menu_id)
|
||||||
await self.cache.delete(key=str(menu_id), bg_task=self.bg_tasks)
|
await self.cache.delete(key=str(menu_id), bg_task=self.bg_tasks)
|
||||||
await self.cache.invalidate(key=str(menu_id), bg_task=self.bg_tasks)
|
await self.cache.invalidate(key=str(menu_id), bg_task=self.bg_tasks)
|
||||||
return data
|
|
||||||
|
|
|
@ -89,14 +89,18 @@ class SubmenuService:
|
||||||
|
|
||||||
async def update_submenu(
|
async def update_submenu(
|
||||||
self, menu_id: UUID, submenu_id: UUID, submenu_data: MenuBase
|
self, menu_id: UUID, submenu_id: UUID, submenu_data: MenuBase
|
||||||
) -> SubMenuRead:
|
) -> SubMenuRead | None:
|
||||||
data = await self.submenu_repo.update_submenu_item(
|
data = await self.submenu_repo.update_submenu_item(
|
||||||
menu_id, submenu_id, submenu_data
|
menu_id, submenu_id, submenu_data
|
||||||
)
|
)
|
||||||
|
if data is None:
|
||||||
|
return None
|
||||||
|
|
||||||
submenu = data.__dict__
|
submenu = data.__dict__
|
||||||
submenu = {k: v for k, v in submenu.items() if not k.startswith('_')}
|
submenu = {k: v for k, v in submenu.items() if not k.startswith('_')}
|
||||||
submenu['dishes_count'] = len(submenu.pop('dishes'))
|
submenu['dishes_count'] = len(submenu.pop('dishes'))
|
||||||
submenu = SubMenuRead(**submenu)
|
submenu = SubMenuRead(**submenu)
|
||||||
|
|
||||||
await self.cache.set(
|
await self.cache.set(
|
||||||
self.key('submenu', menu_id=str(menu_id), submenu_id=str(submenu_id)),
|
self.key('submenu', menu_id=str(menu_id), submenu_id=str(submenu_id)),
|
||||||
submenu,
|
submenu,
|
||||||
|
@ -106,8 +110,8 @@ class SubmenuService:
|
||||||
|
|
||||||
return submenu
|
return submenu
|
||||||
|
|
||||||
async def del_menu(self, menu_id: UUID, submenu_id: UUID) -> int:
|
async def del_menu(self, menu_id: UUID, submenu_id: UUID) -> None:
|
||||||
code = await self.submenu_repo.delete_submenu_item(menu_id, submenu_id)
|
await self.submenu_repo.delete_submenu_item(menu_id, submenu_id)
|
||||||
await self.cache.delete(
|
await self.cache.delete(
|
||||||
key=self.key(
|
key=self.key(
|
||||||
'submenu',
|
'submenu',
|
||||||
|
@ -117,4 +121,3 @@ class SubmenuService:
|
||||||
bg_task=self.bg_tasks,
|
bg_task=self.bg_tasks,
|
||||||
)
|
)
|
||||||
await self.cache.invalidate(key=str(menu_id), bg_task=self.bg_tasks)
|
await self.cache.invalidate(key=str(menu_id), bg_task=self.bg_tasks)
|
||||||
return code
|
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -52,6 +52,6 @@ async def client(event_loop) -> AsyncGenerator[AsyncClient, None]:
|
||||||
|
|
||||||
async with AsyncClient(
|
async with AsyncClient(
|
||||||
app=app,
|
app=app,
|
||||||
base_url='http://localhost:8000/api/v1/menus',
|
base_url='http://localhost:8000',
|
||||||
) as async_client:
|
) as async_client:
|
||||||
yield async_client
|
yield async_client
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
from httpx import AsyncClient, Response
|
from httpx import AsyncClient, Response
|
||||||
|
|
||||||
from .urls import reverse_url
|
from .urls import reverse
|
||||||
|
|
||||||
|
|
||||||
class Repository:
|
class Repository:
|
||||||
|
@ -9,28 +9,28 @@ class Repository:
|
||||||
async def read_all(ac: AsyncClient) -> tuple[int, dict]:
|
async def read_all(ac: AsyncClient) -> tuple[int, dict]:
|
||||||
"""чтение всех меню"""
|
"""чтение всех меню"""
|
||||||
|
|
||||||
response: Response = await ac.get(reverse_url('menus'))
|
response: Response = await ac.get(reverse('get_menus'))
|
||||||
return response.status_code, response.json()
|
return response.status_code, response.json()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def get(ac: AsyncClient, data: dict) -> tuple[int, dict]:
|
async def get(ac: AsyncClient, data: dict) -> tuple[int, dict]:
|
||||||
"""Получение меню по id"""
|
"""Получение меню по id"""
|
||||||
response: Response = await ac.get(
|
response: Response = await ac.get(
|
||||||
reverse_url('menu', menu_id=data.get('id'))
|
reverse('get_menu', menu_id=data.get('id'))
|
||||||
)
|
)
|
||||||
return response.status_code, response.json()
|
return response.status_code, response.json()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def write(ac: AsyncClient, data: dict) -> tuple[int, dict]:
|
async def write(ac: AsyncClient, data: dict) -> tuple[int, dict]:
|
||||||
"""создания меню"""
|
"""создания меню"""
|
||||||
response: Response = await ac.post(reverse_url('menus'), json=data)
|
response: Response = await ac.post(reverse('add_menu'), json=data)
|
||||||
return response.status_code, response.json()
|
return response.status_code, response.json()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def update(ac: AsyncClient, data: dict) -> tuple[int, dict]:
|
async def update(ac: AsyncClient, data: dict) -> tuple[int, dict]:
|
||||||
"""Обновление меню по id"""
|
"""Обновление меню по id"""
|
||||||
response: Response = await ac.patch(
|
response: Response = await ac.patch(
|
||||||
reverse_url('menu', menu_id=data.get('id')),
|
reverse('update_menu', menu_id=data.get('id')),
|
||||||
json=data,
|
json=data,
|
||||||
)
|
)
|
||||||
return response.status_code, response.json()
|
return response.status_code, response.json()
|
||||||
|
@ -39,7 +39,7 @@ class Repository:
|
||||||
async def delete(ac: AsyncClient, data: dict) -> int:
|
async def delete(ac: AsyncClient, data: dict) -> int:
|
||||||
"""Удаление меню по id"""
|
"""Удаление меню по id"""
|
||||||
response: Response = await ac.delete(
|
response: Response = await ac.delete(
|
||||||
reverse_url('menu', menu_id=data.get('id')),
|
reverse('delete_menu', menu_id=data.get('id')),
|
||||||
)
|
)
|
||||||
return response.status_code
|
return response.status_code
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ class Repository:
|
||||||
async def read_all(ac: AsyncClient, menu: dict) -> tuple[int, dict]:
|
async def read_all(ac: AsyncClient, menu: dict) -> tuple[int, dict]:
|
||||||
"""чтение всех меню"""
|
"""чтение всех меню"""
|
||||||
response: Response = await ac.get(
|
response: Response = await ac.get(
|
||||||
reverse_url('submenus', menu_id=menu.get('id')),
|
reverse('get_submenus', menu_id=menu.get('id')),
|
||||||
)
|
)
|
||||||
return response.status_code, response.json()
|
return response.status_code, response.json()
|
||||||
|
|
||||||
|
@ -60,8 +60,8 @@ class Repository:
|
||||||
) -> tuple[int, dict]:
|
) -> tuple[int, dict]:
|
||||||
"""Получение меню по id"""
|
"""Получение меню по id"""
|
||||||
response: Response = await ac.get(
|
response: Response = await ac.get(
|
||||||
reverse_url(
|
reverse(
|
||||||
'submenu',
|
'get_submenu',
|
||||||
menu_id=menu.get('id'),
|
menu_id=menu.get('id'),
|
||||||
submenu_id=submenu.get('id'),
|
submenu_id=submenu.get('id'),
|
||||||
),
|
),
|
||||||
|
@ -76,7 +76,7 @@ class Repository:
|
||||||
) -> tuple[int, dict]:
|
) -> tuple[int, dict]:
|
||||||
"""создания меню"""
|
"""создания меню"""
|
||||||
response: Response = await ac.post(
|
response: Response = await ac.post(
|
||||||
reverse_url('submenu', menu_id=menu.get('id')),
|
reverse('create_submenu_item', menu_id=menu.get('id')),
|
||||||
json=submenu,
|
json=submenu,
|
||||||
)
|
)
|
||||||
return response.status_code, response.json()
|
return response.status_code, response.json()
|
||||||
|
@ -87,8 +87,8 @@ class Repository:
|
||||||
) -> tuple[int, dict]:
|
) -> tuple[int, dict]:
|
||||||
"""Обновление меню по id"""
|
"""Обновление меню по id"""
|
||||||
response: Response = await ac.patch(
|
response: Response = await ac.patch(
|
||||||
reverse_url(
|
reverse(
|
||||||
'submenu',
|
'update_submenu',
|
||||||
menu_id=menu.get('id'),
|
menu_id=menu.get('id'),
|
||||||
submenu_id=submenu.get('id'),
|
submenu_id=submenu.get('id'),
|
||||||
),
|
),
|
||||||
|
@ -100,8 +100,8 @@ class Repository:
|
||||||
async def delete(ac: AsyncClient, menu: dict, submenu: dict) -> int:
|
async def delete(ac: AsyncClient, menu: dict, submenu: dict) -> int:
|
||||||
"""Удаление меню по id"""
|
"""Удаление меню по id"""
|
||||||
response: Response = await ac.delete(
|
response: Response = await ac.delete(
|
||||||
reverse_url(
|
reverse(
|
||||||
'submenu',
|
'delete_submenu',
|
||||||
menu_id=menu.get('id'),
|
menu_id=menu.get('id'),
|
||||||
submenu_id=submenu.get('id'),
|
submenu_id=submenu.get('id'),
|
||||||
),
|
),
|
||||||
|
@ -115,8 +115,8 @@ class Repository:
|
||||||
) -> tuple[int, dict]:
|
) -> tuple[int, dict]:
|
||||||
"""чтение всех блюд"""
|
"""чтение всех блюд"""
|
||||||
response: Response = await ac.get(
|
response: Response = await ac.get(
|
||||||
reverse_url(
|
reverse(
|
||||||
'dishes',
|
'get_dishes',
|
||||||
menu_id=menu.get('id'),
|
menu_id=menu.get('id'),
|
||||||
submenu_id=submenu.get('id'),
|
submenu_id=submenu.get('id'),
|
||||||
),
|
),
|
||||||
|
@ -129,8 +129,8 @@ class Repository:
|
||||||
) -> tuple[int, dict]:
|
) -> tuple[int, dict]:
|
||||||
"""Получение блюда по id"""
|
"""Получение блюда по id"""
|
||||||
response: Response = await ac.get(
|
response: Response = await ac.get(
|
||||||
reverse_url(
|
reverse(
|
||||||
'dish',
|
'get_dish',
|
||||||
menu_id=menu.get('id'),
|
menu_id=menu.get('id'),
|
||||||
submenu_id=submenu.get('id'),
|
submenu_id=submenu.get('id'),
|
||||||
dish_id=dish.get('id'),
|
dish_id=dish.get('id'),
|
||||||
|
@ -144,8 +144,8 @@ class Repository:
|
||||||
) -> tuple[int, dict]:
|
) -> tuple[int, dict]:
|
||||||
"""создания блюда"""
|
"""создания блюда"""
|
||||||
response: Response = await ac.post(
|
response: Response = await ac.post(
|
||||||
reverse_url(
|
reverse(
|
||||||
'dishes',
|
'create_dish',
|
||||||
menu_id=menu.get('id'),
|
menu_id=menu.get('id'),
|
||||||
submenu_id=submenu.get('id'),
|
submenu_id=submenu.get('id'),
|
||||||
),
|
),
|
||||||
|
@ -159,8 +159,8 @@ class Repository:
|
||||||
) -> tuple[int, dict]:
|
) -> tuple[int, dict]:
|
||||||
"""Обновление блюда по id"""
|
"""Обновление блюда по id"""
|
||||||
response: Response = await ac.patch(
|
response: Response = await ac.patch(
|
||||||
reverse_url(
|
reverse(
|
||||||
'dish',
|
'update_dish',
|
||||||
menu_id=menu.get('id'),
|
menu_id=menu.get('id'),
|
||||||
submenu_id=submenu.get('id'),
|
submenu_id=submenu.get('id'),
|
||||||
dish_id=dish.get('id'),
|
dish_id=dish.get('id'),
|
||||||
|
@ -178,8 +178,8 @@ class Repository:
|
||||||
) -> int:
|
) -> int:
|
||||||
"""Удаление блюда по id"""
|
"""Удаление блюда по id"""
|
||||||
response: Response = await ac.delete(
|
response: Response = await ac.delete(
|
||||||
reverse_url(
|
reverse(
|
||||||
'dish',
|
'delete_dish',
|
||||||
menu_id=menu.get('id'),
|
menu_id=menu.get('id'),
|
||||||
submenu_id=submenu.get('id'),
|
submenu_id=submenu.get('id'),
|
||||||
dish_id=dish.get('id'),
|
dish_id=dish.get('id'),
|
||||||
|
|
|
@ -1,25 +1,9 @@
|
||||||
def reverse_url(loc: str, **kwargs) -> str:
|
from fastfood.app import create_app
|
||||||
menu_pref = '/'
|
|
||||||
submenu_pref = menu_pref + str(kwargs.get('menu_id', '')) + '/submenus/'
|
|
||||||
dish_pref = submenu_pref + str(kwargs.get('submenu_id', '')) + '/dishes/'
|
|
||||||
|
|
||||||
match loc:
|
app = create_app()
|
||||||
case 'menus':
|
|
||||||
return menu_pref
|
|
||||||
|
|
||||||
case 'menu':
|
|
||||||
return menu_pref + str(kwargs.get('menu_id', ''))
|
|
||||||
|
|
||||||
case 'submenus':
|
def reverse(loc: str, **kwargs) -> str:
|
||||||
return submenu_pref
|
|
||||||
|
|
||||||
case 'submenu':
|
url = app.url_path_for(loc, **kwargs)
|
||||||
return submenu_pref + str(kwargs.get('submenu_id', ''))
|
return url
|
||||||
|
|
||||||
case 'dishes':
|
|
||||||
return dish_pref
|
|
||||||
|
|
||||||
case 'dish':
|
|
||||||
return dish_pref + str(kwargs.get('dish_id', ''))
|
|
||||||
|
|
||||||
return menu_pref
|
|
||||||
|
|
Loading…
Reference in New Issue