2023-04-01 16:19:12 +02:00
|
|
|
from fastapi import HTTPException
|
|
|
|
|
2023-10-27 15:12:48 +02:00
|
|
|
from ..model.meal import (
|
|
|
|
Meal,
|
|
|
|
CreateMealPayload,
|
|
|
|
SaveMealPayload,
|
|
|
|
CreateMealFromPresetPayload,
|
|
|
|
)
|
2023-10-27 16:17:10 +02:00
|
|
|
from ..model.preset import Preset
|
2023-04-01 16:19:12 +02:00
|
|
|
from ..domain.meal import Meal as DBMeal
|
2023-04-03 13:46:03 +02:00
|
|
|
from ..domain.diary import Diary as DBDiary
|
2023-10-27 15:12:48 +02:00
|
|
|
from ..domain.preset import Preset as DBPreset
|
2023-04-01 16:19:12 +02:00
|
|
|
from .base import AuthorizedController
|
|
|
|
|
|
|
|
|
|
|
|
class CreateMeal(AuthorizedController):
|
|
|
|
async def call(self, content: CreateMealPayload) -> Meal:
|
|
|
|
async with self.async_session.begin() as session:
|
2023-04-03 13:46:03 +02:00
|
|
|
if not await DBDiary.has_permission(
|
|
|
|
session, self.user.id, content.diary_id
|
|
|
|
):
|
|
|
|
raise HTTPException(status_code=404, detail="not found")
|
|
|
|
|
2023-04-01 16:19:12 +02:00
|
|
|
try:
|
2024-03-19 16:40:17 +01:00
|
|
|
meal = await DBMeal.create(session, content.diary_id, content.name)
|
2023-04-01 16:19:12 +02:00
|
|
|
return Meal.from_orm(meal)
|
|
|
|
except AssertionError as e:
|
|
|
|
raise HTTPException(status_code=400, detail=e.args[0])
|
2023-10-27 15:12:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
class SaveMeal(AuthorizedController):
|
2024-05-21 15:18:03 +02:00
|
|
|
async def call(self, meal_id: int, payload: SaveMealPayload) -> Preset:
|
2023-10-27 15:12:48 +02:00
|
|
|
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:
|
2023-10-27 16:17:10 +02:00
|
|
|
return Preset.from_orm(
|
|
|
|
await DBPreset.create(
|
2024-05-21 15:18:03 +02:00
|
|
|
session,
|
|
|
|
user_id=self.user.id,
|
|
|
|
name=payload.name or meal.name,
|
|
|
|
meal=meal,
|
2023-10-27 16:17:10 +02:00
|
|
|
)
|
2023-10-27 15:12:48 +02:00
|
|
|
)
|
|
|
|
except AssertionError as e:
|
|
|
|
raise HTTPException(status_code=400, detail=e.args[0])
|
|
|
|
|
|
|
|
|
2023-10-27 16:39:26 +02:00
|
|
|
class DeleteMeal(AuthorizedController):
|
2024-05-21 15:18:03 +02:00
|
|
|
async def call(self, meal_id: int) -> None:
|
2023-10-27 16:39:26 +02:00
|
|
|
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])
|
|
|
|
|
|
|
|
|
2023-10-27 15:12:48 +02:00
|
|
|
class CreateMealFromPreset(AuthorizedController):
|
|
|
|
async def call(self, content: CreateMealFromPresetPayload) -> Meal:
|
|
|
|
async with self.async_session.begin() as session:
|
|
|
|
if not await DBDiary.has_permission(
|
|
|
|
session, self.user.id, content.diary_id
|
|
|
|
):
|
|
|
|
raise HTTPException(status_code=404, detail="diary not found")
|
|
|
|
|
|
|
|
preset = await DBPreset.get(session, self.user.id, content.preset_id)
|
|
|
|
|
|
|
|
if preset is None:
|
|
|
|
raise HTTPException(status_code=404, detail="preset not found")
|
|
|
|
|
|
|
|
try:
|
|
|
|
meal = await DBMeal.create_from_preset(
|
2024-03-19 16:40:17 +01:00
|
|
|
session, content.diary_id, content.name, preset
|
2023-10-27 15:12:48 +02:00
|
|
|
)
|
|
|
|
return Meal.from_orm(meal)
|
|
|
|
except AssertionError as e:
|
|
|
|
raise HTTPException(status_code=400, detail=e.args[0])
|