From 2104f39e3b40f1dd5bf4f52b81fae048c5053761 Mon Sep 17 00:00:00 2001 From: Sergey Vanyushkin Date: Wed, 4 Sep 2024 12:16:42 +0000 Subject: [PATCH] ADD add_menu usecase, fix data_mapper --- src/fastfood_two/infrastructure/menu_gateway.py | 15 +++++++++++++-- .../pg_storage/mappers/menu_mapper.py | 16 ++++++---------- .../presentation/fastapi_backend/dependencies.py | 3 +++ 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/fastfood_two/infrastructure/menu_gateway.py b/src/fastfood_two/infrastructure/menu_gateway.py index 649a009..fb922e0 100644 --- a/src/fastfood_two/infrastructure/menu_gateway.py +++ b/src/fastfood_two/infrastructure/menu_gateway.py @@ -16,11 +16,22 @@ class MenuGatewayImpl: async def get_all_menus(self) -> list[Menu]: query = text("SELECT * FROM menu;") menus = await self._session.execute(query) - return [db_entity_to_domain(menu) for menu in menus.scalars().all()] + return [db_entity_to_domain(menu) for menu in menus] async def get_menu_by_id(self, id: UUID) -> Menu | 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);") + await self._session.execute( + query, + { + "id": menu.id.value, + "title": menu.title.value, + "description": menu.description.value, + }, + ) + await self._session.commit() + return menu async def update_menu(self, updated_menu: Menu) -> Menu | None: ... diff --git a/src/fastfood_two/infrastructure/pg_storage/mappers/menu_mapper.py b/src/fastfood_two/infrastructure/pg_storage/mappers/menu_mapper.py index 32d1cd7..d1fbbf7 100644 --- a/src/fastfood_two/infrastructure/pg_storage/mappers/menu_mapper.py +++ b/src/fastfood_two/infrastructure/pg_storage/mappers/menu_mapper.py @@ -1,14 +1,10 @@ -from typing import TYPE_CHECKING - -from fastfood_two.domain.menu.menu_entity import Menu - -if TYPE_CHECKING: - from fastfood_two.infrastructure.pg_storage.models import SQLAMenu +from fastfood_two.domain.menu.menu_entity import Description, Menu, MenuId, Title -def db_entity_to_domain(menu: "SQLAMenu") -> Menu: +def db_entity_to_domain(menu) -> Menu: + print(menu) return Menu( - id=menu.id, - title=menu.title, - description=menu.description, + id=MenuId(menu[0]), + title=Title(menu[1]), + description=Description(menu[2]), ) diff --git a/src/fastfood_two/presentation/fastapi_backend/dependencies.py b/src/fastfood_two/presentation/fastapi_backend/dependencies.py index 2c0f6aa..d2b0b47 100644 --- a/src/fastfood_two/presentation/fastapi_backend/dependencies.py +++ b/src/fastfood_two/presentation/fastapi_backend/dependencies.py @@ -5,6 +5,7 @@ from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, async_sessionmaker from fastfood_two.application.common.logger import configure_logger from fastfood_two.application.config import Config +from fastfood_two.application.usecases.menu.add_menu import AddMenu from fastfood_two.application.usecases.menu.get_all_menus import GetAllMenus from fastfood_two.domain.menu.gateway import MenuGateway from fastfood_two.presentation.fastapi_backend.depends.config import get_settings @@ -15,6 +16,7 @@ from fastfood_two.presentation.fastapi_backend.depends.session import ( get_session, ) from fastfood_two.presentation.fastapi_backend.depends.usecases import ( + add_menu_usecase, get_all_menus_usecase, ) @@ -38,5 +40,6 @@ def init_dependencies(app: FastAPI) -> None: app.dependency_overrides[MenuGateway] = get_menu_gateway app.dependency_overrides[GetAllMenus] = get_all_menus_usecase + app.dependency_overrides[AddMenu] = add_menu_usecase logger.info("Dependencies initialized")