[order] remove param, always add new meal as last

This commit is contained in:
Piotr Domański 2024-03-19 16:40:17 +01:00
parent c858fe2c96
commit 1eabac6d9f
6 changed files with 13 additions and 16 deletions

View file

@ -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:

View file

@ -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}"

View file

@ -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))

View file

@ -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

View file

@ -6,3 +6,4 @@ uvicorn[standard]
python-jose[cryptography]
passlib[bcrypt]
fastapi-users
pytest

View file

@ -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..."