repo.dish typehint

develop
Сергей Ванюшкин 2024-02-04 02:28:12 +03:00
parent f807bdd275
commit 628babc295
2 changed files with 18 additions and 19 deletions

View File

@ -4,8 +4,8 @@ from fastapi import Depends
from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from fastfood import models
from fastfood.dbase import get_async_session
from fastfood.models import Dish
from fastfood.schemas import Dish_db
@ -13,20 +13,20 @@ class DishRepository:
def __init__(self, session: AsyncSession = Depends(get_async_session)):
self.db = session
async def get_dishes(self, menu_id: UUID, submenu_id: UUID):
query = select(models.Dish).where(
models.Dish.parent_submenu == submenu_id,
async def get_dishes(self, menu_id: UUID, submenu_id: UUID) -> list[Dish]:
query = select(Dish).where(
Dish.parent_submenu == submenu_id,
)
dishes = await self.db.execute(query)
return dishes.scalars().all()
return [x for x in dishes.scalars().all()]
async def create_dish_item(
self,
menu_id: UUID,
submenu_id: UUID,
dish_data: Dish_db,
):
new_dish = models.Dish(**dish_data.model_dump())
) -> Dish:
new_dish = Dish(**dish_data.model_dump())
new_dish.parent_submenu = submenu_id
self.db.add(new_dish)
await self.db.commit()
@ -38,8 +38,8 @@ class DishRepository:
menu_id: UUID,
submenu_id: UUID,
dish_id: UUID,
):
query = select(models.Dish).where(models.Dish.id == dish_id)
) -> Dish | None:
query = select(Dish).where(Dish.id == dish_id)
submenu = await self.db.execute(query)
return submenu.scalars().one_or_none()
@ -49,15 +49,11 @@ class DishRepository:
submenu_id: UUID,
dish_id: UUID,
dish_data: Dish_db,
):
query = (
update(models.Dish)
.where(models.Dish.id == dish_id)
.values(**dish_data.model_dump())
)
) -> Dish:
query = update(Dish).where(Dish.id == dish_id).values(**dish_data.model_dump())
await self.db.execute(query)
await self.db.commit()
qr = select(models.Dish).where(models.Dish.id == dish_id)
qr = select(Dish).where(Dish.id == dish_id)
updated_submenu = await self.db.execute(qr)
return updated_submenu.scalars().one()
@ -66,7 +62,8 @@ class DishRepository:
menu_id: UUID,
submenu_id: UUID,
dish_id: UUID,
):
query = delete(models.Dish).where(models.Dish.id == dish_id)
) -> int:
query = delete(Dish).where(Dish.id == dish_id)
await self.db.execute(query)
await self.db.commit()
return 200

View File

@ -22,12 +22,14 @@ class DishService:
async def read_dishes(self, menu_id: UUID, submenu_id: UUID):
data = await self.dish_repo.get_dishes(menu_id, submenu_id)
if data:
print(type(data[0]))
response = []
for row in data:
dish = row.__dict__
dish['price'] = str(dish['price'])
response.append(dish)
return data
return response
async def create_dish(
self,