[preset] fix

This commit is contained in:
doman 2023-10-27 16:17:10 +02:00
parent 705baa2857
commit 644c6a4e27
4 changed files with 33 additions and 8 deletions

View file

@ -6,6 +6,7 @@ from ..model.meal import (
SaveMealPayload, SaveMealPayload,
CreateMealFromPresetPayload, CreateMealFromPresetPayload,
) )
from ..model.preset import Preset
from ..domain.meal import Meal as DBMeal from ..domain.meal import Meal as DBMeal
from ..domain.diary import Diary as DBDiary from ..domain.diary import Diary as DBDiary
from ..domain.preset import Preset as DBPreset from ..domain.preset import Preset as DBPreset
@ -30,15 +31,17 @@ class CreateMeal(AuthorizedController):
class SaveMeal(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: async with self.async_session.begin() as session:
meal = await DBMeal.get_by_id(session, self.user.id, meal_id) meal = await DBMeal.get_by_id(session, self.user.id, meal_id)
if meal is None: if meal is None:
raise HTTPException(status_code=404, detail="meal not found") raise HTTPException(status_code=404, detail="meal not found")
try: try:
await DBPreset.create( return Preset.from_orm(
session, user_id=self.user.id, name=payload.name, meal=meal await DBPreset.create(
session, user_id=self.user.id, name=payload.name, meal=meal
)
) )
except AssertionError as e: except AssertionError as e:
raise HTTPException(status_code=400, detail=e.args[0]) raise HTTPException(status_code=400, detail=e.args[0])

View file

@ -74,7 +74,9 @@ class Preset(Base, CommonMixin):
raise RuntimeError() raise RuntimeError()
for entry in meal.entries: 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 @classmethod
async def list_all( async def list_all(

View file

@ -3,7 +3,9 @@ import pytest
@pytest.mark.dependency() @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() today = datetime.date.today().isoformat()
response = client.get("diary", params={"date": today}) 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)) response = client.post("meal", json=meal_payload_factory(diary_id, meal_order))
assert response.status_code == 200, response.json() 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"]) @pytest.mark.dependency(depends=["test_create_meal"])
def test_save_meal(client, meal_save_payload): def test_save_meal(client, meal_save_payload):
today = datetime.date.today().isoformat() today = datetime.date.today().isoformat()
response = client.get("diary", params={"date": today}) 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) save_payload = meal_save_payload(meal_id)
response = client.post(f"meal/{meal_id}/save", json=save_payload) response = client.post(f"meal/{meal_id}/save", json=save_payload)
assert response.status_code == 200, response.json() 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"]) @pytest.mark.dependency(depends=["test_create_meal"])
def test_list_presets(client, meal_save_payload): def test_list_presets(client, meal_save_payload):

View file

@ -1,4 +1,5 @@
from fastapi import APIRouter, Depends, Request from fastapi import APIRouter, Depends, Request
from ..model.preset import Preset
from ..model.meal import ( from ..model.meal import (
Meal, Meal,
CreateMealPayload, CreateMealPayload,
@ -20,14 +21,14 @@ async def create_meal(
return await contoller.call(data) return await contoller.call(data)
@router.post("/{meal_id}/save") @router.post("/{meal_id}/save", response_model=Preset)
async def save_meal( async def save_meal(
request: Request, request: Request,
meal_id: int, meal_id: int,
data: SaveMealPayload, data: SaveMealPayload,
contoller: SaveMeal = Depends(SaveMeal), contoller: SaveMeal = Depends(SaveMeal),
): ):
await contoller.call(meal_id, data) return await contoller.call(meal_id, data)
@router.post("/from_preset", response_model=Meal) @router.post("/from_preset", response_model=Meal)