поправил под pep

develop
Сергей Ванюшкин 2024-01-22 05:48:48 +00:00
parent ed11efa704
commit c01affbcb9
11 changed files with 94 additions and 63 deletions

View File

@ -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": "Блюда и работа с ними"},
]

View File

@ -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():

View File

@ -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()

View File

@ -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()

View File

@ -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)

View File

@ -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,

View File

@ -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")
)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -1,4 +1,3 @@
def price_converter(dish: dict) -> dict:
dish.price = str(dish.price)
return dish