[preset] fix
This commit is contained in:
parent
644c6a4e27
commit
890829ad20
4 changed files with 46 additions and 1 deletions
|
@ -47,6 +47,19 @@ class SaveMeal(AuthorizedController):
|
|||
raise HTTPException(status_code=400, detail=e.args[0])
|
||||
|
||||
|
||||
class DeleteMeal(AuthorizedController):
|
||||
async def call(self, meal_id: id) -> None:
|
||||
async with self.async_session.begin() as session:
|
||||
meal = await DBMeal.get_by_id(session, self.user.id, meal_id)
|
||||
if meal is None:
|
||||
raise HTTPException(status_code=404, detail="meal not found")
|
||||
|
||||
try:
|
||||
await meal.delete(session)
|
||||
except AssertionError as e:
|
||||
raise HTTPException(status_code=400, detail=e.args[0])
|
||||
|
||||
|
||||
class CreateMealFromPreset(AuthorizedController):
|
||||
async def call(self, content: CreateMealFromPresetPayload) -> Meal:
|
||||
async with self.async_session.begin() as session:
|
||||
|
|
|
@ -142,3 +142,10 @@ class Meal(Base, CommonMixin):
|
|||
.options(joinedload(cls.entries))
|
||||
)
|
||||
return await session.scalar(query.order_by(cls.id))
|
||||
|
||||
async def delete(self, session: AsyncSession) -> None:
|
||||
"""delete."""
|
||||
for entry in self.entries:
|
||||
await session.delete(entry)
|
||||
await session.delete(self)
|
||||
await session.flush()
|
||||
|
|
|
@ -25,6 +25,22 @@ def test_diary_add_meal(client, meal_payload_factory):
|
|||
assert response.status_code == 200, response.json()
|
||||
|
||||
|
||||
@pytest.mark.dependency(depends=["test_diary_add_meal"])
|
||||
def test_diary_delete_meal(client):
|
||||
today = datetime.date.today().isoformat()
|
||||
response = client.get("diary", params={"date": today})
|
||||
|
||||
meals_amount = len(response.json()["meals"])
|
||||
meal_id = response.json()["meals"][0]["id"]
|
||||
|
||||
response = client.delete(f"meal/{meal_id}")
|
||||
assert response.status_code == 200, response.json()
|
||||
|
||||
response = client.get("diary", params={"date": today})
|
||||
assert response.status_code == 200, response.json()
|
||||
assert len(response.json()["meals"]) == meals_amount - 1
|
||||
|
||||
|
||||
@pytest.mark.dependency(depends=["test_get_diary"])
|
||||
def test_diary_add_entry(client, product_payload_factory, entry_payload_factory):
|
||||
today = datetime.date.today().isoformat()
|
||||
|
|
|
@ -6,7 +6,7 @@ from ..model.meal import (
|
|||
SaveMealPayload,
|
||||
CreateMealFromPresetPayload,
|
||||
)
|
||||
from ..controller.meal import CreateMeal, SaveMeal, CreateMealFromPreset
|
||||
from ..controller.meal import CreateMeal, SaveMeal, CreateMealFromPreset, DeleteMeal
|
||||
|
||||
|
||||
router = APIRouter(tags=["meal"])
|
||||
|
@ -31,6 +31,15 @@ async def save_meal(
|
|||
return await contoller.call(meal_id, data)
|
||||
|
||||
|
||||
@router.delete("/{meal_id}")
|
||||
async def delete_meal(
|
||||
request: Request,
|
||||
meal_id: int,
|
||||
contoller: DeleteMeal = Depends(DeleteMeal),
|
||||
):
|
||||
return await contoller.call(meal_id)
|
||||
|
||||
|
||||
@router.post("/from_preset", response_model=Meal)
|
||||
async def create_meal_from_preset(
|
||||
request: Request,
|
||||
|
|
Loading…
Reference in a new issue