diff --git a/fooder/controller/meal.py b/fooder/controller/meal.py index c0e8132..dafee32 100644 --- a/fooder/controller/meal.py +++ b/fooder/controller/meal.py @@ -22,9 +22,7 @@ class CreateMeal(AuthorizedController): raise HTTPException(status_code=404, detail="not found") try: - meal = await DBMeal.create( - session, content.diary_id, content.order, content.name - ) + meal = await DBMeal.create(session, content.diary_id, content.name) return Meal.from_orm(meal) except AssertionError as e: raise HTTPException(status_code=400, detail=e.args[0]) @@ -75,7 +73,7 @@ class CreateMealFromPreset(AuthorizedController): try: meal = await DBMeal.create_from_preset( - session, content.diary_id, content.order, content.name, preset + session, content.diary_id, content.name, preset ) return Meal.from_orm(meal) except AssertionError as e: diff --git a/fooder/domain/meal.py b/fooder/domain/meal.py index ab541ae..7db038d 100644 --- a/fooder/domain/meal.py +++ b/fooder/domain/meal.py @@ -65,13 +65,14 @@ class Meal(Base, CommonMixin): cls, session: AsyncSession, diary_id: int, - order: int = 0, name: Optional[str] = None, ) -> "Meal": # check if order already exists in diary - query = select(cls).where(cls.diary_id == diary_id).where(cls.order == order) + query = ( + select(cls.order).where(cls.diary_id == diary_id).order_by(cls.order.desc()) + ) existing_meal = await session.scalar(query) - assert existing_meal is None, "order already exists in diary" + order = existing_meal + 1 if existing_meal else 1 if name is None: name = f"Meal {order}" @@ -93,14 +94,15 @@ class Meal(Base, CommonMixin): cls, session: AsyncSession, diary_id: int, - order: int, name: Optional[str], preset: Preset, ) -> "Meal": # check if order already exists in diary - query = select(cls).where(cls.diary_id == diary_id).where(cls.order == order) + query = ( + select(cls.order).where(cls.diary_id == diary_id).order_by(cls.order.desc()) + ) existing_meal = await session.scalar(query) - assert existing_meal is None, "order already exists in diary" + order = existing_meal + 1 if existing_meal else 1 if name is None: name = preset.name or f"Meal {order}" diff --git a/fooder/domain/product.py b/fooder/domain/product.py index ac700c9..4b50e8b 100644 --- a/fooder/domain/product.py +++ b/fooder/domain/product.py @@ -33,9 +33,7 @@ class Product(Base, CommonMixin): if q: q_list = q.split() for qq in q_list: - query = query.filter( - cls.name.ilike(f"%{qq.lower()}%") - ) + query = query.filter(cls.name.ilike(f"%{qq.lower()}%")) query = query.offset(offset).limit(limit) stream = await session.stream_scalars(query.order_by(cls.id)) diff --git a/fooder/model/meal.py b/fooder/model/meal.py index 739db4f..d9e1b13 100644 --- a/fooder/model/meal.py +++ b/fooder/model/meal.py @@ -25,7 +25,6 @@ class CreateMealPayload(BaseModel): """CreateMealPayload.""" name: Optional[str] - order: int diary_id: int @@ -39,6 +38,5 @@ class CreateMealFromPresetPayload(BaseModel): """CreateMealPayload.""" name: Optional[str] - order: int diary_id: int preset_id: int diff --git a/requirements_local.txt b/requirements_local.txt index 80f1e13..3a8626f 100644 --- a/requirements_local.txt +++ b/requirements_local.txt @@ -6,3 +6,4 @@ uvicorn[standard] python-jose[cryptography] passlib[bcrypt] fastapi-users +pytest diff --git a/test.sh b/test.sh index 2e09a40..65ed4d7 100755 --- a/test.sh +++ b/test.sh @@ -54,7 +54,7 @@ ${DC} exec api bash -c "python -m fooder --create-tables" # finally run tests set -xe -pytest fooder -sv -k "${TESTS}" +python -m pytest fooder -sv -k "${TESTS}" # clean up after tests echo "cleaning up..."