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 import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from fastfood import models
from fastfood.dbase import get_async_session from fastfood.dbase import get_async_session
from fastfood.models import Dish
from fastfood.schemas import Dish_db from fastfood.schemas import Dish_db
@ -13,20 +13,20 @@ class DishRepository:
def __init__(self, session: AsyncSession = Depends(get_async_session)): def __init__(self, session: AsyncSession = Depends(get_async_session)):
self.db = session self.db = session
async def get_dishes(self, menu_id: UUID, submenu_id: UUID): async def get_dishes(self, menu_id: UUID, submenu_id: UUID) -> list[Dish]:
query = select(models.Dish).where( query = select(Dish).where(
models.Dish.parent_submenu == submenu_id, Dish.parent_submenu == submenu_id,
) )
dishes = await self.db.execute(query) dishes = await self.db.execute(query)
return dishes.scalars().all() return [x for x in dishes.scalars().all()]
async def create_dish_item( async def create_dish_item(
self, self,
menu_id: UUID, menu_id: UUID,
submenu_id: UUID, submenu_id: UUID,
dish_data: Dish_db, dish_data: Dish_db,
): ) -> Dish:
new_dish = models.Dish(**dish_data.model_dump()) new_dish = Dish(**dish_data.model_dump())
new_dish.parent_submenu = submenu_id new_dish.parent_submenu = submenu_id
self.db.add(new_dish) self.db.add(new_dish)
await self.db.commit() await self.db.commit()
@ -38,8 +38,8 @@ class DishRepository:
menu_id: UUID, menu_id: UUID,
submenu_id: UUID, submenu_id: UUID,
dish_id: UUID, dish_id: UUID,
): ) -> Dish | None:
query = select(models.Dish).where(models.Dish.id == dish_id) query = select(Dish).where(Dish.id == dish_id)
submenu = await self.db.execute(query) submenu = await self.db.execute(query)
return submenu.scalars().one_or_none() return submenu.scalars().one_or_none()
@ -49,15 +49,11 @@ class DishRepository:
submenu_id: UUID, submenu_id: UUID,
dish_id: UUID, dish_id: UUID,
dish_data: Dish_db, dish_data: Dish_db,
): ) -> Dish:
query = ( query = update(Dish).where(Dish.id == dish_id).values(**dish_data.model_dump())
update(models.Dish)
.where(models.Dish.id == dish_id)
.values(**dish_data.model_dump())
)
await self.db.execute(query) await self.db.execute(query)
await self.db.commit() 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) updated_submenu = await self.db.execute(qr)
return updated_submenu.scalars().one() return updated_submenu.scalars().one()
@ -66,7 +62,8 @@ class DishRepository:
menu_id: UUID, menu_id: UUID,
submenu_id: UUID, submenu_id: UUID,
dish_id: UUID, dish_id: UUID,
): ) -> int:
query = delete(models.Dish).where(models.Dish.id == dish_id) query = delete(Dish).where(Dish.id == dish_id)
await self.db.execute(query) await self.db.execute(query)
await self.db.commit() 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): async def read_dishes(self, menu_id: UUID, submenu_id: UUID):
data = await self.dish_repo.get_dishes(menu_id, submenu_id) data = await self.dish_repo.get_dishes(menu_id, submenu_id)
if data:
print(type(data[0]))
response = [] response = []
for row in data: for row in data:
dish = row.__dict__ dish = row.__dict__
dish['price'] = str(dish['price']) dish['price'] = str(dish['price'])
response.append(dish) response.append(dish)
return data return response
async def create_dish( async def create_dish(
self, self,