Api для блюд. Test OK

develop
Сергей Ванюшкин 2024-01-21 08:12:51 +03:00
parent ad497254cd
commit f6c39cea05
4 changed files with 145 additions and 34 deletions

View File

@ -65,7 +65,7 @@ class Crud:
@staticmethod @staticmethod
async def create_submenu_item( async def create_submenu_item(
menu_id: UUID, submenu: schemas.SubmenuBase, 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())
@ -105,4 +105,53 @@ class Crud:
await session.execute(query) await session.execute(query)
await session.commit() await session.commit()
@staticmethod
async def get_dishes(submenu_id: UUID, session: AsyncSession):
async with session:
query = select(models.Dish).where(models.Dish.parent_submenu == submenu_id)
dishes = await session.execute(query)
return dishes.scalars().all()
@staticmethod
async def create_dish_item(
submenu_id: UUID, dish: schemas.DishBase, session: AsyncSession,
):
async with session:
new_dish = models.Dish(**dish.model_dump())
new_dish.parent_submenu = submenu_id
session.add(new_dish)
await session.flush()
await session.commit()
return new_dish
@staticmethod
async def get_dish_item(
dish_id: UUID, session: AsyncSession,
):
async with session:
query = select(models.Dish).where(models.Dish.id == dish_id)
submenu = await session.execute(query)
return submenu.scalars().one_or_none()
@staticmethod
async def update_dish_item(
dish_id: UUID,
dish: schemas.DishBase,
session: AsyncSession,
):
async with session:
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)
updated_submenu = await session.execute(qr)
return updated_submenu.scalars().one()
@staticmethod
async def delete_dish_item(dish_id: UUID, session: AsyncSession):
async with session:
query = delete(models.Dish).where(models.Dish.id == dish_id)
await session.execute(query)
await session.commit()

View File

@ -18,33 +18,30 @@ str_25 = Annotated[str, 25]
class Base(DeclarativeBase): class Base(DeclarativeBase):
pass id: Mapped[uuidpk]
title: Mapped[str_25]
description: Mapped[Optional[str]]
class Menu(Base): class Menu(Base):
__tablename__ = "menu" __tablename__ = "menu"
id: Mapped[uuidpk] submenus: Mapped[List["SubMenu"]] = relationship(
title: Mapped[str_25] "SubMenu", backref="menu", lazy='dynamic', cascade="all,delete",
description: Mapped[Optional[str]] )
submenus: Mapped[List["SubMenu"]] = relationship("SubMenu", backref="menu", lazy='dynamic')
class SubMenu(Base): class SubMenu(Base):
__tablename__ = "submenu" __tablename__ = "submenu"
id: Mapped[uuidpk]
title: Mapped[str_25]
description: Mapped[Optional[str]]
parent_menu: Mapped[uuid.UUID] = mapped_column(ForeignKey("menu.id")) parent_menu: Mapped[uuid.UUID] = mapped_column(ForeignKey("menu.id"))
dishes: Mapped[List["Dish"]] = relationship() dishes: Mapped[List["Dish"]] = relationship(
"Dish", backref="submenu", lazy="dynamic", cascade="all,delete",
)
class Dish(Base): class Dish(Base):
__tablename__ = "dish" __tablename__ = "dish"
id: Mapped[uuidpk] price: Mapped[float]
title: Mapped[str_25] parent_submenu: Mapped[uuid.UUID] = mapped_column(ForeignKey("submenu.id"))
description: Mapped[Optional[str]]
price: Mapped[Decimal]
parent_submenu: Mapped[UUID] = mapped_column(ForeignKey("submenu.id"))

View File

@ -1,13 +1,13 @@
from typing import List, Optional from typing import List, Optional
from fastapi import APIRouter, Depends, HTTPException from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy import insert, select
from uuid import UUID from uuid import UUID
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from fastfood import models, schemas from fastfood import schemas
from fastfood.crud import Crud as crud from fastfood.crud import Crud as crud
from fastfood.dbase import get_async_session from fastfood.dbase import get_async_session
router = APIRouter() router = APIRouter()
@ -36,7 +36,7 @@ async def get_menu(
): ):
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:
raise HTTPException(status_code=404, detail="menu not found") raise HTTPException(status_code=404, detail="menu not found")
return result return result
@ -46,12 +46,16 @@ async def update_menu(
menu: schemas.MenuBase, menu: schemas.MenuBase,
session: AsyncSession = Depends(get_async_session), session: AsyncSession = Depends(get_async_session),
): ):
result = await crud.update_menu_item(menu_id=menu_id, menu=menu, session=session) result = await crud.update_menu_item(
menu_id=menu_id, menu=menu, session=session,
)
return result return result
@router.delete("/api/v1/menus/{menu_id}") @router.delete("/api/v1/menus/{menu_id}")
async def delete_menu(menu_id: UUID, session: AsyncSession = Depends(get_async_session)): async def delete_menu(
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)
@ -67,7 +71,7 @@ async def get_submenus(
@router.post("/api/v1/menus/{menu_id}/submenus", status_code=201) @router.post("/api/v1/menus/{menu_id}/submenus", status_code=201)
async def create_submenu_item( async def create_submenu_item(
menu_id: UUID, menu_id: UUID,
submenu: schemas.SubmenuBase, submenu: schemas.MenuBase,
session: AsyncSession = Depends(get_async_session), session: AsyncSession = Depends(get_async_session),
): ):
result = await crud.create_submenu_item( result = await crud.create_submenu_item(
@ -88,11 +92,14 @@ async def get_submenu(
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")
return result return result
@router.patch("/api/v1/menus/{menu_id}/submenus/{submenu_id}", response_model=schemas.MenuBase) @router.patch(
"/api/v1/menus/{menu_id}/submenus/{submenu_id}",
response_model=schemas.MenuBase,
)
async def update_submenu( async def update_submenu(
menu_id: UUID, menu_id: UUID,
submenu_id: UUID, submenu_id: UUID,
@ -106,5 +113,67 @@ async def update_submenu(
@router.delete("/api/v1/menus/{menu_id}/submenus/{submenu_id}") @router.delete("/api/v1/menus/{menu_id}/submenus/{submenu_id}")
async def delete_menu(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)
@router.get("/api/v1/menus/{menu_id}/submenus/{submenu_id}/dishes")
async def get_dishes(
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
@router.post("/api/v1/menus/{menu_id}/submenus/{submenu_id}/dishes", status_code=201)
async def create_dish(
menu_id: UUID,
submenu_id: UUID,
dish: schemas.DishBase,
session: AsyncSession = Depends(get_async_session),
):
result = await crud.create_dish_item(
submenu_id=submenu_id,
dish=dish,
session=session,
)
return result
@router.get("/api/v1/menus/{menu_id}/submenus/{submenu_id}/dishes/{dish_id}")
async def get_dish(
menu_id: UUID,
submenu_id: UUID,
dish_id: UUID,
session: AsyncSession = Depends(get_async_session),
):
result = await crud.get_dish_item(
dish_id=dish_id, session=session,
)
if not result:
raise HTTPException(status_code=404, detail="dish not found")
return result
@router.patch(
"/api/v1/menus/{menu_id}/submenus/{submenu_id}/dishes/{dish_id}",
response_model=schemas.DishBase,
)
async def update_submenu(
menu_id: UUID,
submenu_id: UUID,
dish_id: UUID,
dish: schemas.DishBase,
session: AsyncSession = Depends(get_async_session),
):
result = await crud.update_dish_item(
dish_id=dish_id, dish=dish, session=session,
)
return result
@router.delete("/api/v1/menus/{menu_id}/submenus/{submenu_id}/dishes/{dish_id}")
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

@ -8,22 +8,18 @@ from pydantic import BaseModel
class MenuBase(BaseModel): class MenuBase(BaseModel):
title: str title: str
description: Optional[str] description: Optional[str]
class Config: class Config:
from_attributes = True from_attributes = True
class Menu(MenuBase): class Menu(MenuBase):
id: UUID id: UUID
title: str
description: Optional[str]
# submenus: Optional[List[SubMenu]]
class Config:
from_attributes = True
class SubmenuBase(MenuBase): class DishBase(MenuBase):
price: str
class Dish(DishBase, Menu):
pass pass