Compare commits

...

7 Commits

9 changed files with 175 additions and 109 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

@ -5,30 +5,31 @@ from sqlalchemy import delete, distinct, func, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import aliased
from fastfood import models, schemas
from fastfood import schemas
from fastfood.dbase import get_async_session
from fastfood.models import Dish, Menu, SubMenu
class MenuRepository:
def __init__(self, session: AsyncSession = Depends(get_async_session)):
self.db = session
async def get_menus(self):
query = select(models.Menu)
async def get_menus(self) -> list[Menu]:
query = select(Menu)
menus = await self.db.execute(query)
return menus.scalars().all()
return [x for x in menus.scalars().all()]
async def create_menu_item(self, menu: schemas.MenuBase):
new_menu = models.Menu(**menu.model_dump())
async def create_menu_item(self, menu: schemas.MenuBase) -> Menu:
new_menu = Menu(**menu.model_dump())
self.db.add(new_menu)
await self.db.commit()
await self.db.refresh(new_menu)
return new_menu
async def get_menu_item(self, menu_id: UUID):
m = aliased(models.Menu)
s = aliased(models.SubMenu)
d = aliased(models.Dish)
async def get_menu_item(self, menu_id: UUID) -> Menu | None:
m = aliased(Menu)
s = aliased(SubMenu)
d = aliased(Dish)
query = (
select(
@ -51,19 +52,16 @@ class MenuRepository:
self,
menu_id: UUID,
menu: schemas.MenuBase,
):
query = (
update(models.Menu)
.where(models.Menu.id == menu_id)
.values(**menu.model_dump())
)
) -> Menu:
query = update(Menu).where(Menu.id == menu_id).values(**menu.model_dump())
await self.db.execute(query)
await self.db.commit()
qr = select(models.Menu).where(models.Menu.id == menu_id)
qr = select(Menu).where(Menu.id == menu_id)
updated_menu = await self.db.execute(qr)
return updated_menu
return updated_menu.scalar_one()
async def delete_menu_item(self, menu_id: UUID):
query = delete(models.Menu).where(models.Menu.id == menu_id)
async def delete_menu_item(self, menu_id: UUID) -> int:
query = delete(Menu).where(Menu.id == menu_id)
await self.db.execute(query)
await self.db.commit()
return 200

View File

@ -5,40 +5,45 @@ from sqlalchemy import delete, distinct, func, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import aliased
from fastfood import models, schemas
from fastfood.dbase import get_async_session
from fastfood.models import Dish, SubMenu
from fastfood.schemas import MenuBase
class SubMenuRepository:
def __init__(self, session: AsyncSession = Depends(get_async_session)):
self.db = session
async def get_submenus(self, menu_id: UUID):
query = select(models.SubMenu).where(
models.SubMenu.parent_menu == menu_id,
async def get_submenus(self, menu_id: UUID) -> list[SubMenu]:
query = select(SubMenu).where(
SubMenu.parent_menu == menu_id,
)
submenus = await self.db.execute(query)
return submenus
return [x for x in submenus.scalars().all()]
async def create_submenu_item(
self,
menu_id: UUID,
submenu: schemas.MenuBase,
):
new_submenu = models.SubMenu(**submenu.model_dump())
submenu: MenuBase,
) -> SubMenu:
new_submenu = SubMenu(**submenu.model_dump())
new_submenu.parent_menu = menu_id
self.db.add(new_submenu)
await self.db.commit()
await self.db.refresh(new_submenu)
return new_submenu
full_sub = await self.get_submenu_item(menu_id, new_submenu.id)
if full_sub is None:
raise TypeError
return full_sub
async def get_submenu_item(
self,
menu_id: UUID,
submenu_id: UUID,
):
s = aliased(models.SubMenu)
d = aliased(models.Dish)
) -> SubMenu | None:
s = aliased(SubMenu)
d = aliased(Dish)
query = (
select(s, func.count(distinct(d.id)).label('dishes_count'))
.join(d, s.id == d.parent_submenu, isouter=True)
@ -55,22 +60,23 @@ class SubMenuRepository:
self,
menu_id: UUID,
submenu_id: UUID,
submenu_data: schemas.MenuBase,
):
submenu_data: MenuBase,
) -> SubMenu:
query = (
update(models.SubMenu)
.where(models.SubMenu.id == submenu_id)
update(SubMenu)
.where(SubMenu.id == submenu_id)
.values(**submenu_data.model_dump())
)
await self.db.execute(query)
await self.db.commit()
qr = select(models.SubMenu).where(models.SubMenu.id == submenu_id)
qr = select(SubMenu).where(SubMenu.id == submenu_id)
updated_submenu = await self.db.execute(qr)
return updated_submenu
return updated_submenu.scalar_one()
async def delete_submenu_item(self, menu_id: UUID, submenu_id: UUID):
query = delete(models.SubMenu).where(
models.SubMenu.id == submenu_id,
async def delete_submenu_item(self, menu_id: UUID, submenu_id: UUID) -> int:
query = delete(SubMenu).where(
SubMenu.id == submenu_id,
)
await self.db.execute(query)
await self.db.commit()
return 200

View File

@ -17,7 +17,7 @@ async def get_dishes(
submenu_id: UUID,
dish: DishService = Depends(),
background_tasks: BackgroundTasks = BackgroundTasks(),
):
) -> list[Dish]:
result = await dish.read_dishes(menu_id, submenu_id)
return result

View File

@ -2,7 +2,7 @@ from uuid import UUID
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException
from fastfood.schemas import Menu, MenuBase, MenuRead
from fastfood.schemas import MenuBase, MenuRead
from fastfood.service.menu import MenuService
router = APIRouter(
@ -11,7 +11,7 @@ router = APIRouter(
)
@router.get('/', response_model=list[Menu])
@router.get('/', response_model=list[MenuRead])
async def get_menus(
menu: MenuService = Depends(),
background_tasks: BackgroundTasks = BackgroundTasks(),
@ -19,7 +19,7 @@ async def get_menus(
return await menu.read_menus()
@router.post('/', status_code=201, response_model=Menu)
@router.post('/', status_code=201, response_model=MenuRead)
async def add_menu(
menu: MenuBase,
responce: MenuService = Depends(),
@ -52,7 +52,7 @@ async def update_menu(
menu_id=menu_id,
menu_data=menu,
)
return result.scalars().one()
return result
@router.delete('/{menu_id}')

View File

@ -1,4 +1,3 @@
from typing import Optional
from uuid import UUID
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException
@ -12,14 +11,14 @@ router = APIRouter(
)
@router.get('/', response_model=Optional[list[SubMenuRead]])
@router.get('/', response_model=list[SubMenuRead])
async def get_submenus(
menu_id: UUID,
submenu: SubmenuService = Depends(),
background_tasks: BackgroundTasks = BackgroundTasks(),
):
result = await submenu.read_submenus(menu_id=menu_id)
return result.scalars().all()
return result
@router.post('/', status_code=201, response_model=SubMenuRead)
@ -54,7 +53,7 @@ async def get_submenu(
@router.patch(
'/{submenu_id}',
response_model=MenuBase,
response_model=SubMenuRead,
)
async def update_submenu(
menu_id: UUID,
@ -68,7 +67,7 @@ async def update_submenu(
submenu_id=submenu_id,
submenu_data=submenu_data,
)
return result.scalars().one()
return result
@router.delete('/{submenu_id}')

View File

@ -6,7 +6,7 @@ from fastapi import BackgroundTasks, Depends
from fastfood.dbase import get_async_redis_client
from fastfood.repository.dish import DishRepository
from fastfood.repository.redis import RedisRepository
from fastfood.schemas import Dish_db, DishBase
from fastfood.schemas import Dish, Dish_db, DishBase
class DishService:
@ -15,26 +15,26 @@ class DishService:
dish_repo: DishRepository = Depends(),
redis_client: redis.Redis = Depends(get_async_redis_client),
background_tasks: BackgroundTasks = None,
):
) -> None:
self.dish_repo = dish_repo
self.cache_client = RedisRepository(redis_client)
self.background_tasks = background_tasks
async def read_dishes(self, menu_id: UUID, submenu_id: UUID):
async def read_dishes(self, menu_id: UUID, submenu_id: UUID) -> list[Dish]:
data = await self.dish_repo.get_dishes(menu_id, submenu_id)
response = []
for row in data:
dish = row.__dict__
dish['price'] = str(dish['price'])
response.append(dish)
return data
response.append(Dish(**dish))
return response
async def create_dish(
self,
menu_id: UUID,
submenu_id: UUID,
dish_data: DishBase,
):
) -> Dish:
dish = Dish_db(**dish_data.model_dump())
data = await self.dish_repo.create_dish_item(
menu_id,
@ -43,30 +43,31 @@ class DishService:
)
response = data.__dict__
response['price'] = str(response['price'])
return response
return Dish(**response)
async def read_dish(self, menu_id: UUID, submenu_id: UUID, dish_id: UUID):
async def read_dish(
self, menu_id: UUID, submenu_id: UUID, dish_id: UUID
) -> Dish | None:
data = await self.dish_repo.get_dish_item(menu_id, submenu_id, dish_id)
if data is None:
return
return None
response = data.__dict__
response['price'] = str(response['price'])
return response
return Dish(**response)
async def update_dish(
self, menu_id: UUID, submenu_id: UUID, dish_id, dish_data: DishBase
):
) -> Dish:
dish = Dish_db(**dish_data.model_dump())
data = await self.dish_repo.update_dish_item(menu_id, submenu_id, dish_id, dish)
response = data.__dict__
response['price'] = str(response['price'])
return response
return Dish(**response)
async def del_dish(self, menu_id: UUID, submenu_id: UUID, dish_id: UUID):
data = await self.dish_repo.delete_dish_item(
async def del_dish(self, menu_id: UUID, submenu_id: UUID, dish_id: UUID) -> int:
response = await self.dish_repo.delete_dish_item(
menu_id,
submenu_id,
dish_id,
)
return data
return response

View File

@ -6,7 +6,7 @@ from fastapi import BackgroundTasks, Depends
from fastfood.dbase import get_async_redis_client
from fastfood.repository.menu import MenuRepository
from fastfood.repository.redis import RedisRepository
from fastfood.schemas import MenuBase
from fastfood.schemas import MenuBase, MenuRead
class MenuService:
@ -15,27 +15,68 @@ class MenuService:
menu_repo: MenuRepository = Depends(),
redis_client: redis.Redis = Depends(get_async_redis_client),
background_tasks: BackgroundTasks = None,
):
) -> None:
self.menu_repo = menu_repo
self.cache_client = RedisRepository(redis_client)
self.background_tasks = background_tasks
async def read_menus(self):
async def read_menus(self) -> list[MenuRead]:
data = await self.menu_repo.get_menus()
return data
menus = []
for r in data:
menu = r.__dict__
menu = {k: v for k, v in menu.items() if not k.startswith('_')}
dishes_conter = 0
for sub in r.submenus:
dishes_conter += len(sub.dishes)
async def create_menu(self, menu_data: MenuBase):
menu['submenus_count'] = len(menu.pop('submenus'))
menu['dishes_count'] = dishes_conter
menu = MenuRead(**menu)
menus.append(menu)
return menus
async def create_menu(self, menu_data: MenuBase) -> MenuRead:
data = await self.menu_repo.create_menu_item(menu_data)
return data
menu = data.__dict__
menu = {k: v for k, v in menu.items() if not k.startswith('_')}
dishes_conter = 0
async def read_menu(self, menu_id: UUID):
for sub in data.submenus:
dishes_conter += len(sub.dishes)
menu['submenus_count'] = len(menu.pop('submenus'))
menu['dishes_count'] = dishes_conter
return MenuRead(**menu)
async def read_menu(self, menu_id: UUID) -> MenuRead | None:
data = await self.menu_repo.get_menu_item(menu_id)
return data
if data is None:
return None
menu = data.__dict__
menu = {k: v for k, v in menu.items() if not k.startswith('_')}
dishes_conter = 0
async def update_menu(self, menu_id: UUID, menu_data):
for sub in data.submenus:
dishes_conter += len(sub.dishes)
menu['submenus_count'] = len(menu.pop('submenus'))
menu['dishes_count'] = dishes_conter
return MenuRead(**menu)
async def update_menu(self, menu_id: UUID, menu_data) -> MenuRead:
data = await self.menu_repo.update_menu_item(menu_id, menu_data)
return data
menu = data.__dict__
menu = {k: v for k, v in menu.items() if not k.startswith('_')}
dishes_conter = 0
async def del_menu(self, menu_id: UUID):
for sub in data.submenus:
dishes_conter += len(sub.dishes)
menu['submenus_count'] = len(menu.pop('submenus'))
menu['dishes_count'] = dishes_conter
return MenuRead(**menu)
async def del_menu(self, menu_id: UUID) -> int:
data = await self.menu_repo.delete_menu_item(menu_id)
return data

View File

@ -6,7 +6,7 @@ from fastapi import BackgroundTasks, Depends
from fastfood.dbase import get_async_redis_client
from fastfood.repository.redis import RedisRepository
from fastfood.repository.submenu import SubMenuRepository
from fastfood.schemas import MenuBase
from fastfood.schemas import MenuBase, SubMenuRead
class SubmenuService:
@ -15,34 +15,58 @@ class SubmenuService:
submenu_repo: SubMenuRepository = Depends(),
redis_client: redis.Redis = Depends(get_async_redis_client),
background_tasks: BackgroundTasks = None,
):
) -> None:
self.submenu_repo = submenu_repo
self.cache_client = RedisRepository(redis_client)
self.background_tasks = background_tasks
async def read_submenus(self, menu_id: UUID):
async def read_submenus(self, menu_id: UUID) -> list[SubMenuRead]:
data = await self.submenu_repo.get_submenus(menu_id=menu_id)
return data
submenus = []
for r in data:
submenu = r.__dict__
subq = await self.submenu_repo.get_submenu_item(menu_id, r.id)
if subq is not None:
submenu['dishes_count'] = len(subq.dishes)
submenu = SubMenuRead(**submenu)
submenus.append(submenu)
return submenus
async def create_submenu(self, menu_id: UUID, submenu_data: MenuBase):
async def create_submenu(
self, menu_id: UUID, submenu_data: MenuBase
) -> SubMenuRead:
data = await self.submenu_repo.create_submenu_item(
menu_id,
submenu_data,
)
return data
menu = data.__dict__
menu = {k: v for k, v in menu.items() if not k.startswith('_')}
menu['dishes_count'] = len(menu.pop('dishes'))
menu = SubMenuRead(**menu)
return menu
async def read_menu(self, menu_id: UUID, submenu_id: UUID):
async def read_menu(self, menu_id: UUID, submenu_id: UUID) -> SubMenuRead | None:
data = await self.submenu_repo.get_submenu_item(menu_id, submenu_id)
return data
if data is None:
return None
menu = data.__dict__
menu = {k: v for k, v in menu.items() if not k.startswith('_')}
menu['dishes_count'] = len(menu.pop('dishes'))
menu = SubMenuRead(**menu)
return menu
async def update_submenu(
self, menu_id: UUID, submenu_id: UUID, submenu_data: MenuBase
):
) -> SubMenuRead:
data = await self.submenu_repo.update_submenu_item(
menu_id, submenu_id, submenu_data
)
return data
menu = data.__dict__
menu = {k: v for k, v in menu.items() if not k.startswith('_')}
menu['dishes_count'] = len(menu.pop('dishes'))
menu = SubMenuRead(**menu)
return menu
async def del_menu(self, menu_id: UUID, submenu_id: UUID):
data = await self.submenu_repo.delete_submenu_item(menu_id, submenu_id)
return data
async def del_menu(self, menu_id: UUID, submenu_id: UUID) -> int:
return await self.submenu_repo.delete_submenu_item(menu_id, submenu_id)