uuid вместо int в primary key всех таблиц

develop
Сергей Ванюшкин 2024-01-19 18:18:46 +00:00
parent 7c130fb59f
commit 068336aebb
3 changed files with 32 additions and 28 deletions

View File

@ -1,7 +1,8 @@
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.dbase import async_engine, async_session_maker
async def create_db_and_tables():
@ -12,12 +13,11 @@ async def create_db_and_tables():
class Crud:
@staticmethod
async def get_menus():
async with async_session_maker() as session:
async def get_menus(session: AsyncSession):
async with session:
query = select(models.Menu)
result = await session.execute(query)
return result.scalars().all()
return result.mappings().all()
@staticmethod
async def add_menu(menu: schemas.Menu):

View File

@ -1,11 +1,19 @@
import uuid
from decimal import Decimal
from typing import List, Annotated, Optional
from typing import Annotated, List, Optional
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
intpk = Annotated[int, mapped_column(primary_key=True)]
uuidpk = Annotated[
int,
mapped_column(
UUID(as_uuid=True),
primary_key=True,
default=uuid.uuid4,
),
]
str_25 = Annotated[str, 25]
@ -16,7 +24,7 @@ class Base(DeclarativeBase):
class Menu(Base):
__tablename__ = "menu"
id: Mapped[intpk]
id: Mapped[uuidpk]
title: Mapped[str_25]
description: Mapped[Optional[str]]
submenus: Mapped[List["SubMenu"]] = relationship()
@ -25,18 +33,18 @@ class Menu(Base):
class SubMenu(Base):
__tablename__ = "submenu"
id: Mapped[intpk]
id: Mapped[uuidpk]
title: Mapped[str_25]
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()
class Dish(Base):
__tablename__ = "dish"
__tablename__ = "dish"
id: Mapped[intpk]
id: Mapped[uuidpk]
title: Mapped[str_25]
description: Mapped[Optional[str]]
price: Mapped[Decimal]
parent_submenu: Mapped[int] = mapped_column(ForeignKey("submenu.id"))
parent_submenu: Mapped[UUID] = mapped_column(ForeignKey("submenu.id"))

View File

@ -1,30 +1,26 @@
from sqlalchemy.ext.asyncio import AsyncSession
from fastapi import APIRouter, Depends
from sqlalchemy import insert, select
from sqlalchemy.ext.asyncio import AsyncSession
from fastfood import models, schemas
from fastfood.crud import Crud as crud
from fastfood.dbase import get_async_session
router = APIRouter()
@router.get("/api/v1/menus")
async def read_menu(session: AsyncSession = Depends(get_async_session)):
stmt = select(models.Menu)
result = await session.execute(stmt)
data = result.mappings().all()
return data
async def get_menus(session: AsyncSession = Depends(get_async_session)):
result = await crud.get_menus(session=session)
return result
@router.post("/api/v1/menus", status_code=201)
async def add_menu(menu: schemas.MenuBase, session: AsyncSession = Depends(get_async_session)):
"""
docstring
"""
async def add_menu(
menu: schemas.MenuBase,
session: AsyncSession = Depends(get_async_session),
):
stmt = insert(models.Menu).values(**menu.dict())
await session.execute(stmt)
await session.commit()
return {"status": "success"}