diff --git a/fastfood/app.py b/fastfood/app.py index 55081a7..d6c8852 100644 --- a/fastfood/app.py +++ b/fastfood/app.py @@ -1,9 +1,8 @@ from fastapi import FastAPI +from fastfood.routers.dish import router as dish_router from fastfood.routers.menu import router as menu_router from fastfood.routers.submenu import router as submenu_router -from fastfood.routers.dish import router as dish_router - description = """ # 🔥🔥🔥Fastfood-API поможет тебе подкрепиться 🔥🔥🔥 @@ -58,10 +57,7 @@ tags_metadata = [ "name": "submenu", "description": "Подменю и работа с ним", }, - { - "name": "dish", - "description": "Блюда и работа с ними" - }, + {"name": "dish", "description": "Блюда и работа с ними"}, ] diff --git a/fastfood/cruds/__init__.py b/fastfood/cruds/__init__.py index 1af1f3e..155d4be 100644 --- a/fastfood/cruds/__init__.py +++ b/fastfood/cruds/__init__.py @@ -1,8 +1,9 @@ from fastfood import models from fastfood.dbase import async_engine + +from .dish import DishCrud from .menu import MenuCrud from .submenu import SubMenuCrud -from .dish import DishCrud async def create_db_and_tables(): diff --git a/fastfood/cruds/dish.py b/fastfood/cruds/dish.py index 38cf973..18cb8c4 100644 --- a/fastfood/cruds/dish.py +++ b/fastfood/cruds/dish.py @@ -1,4 +1,5 @@ from uuid import UUID + from sqlalchemy import delete, select, update from sqlalchemy.ext.asyncio import AsyncSession @@ -15,7 +16,9 @@ class DishCrud: @staticmethod async def create_dish_item( - submenu_id: UUID, dish: schemas.DishBase, session: AsyncSession, + submenu_id: UUID, + dish: schemas.DishBase, + session: AsyncSession, ): async with session: new_dish = models.Dish(**dish.model_dump()) @@ -27,7 +30,8 @@ class DishCrud: @staticmethod async def get_dish_item( - dish_id: UUID, session: AsyncSession, + dish_id: UUID, + session: AsyncSession, ): async with session: query = select(models.Dish).where(models.Dish.id == dish_id) @@ -41,7 +45,11 @@ class DishCrud: session: AsyncSession, ): async with session: - query = update(models.Dish).where(models.Dish.id == dish_id).values(**dish.model_dump()) + query = ( + update(models.Dish) + .where(models.Dish.id == dish_id) + .values(**dish.model_dump()) + ) await session.execute(query) await session.commit() qr = select(models.Dish).where(models.Dish.id == dish_id) @@ -54,5 +62,3 @@ class DishCrud: query = delete(models.Dish).where(models.Dish.id == dish_id) await session.execute(query) await session.commit() - - diff --git a/fastfood/cruds/menu.py b/fastfood/cruds/menu.py index 3770c2a..55561df 100644 --- a/fastfood/cruds/menu.py +++ b/fastfood/cruds/menu.py @@ -1,4 +1,5 @@ from uuid import UUID + from sqlalchemy import delete, func, select, update from sqlalchemy.ext.asyncio import AsyncSession @@ -30,7 +31,9 @@ class MenuCrud: menu = menu.scalars().one_or_none() if menu is None: return None - submenu_query = select(func.count(models.SubMenu.id).label("counter")).filter(models.SubMenu.parent_menu == menu_id) + submenu_query = select( + func.count(models.SubMenu.id).label("counter") + ).filter(models.SubMenu.parent_menu == menu_id) counter = await session.execute(submenu_query) dish_query = ( @@ -45,12 +48,17 @@ class MenuCrud: return menu @staticmethod - async def update_menu_item(menu_id: UUID, - menu: schemas.MenuBase, - session: AsyncSession, - ): + async def update_menu_item( + menu_id: UUID, + menu: schemas.MenuBase, + session: AsyncSession, + ): async with session: - query = update(models.Menu).where(models.Menu.id == menu_id).values(**menu.model_dump()) + query = ( + update(models.Menu) + .where(models.Menu.id == menu_id) + .values(**menu.model_dump()) + ) await session.execute(query) await session.commit() qr = select(models.Menu).where(models.Menu.id == menu_id) @@ -63,5 +71,3 @@ class MenuCrud: query = delete(models.Menu).where(models.Menu.id == menu_id) await session.execute(query) await session.commit() - - diff --git a/fastfood/cruds/submenu.py b/fastfood/cruds/submenu.py index 9bdd3cd..4e6a4ee 100644 --- a/fastfood/cruds/submenu.py +++ b/fastfood/cruds/submenu.py @@ -1,4 +1,5 @@ from uuid import UUID + from sqlalchemy import delete, func, select, update from sqlalchemy.ext.asyncio import AsyncSession @@ -15,7 +16,9 @@ class SubMenuCrud: @staticmethod async def create_submenu_item( - menu_id: UUID, submenu: schemas.MenuBase, session: AsyncSession, + menu_id: UUID, + submenu: schemas.MenuBase, + session: AsyncSession, ): async with session: new_submenu = models.SubMenu(**submenu.model_dump()) @@ -27,7 +30,9 @@ class SubMenuCrud: @staticmethod async def get_submenu_item( - menu_id: UUID, submenu_id: UUID, session: AsyncSession, + menu_id: UUID, + submenu_id: UUID, + session: AsyncSession, ): async with session: query = select(models.SubMenu).where(models.SubMenu.id == submenu_id) @@ -53,7 +58,11 @@ class SubMenuCrud: session: AsyncSession, ): async with session: - query = update(models.SubMenu).where(models.SubMenu.id == submenu_id).values(**submenu.model_dump()) + query = ( + update(models.SubMenu) + .where(models.SubMenu.id == submenu_id) + .values(**submenu.model_dump()) + ) await session.execute(query) await session.commit() qr = select(models.SubMenu).where(models.SubMenu.id == submenu_id) diff --git a/fastfood/dbase.py b/fastfood/dbase.py index 143268e..eb49cca 100644 --- a/fastfood/dbase.py +++ b/fastfood/dbase.py @@ -1,14 +1,10 @@ from typing import AsyncGenerator -from sqlalchemy.ext.asyncio import ( - AsyncSession, - async_sessionmaker, - create_async_engine, -) +from sqlalchemy.ext.asyncio import (AsyncSession, async_sessionmaker, + create_async_engine) from fastfood.config import settings - async_engine = create_async_engine(settings.DATABASE_URL_asyncpg) async_session_maker = async_sessionmaker( async_engine, diff --git a/fastfood/models.py b/fastfood/models.py index 2c442af..3c42ee6 100644 --- a/fastfood/models.py +++ b/fastfood/models.py @@ -26,16 +26,24 @@ class Menu(Base): __tablename__ = "menu" submenus: Mapped[List["SubMenu"]] = relationship( - "SubMenu", backref="menu", lazy='dynamic', cascade="all, delete", + "SubMenu", + backref="menu", + lazy="dynamic", + cascade="all, delete", ) class SubMenu(Base): __tablename__ = "submenu" - parent_menu: Mapped[uuid.UUID] = mapped_column(ForeignKey("menu.id", ondelete="CASCADE")) + parent_menu: Mapped[uuid.UUID] = mapped_column( + ForeignKey("menu.id", ondelete="CASCADE") + ) dishes: Mapped[List["Dish"]] = relationship( - "Dish", backref="submenu", lazy="dynamic", cascade="all, delete", + "Dish", + backref="submenu", + lazy="dynamic", + cascade="all, delete", ) @@ -43,4 +51,6 @@ class Dish(Base): __tablename__ = "dish" price: Mapped[float] - parent_submenu: Mapped[uuid.UUID] = mapped_column(ForeignKey("submenu.id", ondelete="CASCADE")) + parent_submenu: Mapped[uuid.UUID] = mapped_column( + ForeignKey("submenu.id", ondelete="CASCADE") + ) diff --git a/fastfood/routers/dish.py b/fastfood/routers/dish.py index c034c9c..9007cef 100644 --- a/fastfood/routers/dish.py +++ b/fastfood/routers/dish.py @@ -1,13 +1,13 @@ from typing import List, Optional -from fastapi import APIRouter, Depends, HTTPException from uuid import UUID + +from fastapi import APIRouter, Depends, HTTPException from sqlalchemy.ext.asyncio import AsyncSession from fastfood import schemas -from fastfood.utils import price_converter from fastfood.cruds import crud from fastfood.dbase import get_async_session - +from fastfood.utils import price_converter router = APIRouter( prefix="/api/v1/menus/{menu_id}/submenus/{submenu_id}/dishes", @@ -17,9 +17,7 @@ router = APIRouter( @router.get("/") async def get_dishes( - menu_id: UUID, - submenu_id: UUID, - session: AsyncSession = Depends(get_async_session) + menu_id: UUID, submenu_id: UUID, session: AsyncSession = Depends(get_async_session) ): result = await crud.get_dishes(submenu_id=submenu_id, session=session) return result @@ -48,16 +46,15 @@ async def get_dish( session: AsyncSession = Depends(get_async_session), ): result = await crud.get_dish_item( - dish_id=dish_id, session=session, + dish_id=dish_id, + session=session, ) if not result: raise HTTPException(status_code=404, detail="dish not found") return price_converter(result) -@router.patch( - "/{dish_id}" -) +@router.patch("/{dish_id}") async def update_dish( menu_id: UUID, submenu_id: UUID, @@ -66,11 +63,18 @@ async def update_dish( session: AsyncSession = Depends(get_async_session), ): result = await crud.update_dish_item( - dish_id=dish_id, dish=dish, session=session, + dish_id=dish_id, + dish=dish, + session=session, ) return price_converter(result) @router.delete("/{dish_id}") -async def delete_dish(menu_id: UUID, submenu_id: UUID, dish_id: UUID, session: AsyncSession = Depends(get_async_session)): +async def delete_dish( + menu_id: UUID, + submenu_id: UUID, + dish_id: UUID, + session: AsyncSession = Depends(get_async_session), +): await crud.delete_dish_item(dish_id=dish_id, session=session) diff --git a/fastfood/routers/menu.py b/fastfood/routers/menu.py index 8ccfec9..df5186a 100644 --- a/fastfood/routers/menu.py +++ b/fastfood/routers/menu.py @@ -1,13 +1,13 @@ from typing import List, Optional -from fastapi import APIRouter, Depends, HTTPException from uuid import UUID + +from fastapi import APIRouter, Depends, HTTPException from sqlalchemy.ext.asyncio import AsyncSession from fastfood import schemas from fastfood.cruds import crud from fastfood.dbase import get_async_session - router = APIRouter( prefix="/api/v1/menus", tags=["menu"], @@ -34,8 +34,8 @@ async def add_menu( @router.get("/{menu_id}", response_model=schemas.MenuRead) async def get_menu( - menu_id: UUID, - session: AsyncSession = Depends(get_async_session), + menu_id: UUID, + session: AsyncSession = Depends(get_async_session), ): result = await crud.get_menu_item(menu_id=menu_id, session=session) if not result: @@ -50,15 +50,16 @@ async def update_menu( session: AsyncSession = Depends(get_async_session), ): result = await crud.update_menu_item( - menu_id=menu_id, menu=menu, session=session, + menu_id=menu_id, + menu=menu, + session=session, ) return result @router.delete("/{menu_id}") async def delete_menu( - menu_id: UUID, session: AsyncSession = Depends(get_async_session), + menu_id: UUID, + session: AsyncSession = Depends(get_async_session), ): await crud.delete_menu_item(menu_id=menu_id, session=session) - - diff --git a/fastfood/routers/submenu.py b/fastfood/routers/submenu.py index 1afe313..80fb205 100644 --- a/fastfood/routers/submenu.py +++ b/fastfood/routers/submenu.py @@ -1,12 +1,12 @@ -from fastapi import APIRouter, Depends, HTTPException from uuid import UUID + +from fastapi import APIRouter, Depends, HTTPException from sqlalchemy.ext.asyncio import AsyncSession from fastfood import schemas from fastfood.cruds import crud from fastfood.dbase import get_async_session - router = APIRouter( prefix="/api/v1/menus/{menu_id}/submenus", tags=["submenu"], @@ -15,8 +15,7 @@ router = APIRouter( @router.get("/") async def get_submenus( - menu_id: UUID, - session: AsyncSession = Depends(get_async_session) + menu_id: UUID, session: AsyncSession = Depends(get_async_session) ): result = await crud.get_submenus(menu_id=menu_id, session=session) return result @@ -43,7 +42,9 @@ async def get_submenu( session: AsyncSession = Depends(get_async_session), ): result = await crud.get_submenu_item( - menu_id=menu_id, submenu_id=submenu_id, session=session, + menu_id=menu_id, + submenu_id=submenu_id, + session=session, ) if not result: raise HTTPException(status_code=404, detail="submenu not found") @@ -61,13 +62,15 @@ async def update_submenu( session: AsyncSession = Depends(get_async_session), ): result = await crud.update_submenu_item( - submenu_id=submenu_id, submenu=submenu, session=session, + submenu_id=submenu_id, + submenu=submenu, + session=session, ) return result @router.delete("/{submenu_id}") -async def delete_submenu(menu_id: UUID, submenu_id: UUID, session: AsyncSession = Depends(get_async_session)): +async def delete_submenu( + menu_id: UUID, submenu_id: UUID, session: AsyncSession = Depends(get_async_session) +): await crud.delete_submenu_item(submenu_id=submenu_id, session=session) - - diff --git a/fastfood/utils.py b/fastfood/utils.py index 8cb26a0..aa1205f 100644 --- a/fastfood/utils.py +++ b/fastfood/utils.py @@ -1,4 +1,3 @@ - def price_converter(dish: dict) -> dict: dish.price = str(dish.price) return dish