Skip to content

Commit

Permalink
⚡️(api) return the number of created items for dynamique bulk requests
Browse files Browse the repository at this point in the history
This is a relevant information when using the API with the client or the
CLI.
  • Loading branch information
jmaupetit committed Jun 14, 2024
1 parent 349fbe0 commit 3074249
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ and this project adheres to

## [Unreleased]

### Changed

- API dynamique bulk requests now returns the number of created items

## [0.9.0] - 2024-06-11

### Added
Expand Down
16 changes: 13 additions & 3 deletions src/api/qualicharge/api/v1/routers/dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from annotated_types import Len
from fastapi import APIRouter, Depends, HTTPException, Path, Query, Security
from fastapi import status as fa_status
from pydantic import PastDatetime, StringConstraints
from pydantic import BaseModel, PastDatetime, StringConstraints
from sqlalchemy import func
from sqlalchemy.schema import Column as SAColumn
from sqlmodel import Session, join, select
Expand Down Expand Up @@ -44,6 +44,12 @@
]


class DynamiqueItemsCreatedResponse(BaseModel):
"""API response model used when dynamic items are created."""

size: int


@router.get("/status/", tags=["Status"])
async def list_statuses(
user: Annotated[User, Security(get_user, scopes=[ScopesEnum.DYNAMIC_READ.value])],
Expand Down Expand Up @@ -319,7 +325,7 @@ async def create_status_bulk(
user: Annotated[User, Security(get_user, scopes=[ScopesEnum.DYNAMIC_CREATE.value])],
statuses: BulkStatusCreateList,
session: Session = Depends(get_session),
) -> None:
) -> DynamiqueItemsCreatedResponse:
"""Create a statuses batch."""
for status in statuses:
if not is_pdc_allowed_for_user(status.id_pdc_itinerance, user):
Expand Down Expand Up @@ -359,6 +365,8 @@ async def create_status_bulk(
session.add_all(db_statuses)
session.commit()

return DynamiqueItemsCreatedResponse(size=len(db_statuses))


@router.post("/session/", status_code=fa_status.HTTP_201_CREATED, tags=["Session"])
async def create_session(
Expand Down Expand Up @@ -395,7 +403,7 @@ async def create_session_bulk(
user: Annotated[User, Security(get_user, scopes=[ScopesEnum.DYNAMIC_CREATE.value])],
sessions: BulkSessionCreateList,
db_session: Session = Depends(get_session),
) -> None:
) -> DynamiqueItemsCreatedResponse:
"""Create a sessions batch."""
for session in sessions:
if not is_pdc_allowed_for_user(session.id_pdc_itinerance, user):
Expand Down Expand Up @@ -433,3 +441,5 @@ async def create_session_bulk(
db_qc_sessions.append(db_qc_session)
db_session.add_all(db_qc_sessions)
db_session.commit()

return DynamiqueItemsCreatedResponse(size=len(db_qc_sessions))
8 changes: 4 additions & 4 deletions src/api/tests/api/v1/routers/test_dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ def test_create_status_bulk_for_superuser(db_session, client_auth):
json=[json.loads(s.model_dump_json()) for s in qc_statuses],
)
assert response.status_code == status.HTTP_201_CREATED
assert response.json() is None
assert response.json() == {"size": 3}

# Check created statuses
db_statuses = db_session.exec(select(Status)).all()
Expand Down Expand Up @@ -1023,7 +1023,7 @@ def test_create_status_bulk_for_user(db_session, client_auth):
json=[json.loads(s.model_dump_json()) for s in qc_statuses],
)
assert response.status_code == status.HTTP_201_CREATED
assert response.json() is None
assert response.json() == {"size": 3}

# Check created statuses
db_statuses = db_session.exec(select(Status)).all()
Expand Down Expand Up @@ -1280,7 +1280,7 @@ def test_create_session_bulk_for_superuser(db_session, client_auth):
json=[json.loads(s.model_dump_json()) for s in qc_sessions],
)
assert response.status_code == status.HTTP_201_CREATED
assert response.json() is None
assert response.json() == {"size": 3}

# Check created statuses
db_qc_sessions = db_session.exec(select(Session)).all()
Expand Down Expand Up @@ -1391,7 +1391,7 @@ def test_create_session_bulk_for_user(db_session, client_auth):
json=[json.loads(s.model_dump_json()) for s in qc_sessions],
)
assert response.status_code == status.HTTP_201_CREATED
assert response.json() is None
assert response.json() == {"size": 3}

# Check created statuses
db_qc_sessions = db_session.exec(select(Session)).all()
Expand Down

0 comments on commit 3074249

Please sign in to comment.