поправил под pep
parent
ed11efa704
commit
c01affbcb9
|
@ -1,9 +1,8 @@
|
||||||
from fastapi import FastAPI
|
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.menu import router as menu_router
|
||||||
from fastfood.routers.submenu import router as submenu_router
|
from fastfood.routers.submenu import router as submenu_router
|
||||||
from fastfood.routers.dish import router as dish_router
|
|
||||||
|
|
||||||
|
|
||||||
description = """
|
description = """
|
||||||
# 🔥🔥🔥Fastfood-API поможет тебе подкрепиться 🔥🔥🔥
|
# 🔥🔥🔥Fastfood-API поможет тебе подкрепиться 🔥🔥🔥
|
||||||
|
@ -58,10 +57,7 @@ tags_metadata = [
|
||||||
"name": "submenu",
|
"name": "submenu",
|
||||||
"description": "Подменю и работа с ним",
|
"description": "Подменю и работа с ним",
|
||||||
},
|
},
|
||||||
{
|
{"name": "dish", "description": "Блюда и работа с ними"},
|
||||||
"name": "dish",
|
|
||||||
"description": "Блюда и работа с ними"
|
|
||||||
},
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
from fastfood import models
|
from fastfood import models
|
||||||
from fastfood.dbase import async_engine
|
from fastfood.dbase import async_engine
|
||||||
|
|
||||||
|
from .dish import DishCrud
|
||||||
from .menu import MenuCrud
|
from .menu import MenuCrud
|
||||||
from .submenu import SubMenuCrud
|
from .submenu import SubMenuCrud
|
||||||
from .dish import DishCrud
|
|
||||||
|
|
||||||
|
|
||||||
async def create_db_and_tables():
|
async def create_db_and_tables():
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
from sqlalchemy import delete, select, update
|
from sqlalchemy import delete, select, update
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
|
@ -15,7 +16,9 @@ class DishCrud:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def create_dish_item(
|
async def create_dish_item(
|
||||||
submenu_id: UUID, dish: schemas.DishBase, session: AsyncSession,
|
submenu_id: UUID,
|
||||||
|
dish: schemas.DishBase,
|
||||||
|
session: AsyncSession,
|
||||||
):
|
):
|
||||||
async with session:
|
async with session:
|
||||||
new_dish = models.Dish(**dish.model_dump())
|
new_dish = models.Dish(**dish.model_dump())
|
||||||
|
@ -27,7 +30,8 @@ class DishCrud:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def get_dish_item(
|
async def get_dish_item(
|
||||||
dish_id: UUID, session: AsyncSession,
|
dish_id: UUID,
|
||||||
|
session: AsyncSession,
|
||||||
):
|
):
|
||||||
async with session:
|
async with session:
|
||||||
query = select(models.Dish).where(models.Dish.id == dish_id)
|
query = select(models.Dish).where(models.Dish.id == dish_id)
|
||||||
|
@ -41,7 +45,11 @@ class DishCrud:
|
||||||
session: AsyncSession,
|
session: AsyncSession,
|
||||||
):
|
):
|
||||||
async with session:
|
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.execute(query)
|
||||||
await session.commit()
|
await session.commit()
|
||||||
qr = select(models.Dish).where(models.Dish.id == dish_id)
|
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)
|
query = delete(models.Dish).where(models.Dish.id == dish_id)
|
||||||
await session.execute(query)
|
await session.execute(query)
|
||||||
await session.commit()
|
await session.commit()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
from sqlalchemy import delete, func, select, update
|
from sqlalchemy import delete, func, select, update
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
|
@ -30,7 +31,9 @@ class MenuCrud:
|
||||||
menu = menu.scalars().one_or_none()
|
menu = menu.scalars().one_or_none()
|
||||||
if menu is None:
|
if menu is None:
|
||||||
return 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)
|
counter = await session.execute(submenu_query)
|
||||||
|
|
||||||
dish_query = (
|
dish_query = (
|
||||||
|
@ -45,12 +48,17 @@ class MenuCrud:
|
||||||
return menu
|
return menu
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def update_menu_item(menu_id: UUID,
|
async def update_menu_item(
|
||||||
menu: schemas.MenuBase,
|
menu_id: UUID,
|
||||||
session: AsyncSession,
|
menu: schemas.MenuBase,
|
||||||
):
|
session: AsyncSession,
|
||||||
|
):
|
||||||
async with session:
|
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.execute(query)
|
||||||
await session.commit()
|
await session.commit()
|
||||||
qr = select(models.Menu).where(models.Menu.id == menu_id)
|
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)
|
query = delete(models.Menu).where(models.Menu.id == menu_id)
|
||||||
await session.execute(query)
|
await session.execute(query)
|
||||||
await session.commit()
|
await session.commit()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
from sqlalchemy import delete, func, select, update
|
from sqlalchemy import delete, func, select, update
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
|
@ -15,7 +16,9 @@ class SubMenuCrud:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def create_submenu_item(
|
async def create_submenu_item(
|
||||||
menu_id: UUID, submenu: schemas.MenuBase, session: AsyncSession,
|
menu_id: UUID,
|
||||||
|
submenu: schemas.MenuBase,
|
||||||
|
session: AsyncSession,
|
||||||
):
|
):
|
||||||
async with session:
|
async with session:
|
||||||
new_submenu = models.SubMenu(**submenu.model_dump())
|
new_submenu = models.SubMenu(**submenu.model_dump())
|
||||||
|
@ -27,7 +30,9 @@ class SubMenuCrud:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def get_submenu_item(
|
async def get_submenu_item(
|
||||||
menu_id: UUID, submenu_id: UUID, session: AsyncSession,
|
menu_id: UUID,
|
||||||
|
submenu_id: UUID,
|
||||||
|
session: AsyncSession,
|
||||||
):
|
):
|
||||||
async with session:
|
async with session:
|
||||||
query = select(models.SubMenu).where(models.SubMenu.id == submenu_id)
|
query = select(models.SubMenu).where(models.SubMenu.id == submenu_id)
|
||||||
|
@ -53,7 +58,11 @@ class SubMenuCrud:
|
||||||
session: AsyncSession,
|
session: AsyncSession,
|
||||||
):
|
):
|
||||||
async with session:
|
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.execute(query)
|
||||||
await session.commit()
|
await session.commit()
|
||||||
qr = select(models.SubMenu).where(models.SubMenu.id == submenu_id)
|
qr = select(models.SubMenu).where(models.SubMenu.id == submenu_id)
|
||||||
|
|
|
@ -1,14 +1,10 @@
|
||||||
from typing import AsyncGenerator
|
from typing import AsyncGenerator
|
||||||
|
|
||||||
from sqlalchemy.ext.asyncio import (
|
from sqlalchemy.ext.asyncio import (AsyncSession, async_sessionmaker,
|
||||||
AsyncSession,
|
create_async_engine)
|
||||||
async_sessionmaker,
|
|
||||||
create_async_engine,
|
|
||||||
)
|
|
||||||
|
|
||||||
from fastfood.config import settings
|
from fastfood.config import settings
|
||||||
|
|
||||||
|
|
||||||
async_engine = create_async_engine(settings.DATABASE_URL_asyncpg)
|
async_engine = create_async_engine(settings.DATABASE_URL_asyncpg)
|
||||||
async_session_maker = async_sessionmaker(
|
async_session_maker = async_sessionmaker(
|
||||||
async_engine,
|
async_engine,
|
||||||
|
|
|
@ -26,16 +26,24 @@ class Menu(Base):
|
||||||
__tablename__ = "menu"
|
__tablename__ = "menu"
|
||||||
|
|
||||||
submenus: Mapped[List["SubMenu"]] = relationship(
|
submenus: Mapped[List["SubMenu"]] = relationship(
|
||||||
"SubMenu", backref="menu", lazy='dynamic', cascade="all, delete",
|
"SubMenu",
|
||||||
|
backref="menu",
|
||||||
|
lazy="dynamic",
|
||||||
|
cascade="all, delete",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class SubMenu(Base):
|
class SubMenu(Base):
|
||||||
__tablename__ = "submenu"
|
__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(
|
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"
|
__tablename__ = "dish"
|
||||||
|
|
||||||
price: Mapped[float]
|
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")
|
||||||
|
)
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
from fastapi import APIRouter, Depends, HTTPException
|
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
|
from fastapi import APIRouter, Depends, HTTPException
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
from fastfood import schemas
|
from fastfood import schemas
|
||||||
from fastfood.utils import price_converter
|
|
||||||
from fastfood.cruds import crud
|
from fastfood.cruds import crud
|
||||||
from fastfood.dbase import get_async_session
|
from fastfood.dbase import get_async_session
|
||||||
|
from fastfood.utils import price_converter
|
||||||
|
|
||||||
router = APIRouter(
|
router = APIRouter(
|
||||||
prefix="/api/v1/menus/{menu_id}/submenus/{submenu_id}/dishes",
|
prefix="/api/v1/menus/{menu_id}/submenus/{submenu_id}/dishes",
|
||||||
|
@ -17,9 +17,7 @@ router = APIRouter(
|
||||||
|
|
||||||
@router.get("/")
|
@router.get("/")
|
||||||
async def get_dishes(
|
async def get_dishes(
|
||||||
menu_id: UUID,
|
menu_id: UUID, submenu_id: UUID, session: AsyncSession = Depends(get_async_session)
|
||||||
submenu_id: UUID,
|
|
||||||
session: AsyncSession = Depends(get_async_session)
|
|
||||||
):
|
):
|
||||||
result = await crud.get_dishes(submenu_id=submenu_id, session=session)
|
result = await crud.get_dishes(submenu_id=submenu_id, session=session)
|
||||||
return result
|
return result
|
||||||
|
@ -48,16 +46,15 @@ async def get_dish(
|
||||||
session: AsyncSession = Depends(get_async_session),
|
session: AsyncSession = Depends(get_async_session),
|
||||||
):
|
):
|
||||||
result = await crud.get_dish_item(
|
result = await crud.get_dish_item(
|
||||||
dish_id=dish_id, session=session,
|
dish_id=dish_id,
|
||||||
|
session=session,
|
||||||
)
|
)
|
||||||
if not result:
|
if not result:
|
||||||
raise HTTPException(status_code=404, detail="dish not found")
|
raise HTTPException(status_code=404, detail="dish not found")
|
||||||
return price_converter(result)
|
return price_converter(result)
|
||||||
|
|
||||||
|
|
||||||
@router.patch(
|
@router.patch("/{dish_id}")
|
||||||
"/{dish_id}"
|
|
||||||
)
|
|
||||||
async def update_dish(
|
async def update_dish(
|
||||||
menu_id: UUID,
|
menu_id: UUID,
|
||||||
submenu_id: UUID,
|
submenu_id: UUID,
|
||||||
|
@ -66,11 +63,18 @@ async def update_dish(
|
||||||
session: AsyncSession = Depends(get_async_session),
|
session: AsyncSession = Depends(get_async_session),
|
||||||
):
|
):
|
||||||
result = await crud.update_dish_item(
|
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)
|
return price_converter(result)
|
||||||
|
|
||||||
|
|
||||||
@router.delete("/{dish_id}")
|
@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)
|
await crud.delete_dish_item(dish_id=dish_id, session=session)
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
from fastapi import APIRouter, Depends, HTTPException
|
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
|
from fastapi import APIRouter, Depends, HTTPException
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
from fastfood import schemas
|
from fastfood import schemas
|
||||||
from fastfood.cruds import crud
|
from fastfood.cruds import crud
|
||||||
from fastfood.dbase import get_async_session
|
from fastfood.dbase import get_async_session
|
||||||
|
|
||||||
|
|
||||||
router = APIRouter(
|
router = APIRouter(
|
||||||
prefix="/api/v1/menus",
|
prefix="/api/v1/menus",
|
||||||
tags=["menu"],
|
tags=["menu"],
|
||||||
|
@ -34,8 +34,8 @@ async def add_menu(
|
||||||
|
|
||||||
@router.get("/{menu_id}", response_model=schemas.MenuRead)
|
@router.get("/{menu_id}", response_model=schemas.MenuRead)
|
||||||
async def get_menu(
|
async def get_menu(
|
||||||
menu_id: UUID,
|
menu_id: UUID,
|
||||||
session: AsyncSession = Depends(get_async_session),
|
session: AsyncSession = Depends(get_async_session),
|
||||||
):
|
):
|
||||||
result = await crud.get_menu_item(menu_id=menu_id, session=session)
|
result = await crud.get_menu_item(menu_id=menu_id, session=session)
|
||||||
if not result:
|
if not result:
|
||||||
|
@ -50,15 +50,16 @@ async def update_menu(
|
||||||
session: AsyncSession = Depends(get_async_session),
|
session: AsyncSession = Depends(get_async_session),
|
||||||
):
|
):
|
||||||
result = await crud.update_menu_item(
|
result = await crud.update_menu_item(
|
||||||
menu_id=menu_id, menu=menu, session=session,
|
menu_id=menu_id,
|
||||||
|
menu=menu,
|
||||||
|
session=session,
|
||||||
)
|
)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
@router.delete("/{menu_id}")
|
@router.delete("/{menu_id}")
|
||||||
async def delete_menu(
|
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)
|
await crud.delete_menu_item(menu_id=menu_id, session=session)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
from fastapi import APIRouter, Depends, HTTPException
|
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
|
from fastapi import APIRouter, Depends, HTTPException
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
from fastfood import schemas
|
from fastfood import schemas
|
||||||
from fastfood.cruds import crud
|
from fastfood.cruds import crud
|
||||||
from fastfood.dbase import get_async_session
|
from fastfood.dbase import get_async_session
|
||||||
|
|
||||||
|
|
||||||
router = APIRouter(
|
router = APIRouter(
|
||||||
prefix="/api/v1/menus/{menu_id}/submenus",
|
prefix="/api/v1/menus/{menu_id}/submenus",
|
||||||
tags=["submenu"],
|
tags=["submenu"],
|
||||||
|
@ -15,8 +15,7 @@ router = APIRouter(
|
||||||
|
|
||||||
@router.get("/")
|
@router.get("/")
|
||||||
async def get_submenus(
|
async def get_submenus(
|
||||||
menu_id: UUID,
|
menu_id: UUID, session: AsyncSession = Depends(get_async_session)
|
||||||
session: AsyncSession = Depends(get_async_session)
|
|
||||||
):
|
):
|
||||||
result = await crud.get_submenus(menu_id=menu_id, session=session)
|
result = await crud.get_submenus(menu_id=menu_id, session=session)
|
||||||
return result
|
return result
|
||||||
|
@ -43,7 +42,9 @@ async def get_submenu(
|
||||||
session: AsyncSession = Depends(get_async_session),
|
session: AsyncSession = Depends(get_async_session),
|
||||||
):
|
):
|
||||||
result = await crud.get_submenu_item(
|
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:
|
if not result:
|
||||||
raise HTTPException(status_code=404, detail="submenu not found")
|
raise HTTPException(status_code=404, detail="submenu not found")
|
||||||
|
@ -61,13 +62,15 @@ async def update_submenu(
|
||||||
session: AsyncSession = Depends(get_async_session),
|
session: AsyncSession = Depends(get_async_session),
|
||||||
):
|
):
|
||||||
result = await crud.update_submenu_item(
|
result = await crud.update_submenu_item(
|
||||||
submenu_id=submenu_id, submenu=submenu, session=session,
|
submenu_id=submenu_id,
|
||||||
|
submenu=submenu,
|
||||||
|
session=session,
|
||||||
)
|
)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
@router.delete("/{submenu_id}")
|
@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)
|
await crud.delete_submenu_item(submenu_id=submenu_id, session=session)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
def price_converter(dish: dict) -> dict:
|
def price_converter(dish: dict) -> dict:
|
||||||
dish.price = str(dish.price)
|
dish.price = str(dish.price)
|
||||||
return dish
|
return dish
|
||||||
|
|
Loading…
Reference in New Issue