[delete] radom jazda
This commit is contained in:
parent
890829ad20
commit
bec52975ae
4 changed files with 68 additions and 3 deletions
|
@ -1,6 +1,7 @@
|
||||||
from typing import AsyncIterator, Optional
|
from typing import AsyncIterator, Optional
|
||||||
|
from fastapi import HTTPException
|
||||||
|
|
||||||
from ..model.preset import Preset
|
from ..model.preset import Preset, PresetDetails
|
||||||
from ..domain.preset import Preset as DBPreset
|
from ..domain.preset import Preset as DBPreset
|
||||||
from .base import AuthorizedController
|
from .base import AuthorizedController
|
||||||
|
|
||||||
|
@ -14,3 +15,28 @@ class ListPresets(AuthorizedController):
|
||||||
session, limit=limit, offset=offset, q=q
|
session, limit=limit, offset=offset, q=q
|
||||||
):
|
):
|
||||||
yield Preset.from_orm(preset)
|
yield Preset.from_orm(preset)
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
|
@ -104,3 +104,9 @@ class Preset(Base, CommonMixin):
|
||||||
.options(joinedload(cls.entries).joinedload(PresetEntry.product))
|
.options(joinedload(cls.entries).joinedload(PresetEntry.product))
|
||||||
)
|
)
|
||||||
return await session.scalar(query)
|
return await session.scalar(query)
|
||||||
|
|
||||||
|
async def delete(self, session: AsyncSession) -> None:
|
||||||
|
for entry in self.entries:
|
||||||
|
await session.delete(entry)
|
||||||
|
await session.delete(self)
|
||||||
|
await session.flush()
|
||||||
|
|
|
@ -86,3 +86,18 @@ def test_create_meal_from_preset(client, meal_from_preset):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
assert meal[k] == v, f"{k} != {v}"
|
assert meal[k] == v, f"{k} != {v}"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.dependency(depends=["test_list_presets"])
|
||||||
|
def test_delete_preset(client):
|
||||||
|
presets = client.get("preset").json()["presets"]
|
||||||
|
preset_id = presets[0]["id"]
|
||||||
|
|
||||||
|
response = client.get(f"preset/{preset_id}")
|
||||||
|
assert response.status_code == 200, response.json()
|
||||||
|
|
||||||
|
response = client.delete(f"preset/{preset_id}")
|
||||||
|
assert response.status_code == 200, response.json()
|
||||||
|
|
||||||
|
response = client.get(f"preset/{preset_id}")
|
||||||
|
assert response.status_code == 404, response.json()
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
from fastapi import APIRouter, Depends, Request
|
from fastapi import APIRouter, Depends, Request
|
||||||
from ..model.preset import ListPresetsPayload
|
from ..model.preset import ListPresetsPayload, PresetDetails
|
||||||
from ..controller.preset import ListPresets
|
from ..controller.preset import ListPresets, DeletePreset, GetPreset
|
||||||
|
|
||||||
|
|
||||||
router = APIRouter(tags=["preset"])
|
router = APIRouter(tags=["preset"])
|
||||||
|
@ -17,3 +17,21 @@ async def list_presets(
|
||||||
return ListPresetsPayload(
|
return ListPresetsPayload(
|
||||||
presets=[p async for p in controller.call(limit=limit, offset=offset, q=q)]
|
presets=[p async for p in controller.call(limit=limit, offset=offset, q=q)]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/{preset_id}", response_model=PresetDetails)
|
||||||
|
async def get_preset(
|
||||||
|
request: Request,
|
||||||
|
preset_id: int,
|
||||||
|
controller: GetPreset = Depends(GetPreset),
|
||||||
|
):
|
||||||
|
return await controller.call(preset_id)
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/{preset_id}")
|
||||||
|
async def delete_preset(
|
||||||
|
request: Request,
|
||||||
|
preset_id: int,
|
||||||
|
controller: DeletePreset = Depends(DeletePreset),
|
||||||
|
):
|
||||||
|
await controller.call(preset_id)
|
||||||
|
|
Loading…
Reference in a new issue