fix: Поправил pydantic валидацию, убрал костыль
parent
45dd8dc73e
commit
2afba14e44
|
@ -6,7 +6,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
from fastfood import models
|
from fastfood import models
|
||||||
from fastfood.dbase import get_async_session
|
from fastfood.dbase import get_async_session
|
||||||
from fastfood.schemas import DishBase
|
from fastfood.schemas import Dish_db
|
||||||
|
|
||||||
|
|
||||||
class DishRepository:
|
class DishRepository:
|
||||||
|
@ -24,13 +24,13 @@ class DishRepository:
|
||||||
self,
|
self,
|
||||||
menu_id: UUID,
|
menu_id: UUID,
|
||||||
submenu_id: UUID,
|
submenu_id: UUID,
|
||||||
dish_data: DishBase,
|
dish_data: Dish_db,
|
||||||
):
|
):
|
||||||
new_dish = models.Dish(**dish_data.model_dump())
|
new_dish = models.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.flush()
|
|
||||||
await self.db.commit()
|
await self.db.commit()
|
||||||
|
await self.db.refresh(new_dish)
|
||||||
return new_dish
|
return new_dish
|
||||||
|
|
||||||
async def get_dish_item(
|
async def get_dish_item(
|
||||||
|
@ -48,7 +48,7 @@ class DishRepository:
|
||||||
menu_id: UUID,
|
menu_id: UUID,
|
||||||
submenu_id: UUID,
|
submenu_id: UUID,
|
||||||
dish_id: UUID,
|
dish_id: UUID,
|
||||||
dish_data: DishBase,
|
dish_data: Dish_db,
|
||||||
):
|
):
|
||||||
query = (
|
query = (
|
||||||
update(models.Dish)
|
update(models.Dish)
|
||||||
|
|
|
@ -2,9 +2,8 @@ from uuid import UUID
|
||||||
|
|
||||||
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException
|
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException
|
||||||
|
|
||||||
from fastfood import schemas
|
from fastfood.schemas import Dish, DishBase
|
||||||
from fastfood.service.dish import DishService
|
from fastfood.service.dish import DishService
|
||||||
from fastfood.utils import price_converter
|
|
||||||
|
|
||||||
router = APIRouter(
|
router = APIRouter(
|
||||||
prefix='/api/v1/menus/{menu_id}/submenus/{submenu_id}/dishes',
|
prefix='/api/v1/menus/{menu_id}/submenus/{submenu_id}/dishes',
|
||||||
|
@ -12,7 +11,7 @@ router = APIRouter(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.get('/')
|
@router.get('/', response_model=list[Dish])
|
||||||
async def get_dishes(
|
async def get_dishes(
|
||||||
menu_id: UUID,
|
menu_id: UUID,
|
||||||
submenu_id: UUID,
|
submenu_id: UUID,
|
||||||
|
@ -23,23 +22,22 @@ async def get_dishes(
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
@router.post('/', status_code=201)
|
@router.post('/', status_code=201, response_model=Dish)
|
||||||
async def create_dish(
|
async def create_dish(
|
||||||
menu_id: UUID,
|
menu_id: UUID,
|
||||||
submenu_id: UUID,
|
submenu_id: UUID,
|
||||||
dish_data: schemas.DishBase,
|
dish_data: DishBase,
|
||||||
dish: DishService = Depends(),
|
dish: DishService = Depends(),
|
||||||
background_tasks: BackgroundTasks = BackgroundTasks(),
|
background_tasks: BackgroundTasks = BackgroundTasks(),
|
||||||
):
|
):
|
||||||
result = await dish.create_dish(
|
return await dish.create_dish(
|
||||||
menu_id,
|
menu_id,
|
||||||
submenu_id,
|
submenu_id,
|
||||||
dish_data,
|
dish_data,
|
||||||
)
|
)
|
||||||
return price_converter(result)
|
|
||||||
|
|
||||||
|
|
||||||
@router.get('/{dish_id}')
|
@router.get('/{dish_id}', response_model=Dish)
|
||||||
async def get_dish(
|
async def get_dish(
|
||||||
menu_id: UUID,
|
menu_id: UUID,
|
||||||
submenu_id: UUID,
|
submenu_id: UUID,
|
||||||
|
@ -54,15 +52,15 @@ async def get_dish(
|
||||||
)
|
)
|
||||||
if not result:
|
if not result:
|
||||||
raise HTTPException(status_code=404, detail='dish not found')
|
raise HTTPException(status_code=404, detail='dish not found')
|
||||||
return price_converter(result)
|
return result
|
||||||
|
|
||||||
|
|
||||||
@router.patch('/{dish_id}')
|
@router.patch('/{dish_id}', response_model=Dish)
|
||||||
async def update_dish(
|
async def update_dish(
|
||||||
menu_id: UUID,
|
menu_id: UUID,
|
||||||
submenu_id: UUID,
|
submenu_id: UUID,
|
||||||
dish_id: UUID,
|
dish_id: UUID,
|
||||||
dish_data: schemas.DishBase,
|
dish_data: DishBase,
|
||||||
dish: DishService = Depends(),
|
dish: DishService = Depends(),
|
||||||
background_tasks: BackgroundTasks = BackgroundTasks(),
|
background_tasks: BackgroundTasks = BackgroundTasks(),
|
||||||
):
|
):
|
||||||
|
@ -72,7 +70,7 @@ async def update_dish(
|
||||||
dish_id,
|
dish_id,
|
||||||
dish_data,
|
dish_data,
|
||||||
)
|
)
|
||||||
return price_converter(result)
|
return result
|
||||||
|
|
||||||
|
|
||||||
@router.delete('/{dish_id}')
|
@router.delete('/{dish_id}')
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
from typing import Optional
|
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException
|
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException
|
||||||
|
@ -12,7 +11,7 @@ router = APIRouter(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.get('/', response_model=Optional[list[Menu]])
|
@router.get('/', response_model=list[Menu])
|
||||||
async def get_menus(
|
async def get_menus(
|
||||||
menu: MenuService = Depends(),
|
menu: MenuService = Depends(),
|
||||||
background_tasks: BackgroundTasks = BackgroundTasks(),
|
background_tasks: BackgroundTasks = BackgroundTasks(),
|
||||||
|
|
|
@ -25,8 +25,12 @@ class SubMenuRead(Menu):
|
||||||
|
|
||||||
|
|
||||||
class DishBase(MenuBase):
|
class DishBase(MenuBase):
|
||||||
price: float
|
price: str
|
||||||
|
|
||||||
|
|
||||||
class Dish(DishBase, Menu):
|
class Dish(DishBase, Menu):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class Dish_db(MenuBase):
|
||||||
|
price: float
|
||||||
|
|
|
@ -6,7 +6,7 @@ from fastapi import BackgroundTasks, Depends
|
||||||
from fastfood.dbase import get_async_redis_client
|
from fastfood.dbase import get_async_redis_client
|
||||||
from fastfood.repository.dish import DishRepository
|
from fastfood.repository.dish import DishRepository
|
||||||
from fastfood.repository.redis import RedisRepository
|
from fastfood.repository.redis import RedisRepository
|
||||||
from fastfood.schemas import DishBase
|
from fastfood.schemas import Dish_db, DishBase
|
||||||
|
|
||||||
|
|
||||||
class DishService:
|
class DishService:
|
||||||
|
@ -22,6 +22,11 @@ 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)
|
||||||
|
response = []
|
||||||
|
for row in data:
|
||||||
|
dish = row.__dict__
|
||||||
|
dish['price'] = str(dish['price'])
|
||||||
|
response.append(dish)
|
||||||
return data
|
return data
|
||||||
|
|
||||||
async def create_dish(
|
async def create_dish(
|
||||||
|
@ -30,24 +35,33 @@ class DishService:
|
||||||
submenu_id: UUID,
|
submenu_id: UUID,
|
||||||
dish_data: DishBase,
|
dish_data: DishBase,
|
||||||
):
|
):
|
||||||
|
dish = Dish_db(**dish_data.model_dump())
|
||||||
data = await self.dish_repo.create_dish_item(
|
data = await self.dish_repo.create_dish_item(
|
||||||
menu_id,
|
menu_id,
|
||||||
submenu_id,
|
submenu_id,
|
||||||
dish_data,
|
dish,
|
||||||
)
|
)
|
||||||
return data
|
response = data.__dict__
|
||||||
|
response['price'] = str(response['price'])
|
||||||
|
return 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):
|
||||||
data = await self.dish_repo.get_dish_item(menu_id, submenu_id, dish_id)
|
data = await self.dish_repo.get_dish_item(menu_id, submenu_id, dish_id)
|
||||||
return data
|
if data is None:
|
||||||
|
return
|
||||||
|
response = data.__dict__
|
||||||
|
response['price'] = str(response['price'])
|
||||||
|
|
||||||
|
return response
|
||||||
|
|
||||||
async def update_dish(
|
async def update_dish(
|
||||||
self, menu_id: UUID, submenu_id: UUID, dish_id, dish_data: DishBase
|
self, menu_id: UUID, submenu_id: UUID, dish_id, dish_data: DishBase
|
||||||
):
|
):
|
||||||
data = await self.dish_repo.update_dish_item(
|
dish = Dish_db(**dish_data.model_dump())
|
||||||
menu_id, submenu_id, dish_id, dish_data
|
data = await self.dish_repo.update_dish_item(menu_id, submenu_id, dish_id, dish)
|
||||||
)
|
response = data.__dict__
|
||||||
return data
|
response['price'] = str(response['price'])
|
||||||
|
return response
|
||||||
|
|
||||||
async def del_dish(self, menu_id: UUID, submenu_id: UUID, dish_id: UUID):
|
async def del_dish(self, menu_id: UUID, submenu_id: UUID, dish_id: UUID):
|
||||||
data = await self.dish_repo.delete_dish_item(
|
data = await self.dish_repo.delete_dish_item(
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
def price_converter(dish: dict) -> dict:
|
|
||||||
dish['price'] = str(dish['price'])
|
|
||||||
return dish
|
|
Loading…
Reference in New Issue