ADD get menu by id

main
Сергей Ванюшкин 2024-09-08 12:30:35 +00:00
parent 32091d6e85
commit 1afb88d746
6 changed files with 73 additions and 1 deletions

View File

@ -18,3 +18,8 @@ class UpdateMenuDTO:
@dataclass(frozen=True) @dataclass(frozen=True)
class DeleteMenuDTO: class DeleteMenuDTO:
id: UUID id: UUID
@dataclass(frozen=True)
class GetMenuByIdDTO:
id: UUID

View File

@ -0,0 +1,26 @@
from fastfood_two.application.abstractions.interactor import Interactor
from fastfood_two.application.contracts.requests import GetMenuByIdDTO
from fastfood_two.application.contracts.responses import MenuDTO
from fastfood_two.domain.menu.error import MenuNotFoundError
from fastfood_two.domain.menu.gateway import MenuGateway
from fastfood_two.domain.menu.menu_entity import MenuId
class GetMenuById(Interactor[GetMenuByIdDTO, MenuDTO]):
def __init__(self, gateway: MenuGateway) -> None:
self._menu_gateway = gateway
async def __call__(self, request: GetMenuByIdDTO) -> MenuDTO:
menu = await self._menu_gateway.get_menu_by_id(
id=MenuId(request.id),
)
if not menu:
raise MenuNotFoundError(message="Menu not found")
return MenuDTO(
id=menu.id.value,
title=menu.title.value,
description=menu.description.value,
)

View File

@ -16,7 +16,11 @@ class MenuGatewayImpl:
menus = await self._session.execute(query) menus = await self._session.execute(query)
return [db_entity_to_domain(menu.tuple()) for menu in menus] return [db_entity_to_domain(menu.tuple()) for menu in menus]
async def get_menu_by_id(self, id: MenuId) -> Menu | None: ... async def get_menu_by_id(self, id: MenuId) -> Menu | None:
query = text("SELECT * FROM menu WHERE id = :id;")
menu = (await self._session.execute(query, {"id": id.value})).tuples().first()
return db_entity_to_domain(menu) if menu else None
async def insert_menu(self, menu: Menu) -> Menu: async def insert_menu(self, menu: Menu) -> Menu:
query = text("INSERT INTO menu (id, title, description) VALUES (:id, :title, :description);") query = text("INSERT INTO menu (id, title, description) VALUES (:id, :title, :description);")

View File

@ -8,6 +8,7 @@ from fastfood_two.application.config import Config
from fastfood_two.application.usecases.menu.add_menu import AddMenu from fastfood_two.application.usecases.menu.add_menu import AddMenu
from fastfood_two.application.usecases.menu.delete_menu import DeleteMenu from fastfood_two.application.usecases.menu.delete_menu import DeleteMenu
from fastfood_two.application.usecases.menu.get_all_menus import GetAllMenus from fastfood_two.application.usecases.menu.get_all_menus import GetAllMenus
from fastfood_two.application.usecases.menu.get_menu_by_id import GetMenuById
from fastfood_two.application.usecases.menu.update_menu import UpdateMenu from fastfood_two.application.usecases.menu.update_menu import UpdateMenu
from fastfood_two.domain.menu.gateway import MenuGateway from fastfood_two.domain.menu.gateway import MenuGateway
from fastfood_two.presentation.fastapi_backend.depends.config import get_settings from fastfood_two.presentation.fastapi_backend.depends.config import get_settings
@ -21,6 +22,7 @@ from fastfood_two.presentation.fastapi_backend.depends.usecases import (
add_menu_usecase, add_menu_usecase,
delete_menu_usecase, delete_menu_usecase,
get_all_menus_usecase, get_all_menus_usecase,
get_menu_by_id_usecase,
update_menu_usecase, update_menu_usecase,
) )
@ -47,5 +49,6 @@ def init_dependencies(app: FastAPI) -> None:
app.dependency_overrides[AddMenu] = add_menu_usecase app.dependency_overrides[AddMenu] = add_menu_usecase
app.dependency_overrides[UpdateMenu] = update_menu_usecase app.dependency_overrides[UpdateMenu] = update_menu_usecase
app.dependency_overrides[DeleteMenu] = delete_menu_usecase app.dependency_overrides[DeleteMenu] = delete_menu_usecase
app.dependency_overrides[GetMenuById] = get_menu_by_id_usecase
logger.info("Dependencies initialized") logger.info("Dependencies initialized")

View File

@ -5,6 +5,7 @@ from fastapi import Depends
from fastfood_two.application.usecases.menu.add_menu import AddMenu from fastfood_two.application.usecases.menu.add_menu import AddMenu
from fastfood_two.application.usecases.menu.delete_menu import DeleteMenu from fastfood_two.application.usecases.menu.delete_menu import DeleteMenu
from fastfood_two.application.usecases.menu.get_all_menus import GetAllMenus from fastfood_two.application.usecases.menu.get_all_menus import GetAllMenus
from fastfood_two.application.usecases.menu.get_menu_by_id import GetMenuById
from fastfood_two.application.usecases.menu.update_menu import UpdateMenu from fastfood_two.application.usecases.menu.update_menu import UpdateMenu
from fastfood_two.domain.menu.gateway import MenuGateway from fastfood_two.domain.menu.gateway import MenuGateway
from fastfood_two.presentation.fastapi_backend.depends.stub import Stub from fastfood_two.presentation.fastapi_backend.depends.stub import Stub
@ -24,3 +25,7 @@ def update_menu_usecase(gateway: Annotated[MenuGateway, Depends(Stub(MenuGateway
def delete_menu_usecase(gateway: Annotated[MenuGateway, Depends(Stub(MenuGateway))]) -> DeleteMenu: def delete_menu_usecase(gateway: Annotated[MenuGateway, Depends(Stub(MenuGateway))]) -> DeleteMenu:
return DeleteMenu(gateway=gateway) return DeleteMenu(gateway=gateway)
def get_menu_by_id_usecase(gateway: Annotated[MenuGateway, Depends(Stub(MenuGateway))]) -> GetMenuById:
return GetMenuById(gateway=gateway)

View File

@ -6,12 +6,14 @@ from fastapi import APIRouter, Depends
from fastfood_two.application.contracts.requests import ( from fastfood_two.application.contracts.requests import (
AddMenuDTO, AddMenuDTO,
DeleteMenuDTO, DeleteMenuDTO,
GetMenuByIdDTO,
UpdateMenuDTO, UpdateMenuDTO,
) )
from fastfood_two.application.contracts.responses import MenuDTO from fastfood_two.application.contracts.responses import MenuDTO
from fastfood_two.application.usecases.menu.add_menu import AddMenu from fastfood_two.application.usecases.menu.add_menu import AddMenu
from fastfood_two.application.usecases.menu.delete_menu import DeleteMenu from fastfood_two.application.usecases.menu.delete_menu import DeleteMenu
from fastfood_two.application.usecases.menu.get_all_menus import GetAllMenus from fastfood_two.application.usecases.menu.get_all_menus import GetAllMenus
from fastfood_two.application.usecases.menu.get_menu_by_id import GetMenuById
from fastfood_two.application.usecases.menu.update_menu import UpdateMenu from fastfood_two.application.usecases.menu.update_menu import UpdateMenu
from fastfood_two.presentation.fastapi_backend.depends.stub import Stub from fastfood_two.presentation.fastapi_backend.depends.stub import Stub
from fastfood_two.presentation.fastapi_backend.pdmodels.menu import ( from fastfood_two.presentation.fastapi_backend.pdmodels.menu import (
@ -129,3 +131,30 @@ async def delete_menu(
) )
) )
return return
@router.get("/{menu_id}", response_model=MenuDTO)
async def get_menu_by_id(
menu_id: UUID,
usecase: Annotated[GetMenuById, Depends(Stub(GetMenuById))],
) -> MenuDTO:
"""Get menus by id.
Endpoint allows to get food menu item by id
Parameters
----------
menu_id: UUID
requaired id of the menu item
Returns
-------
MenuDTO
menu item
"""
menu = await usecase(
request=GetMenuByIdDTO(
id=menu_id,
)
)
return menu