fooder-api/fooder/controller/meal.py

25 lines
905 B
Python
Raw Normal View History

2023-04-01 16:19:12 +02:00
from typing import AsyncIterator
from fastapi import HTTPException
from ..model.meal import Meal, CreateMealPayload
from ..domain.meal import Meal as DBMeal
2023-04-03 13:46:03 +02:00
from ..domain.diary import Diary as DBDiary
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:
meal = await DBMeal.create(
session, content.diary_id, content.order, content.name
)
return Meal.from_orm(meal)
except AssertionError as e:
raise HTTPException(status_code=400, detail=e.args[0])