uuid вместо int в primary key всех таблиц
parent
7c130fb59f
commit
068336aebb
|
@ -1,7 +1,8 @@
|
||||||
from sqlalchemy import select
|
from sqlalchemy import select
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
from fastfood.dbase import async_session_maker, async_engine
|
|
||||||
from fastfood import models, schemas
|
from fastfood import models, schemas
|
||||||
|
from fastfood.dbase import async_engine, async_session_maker
|
||||||
|
|
||||||
|
|
||||||
async def create_db_and_tables():
|
async def create_db_and_tables():
|
||||||
|
@ -12,12 +13,11 @@ async def create_db_and_tables():
|
||||||
|
|
||||||
class Crud:
|
class Crud:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def get_menus():
|
async def get_menus(session: AsyncSession):
|
||||||
async with async_session_maker() as session:
|
async with session:
|
||||||
query = select(models.Menu)
|
query = select(models.Menu)
|
||||||
result = await session.execute(query)
|
result = await session.execute(query)
|
||||||
return result.scalars().all()
|
return result.mappings().all()
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def add_menu(menu: schemas.Menu):
|
async def add_menu(menu: schemas.Menu):
|
||||||
|
|
|
@ -1,11 +1,19 @@
|
||||||
|
import uuid
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
from typing import List, Annotated, Optional
|
from typing import Annotated, List, Optional
|
||||||
|
|
||||||
from sqlalchemy import ForeignKey
|
from sqlalchemy import ForeignKey
|
||||||
from sqlalchemy.orm import Mapped, mapped_column, relationship, DeclarativeBase
|
from sqlalchemy.dialects.postgresql import UUID
|
||||||
|
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
|
||||||
|
|
||||||
|
uuidpk = Annotated[
|
||||||
intpk = Annotated[int, mapped_column(primary_key=True)]
|
int,
|
||||||
|
mapped_column(
|
||||||
|
UUID(as_uuid=True),
|
||||||
|
primary_key=True,
|
||||||
|
default=uuid.uuid4,
|
||||||
|
),
|
||||||
|
]
|
||||||
str_25 = Annotated[str, 25]
|
str_25 = Annotated[str, 25]
|
||||||
|
|
||||||
|
|
||||||
|
@ -16,7 +24,7 @@ class Base(DeclarativeBase):
|
||||||
class Menu(Base):
|
class Menu(Base):
|
||||||
__tablename__ = "menu"
|
__tablename__ = "menu"
|
||||||
|
|
||||||
id: Mapped[intpk]
|
id: Mapped[uuidpk]
|
||||||
title: Mapped[str_25]
|
title: Mapped[str_25]
|
||||||
description: Mapped[Optional[str]]
|
description: Mapped[Optional[str]]
|
||||||
submenus: Mapped[List["SubMenu"]] = relationship()
|
submenus: Mapped[List["SubMenu"]] = relationship()
|
||||||
|
@ -25,18 +33,18 @@ class Menu(Base):
|
||||||
class SubMenu(Base):
|
class SubMenu(Base):
|
||||||
__tablename__ = "submenu"
|
__tablename__ = "submenu"
|
||||||
|
|
||||||
id: Mapped[intpk]
|
id: Mapped[uuidpk]
|
||||||
title: Mapped[str_25]
|
title: Mapped[str_25]
|
||||||
description: Mapped[Optional[str]]
|
description: Mapped[Optional[str]]
|
||||||
parent_menu: Mapped[int] = mapped_column(ForeignKey("menu.id"))
|
parent_menu: Mapped[UUID] = mapped_column(ForeignKey("menu.id"))
|
||||||
dishes: Mapped[List["Dish"]] = relationship()
|
dishes: Mapped[List["Dish"]] = relationship()
|
||||||
|
|
||||||
|
|
||||||
class Dish(Base):
|
class Dish(Base):
|
||||||
__tablename__ = "dish"
|
__tablename__ = "dish"
|
||||||
|
|
||||||
id: Mapped[intpk]
|
id: Mapped[uuidpk]
|
||||||
title: Mapped[str_25]
|
title: Mapped[str_25]
|
||||||
description: Mapped[Optional[str]]
|
description: Mapped[Optional[str]]
|
||||||
price: Mapped[Decimal]
|
price: Mapped[Decimal]
|
||||||
parent_submenu: Mapped[int] = mapped_column(ForeignKey("submenu.id"))
|
parent_submenu: Mapped[UUID] = mapped_column(ForeignKey("submenu.id"))
|
||||||
|
|
|
@ -1,30 +1,26 @@
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends
|
from fastapi import APIRouter, Depends
|
||||||
from sqlalchemy import insert, select
|
from sqlalchemy import insert, select
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
from fastfood import models, schemas
|
from fastfood import models, schemas
|
||||||
|
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()
|
||||||
|
|
||||||
|
|
||||||
@router.get("/api/v1/menus")
|
@router.get("/api/v1/menus")
|
||||||
async def read_menu(session: AsyncSession = Depends(get_async_session)):
|
async def get_menus(session: AsyncSession = Depends(get_async_session)):
|
||||||
stmt = select(models.Menu)
|
result = await crud.get_menus(session=session)
|
||||||
result = await session.execute(stmt)
|
return result
|
||||||
data = result.mappings().all()
|
|
||||||
return data
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/api/v1/menus", status_code=201)
|
@router.post("/api/v1/menus", status_code=201)
|
||||||
async def add_menu(menu: schemas.MenuBase, session: AsyncSession = Depends(get_async_session)):
|
async def add_menu(
|
||||||
"""
|
menu: schemas.MenuBase,
|
||||||
docstring
|
session: AsyncSession = Depends(get_async_session),
|
||||||
"""
|
):
|
||||||
stmt = insert(models.Menu).values(**menu.dict())
|
stmt = insert(models.Menu).values(**menu.dict())
|
||||||
await session.execute(stmt)
|
await session.execute(stmt)
|
||||||
await session.commit()
|
await session.commit()
|
||||||
return {"status": "success"}
|
return {"status": "success"}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue