fooder-api/fooder/controller/preset.py

43 lines
1.4 KiB
Python
Raw Normal View History

2023-10-27 15:12:48 +02:00
from typing import AsyncIterator, Optional
2023-10-27 16:49:19 +02:00
from fastapi import HTTPException
2023-10-27 15:12:48 +02:00
2023-10-27 16:49:19 +02:00
from ..model.preset import Preset, PresetDetails
2023-10-27 15:12:48 +02:00
from ..domain.preset import Preset as DBPreset
from .base import AuthorizedController
class ListPresets(AuthorizedController):
async def call(
self, limit: int, offset: int, q: Optional[str]
) -> AsyncIterator[Preset]:
async with self.async_session() as session:
async for preset in DBPreset.list_all(
2023-10-27 17:19:20 +02:00
session, user_id=self.user.id, limit=limit, offset=offset, q=q
2023-10-27 15:12:48 +02:00
):
yield Preset.from_orm(preset)
2023-10-27 16:49:19 +02:00
class GetPreset(AuthorizedController):
async def call(self, id: int) -> PresetDetails:
async with self.async_session() as session:
preset = await DBPreset.get(session, self.user.id, id)
if preset is not None:
return PresetDetails.from_orm(preset)
raise HTTPException(status_code=404, detail="preset not found")
class DeletePreset(AuthorizedController):
async def call(
self,
id: int,
) -> AsyncIterator[Preset]:
async with self.async_session.begin() as session:
preset = await DBPreset.get(session, self.user.id, id)
if preset is None:
raise HTTPException(status_code=404, detail="preset not found")
await preset.delete(session)