[preset] fix
This commit is contained in:
parent
705baa2857
commit
644c6a4e27
4 changed files with 33 additions and 8 deletions
|
@ -6,6 +6,7 @@ from ..model.meal import (
|
|||
SaveMealPayload,
|
||||
CreateMealFromPresetPayload,
|
||||
)
|
||||
from ..model.preset import Preset
|
||||
from ..domain.meal import Meal as DBMeal
|
||||
from ..domain.diary import Diary as DBDiary
|
||||
from ..domain.preset import Preset as DBPreset
|
||||
|
@ -30,16 +31,18 @@ class CreateMeal(AuthorizedController):
|
|||
|
||||
|
||||
class SaveMeal(AuthorizedController):
|
||||
async def call(self, meal_id: id, payload: SaveMealPayload) -> None:
|
||||
async def call(self, meal_id: id, payload: SaveMealPayload) -> Preset:
|
||||
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:
|
||||
return Preset.from_orm(
|
||||
await DBPreset.create(
|
||||
session, user_id=self.user.id, name=payload.name, meal=meal
|
||||
)
|
||||
)
|
||||
except AssertionError as e:
|
||||
raise HTTPException(status_code=400, detail=e.args[0])
|
||||
|
||||
|
|
|
@ -74,7 +74,9 @@ class Preset(Base, CommonMixin):
|
|||
raise RuntimeError()
|
||||
|
||||
for entry in meal.entries:
|
||||
PresetEntry.create(session, preset.id, entry)
|
||||
await PresetEntry.create(session, preset.id, entry)
|
||||
|
||||
return await cls.get(session, user_id, preset.id)
|
||||
|
||||
@classmethod
|
||||
async def list_all(
|
||||
|
|
|
@ -3,7 +3,9 @@ import pytest
|
|||
|
||||
|
||||
@pytest.mark.dependency()
|
||||
def test_create_meal(client, meal_payload_factory):
|
||||
def test_create_meal(
|
||||
client, meal_payload_factory, product_payload_factory, entry_payload_factory
|
||||
):
|
||||
today = datetime.date.today().isoformat()
|
||||
response = client.get("diary", params={"date": today})
|
||||
|
||||
|
@ -13,18 +15,35 @@ def test_create_meal(client, meal_payload_factory):
|
|||
response = client.post("meal", json=meal_payload_factory(diary_id, meal_order))
|
||||
assert response.status_code == 200, response.json()
|
||||
|
||||
meal_id = response.json()["id"]
|
||||
|
||||
product_id = client.post("product", json=product_payload_factory()).json()["id"]
|
||||
|
||||
entry_payload = entry_payload_factory(meal_id, product_id, 100.0)
|
||||
response = client.post("entry", json=entry_payload)
|
||||
assert response.status_code == 200, response.json()
|
||||
|
||||
|
||||
@pytest.mark.dependency(depends=["test_create_meal"])
|
||||
def test_save_meal(client, meal_save_payload):
|
||||
today = datetime.date.today().isoformat()
|
||||
response = client.get("diary", params={"date": today})
|
||||
|
||||
meal_id = response.json()["meals"][0]["id"]
|
||||
meal = response.json()["meals"][0]
|
||||
meal_id = meal["id"]
|
||||
save_payload = meal_save_payload(meal_id)
|
||||
|
||||
response = client.post(f"meal/{meal_id}/save", json=save_payload)
|
||||
assert response.status_code == 200, response.json()
|
||||
|
||||
preset = response.json()
|
||||
|
||||
for k, v in preset.items():
|
||||
if k in ("id", "name", "entries"):
|
||||
continue
|
||||
|
||||
assert meal[k] == v, f"{k} != {v}"
|
||||
|
||||
|
||||
@pytest.mark.dependency(depends=["test_create_meal"])
|
||||
def test_list_presets(client, meal_save_payload):
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
from fastapi import APIRouter, Depends, Request
|
||||
from ..model.preset import Preset
|
||||
from ..model.meal import (
|
||||
Meal,
|
||||
CreateMealPayload,
|
||||
|
@ -20,14 +21,14 @@ async def create_meal(
|
|||
return await contoller.call(data)
|
||||
|
||||
|
||||
@router.post("/{meal_id}/save")
|
||||
@router.post("/{meal_id}/save", response_model=Preset)
|
||||
async def save_meal(
|
||||
request: Request,
|
||||
meal_id: int,
|
||||
data: SaveMealPayload,
|
||||
contoller: SaveMeal = Depends(SaveMeal),
|
||||
):
|
||||
await contoller.call(meal_id, data)
|
||||
return await contoller.call(meal_id, data)
|
||||
|
||||
|
||||
@router.post("/from_preset", response_model=Meal)
|
||||
|
|
Loading…
Reference in a new issue