ADD add_menu usecase, fix data_mapper

main
Сергей Ванюшкин 2024-09-04 12:16:42 +00:00
parent fad06c45f8
commit 2104f39e3b
3 changed files with 22 additions and 12 deletions

View File

@ -16,11 +16,22 @@ class MenuGatewayImpl:
async def get_all_menus(self) -> list[Menu]: async def get_all_menus(self) -> list[Menu]:
query = text("SELECT * FROM menu;") query = text("SELECT * FROM menu;")
menus = await self._session.execute(query) 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 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: ... async def update_menu(self, updated_menu: Menu) -> Menu | None: ...

View File

@ -1,14 +1,10 @@
from typing import TYPE_CHECKING from fastfood_two.domain.menu.menu_entity import Description, Menu, MenuId, Title
from fastfood_two.domain.menu.menu_entity import Menu
if TYPE_CHECKING:
from fastfood_two.infrastructure.pg_storage.models import SQLAMenu
def db_entity_to_domain(menu: "SQLAMenu") -> Menu: def db_entity_to_domain(menu) -> Menu:
print(menu)
return Menu( return Menu(
id=menu.id, id=MenuId(menu[0]),
title=menu.title, title=Title(menu[1]),
description=menu.description, description=Description(menu[2]),
) )

View File

@ -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.common.logger import configure_logger
from fastfood_two.application.config import Config 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.application.usecases.menu.get_all_menus import GetAllMenus
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
@ -15,6 +16,7 @@ from fastfood_two.presentation.fastapi_backend.depends.session import (
get_session, get_session,
) )
from fastfood_two.presentation.fastapi_backend.depends.usecases import ( from fastfood_two.presentation.fastapi_backend.depends.usecases import (
add_menu_usecase,
get_all_menus_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[MenuGateway] = get_menu_gateway
app.dependency_overrides[GetAllMenus] = get_all_menus_usecase app.dependency_overrides[GetAllMenus] = get_all_menus_usecase
app.dependency_overrides[AddMenu] = add_menu_usecase
logger.info("Dependencies initialized") logger.info("Dependencies initialized")