🔧 fix(store.py): add type hints to improve code readability and maintainability
🚀 feat(store.py): add support for pagination in get_components endpoint to limit the number of results returned 🔧 fix(store.py): fix count_components endpoint to handle cases where the number of results is less than the limit
This commit is contained in:
parent
564eef87c9
commit
0cd1fcdcbd
1 changed files with 15 additions and 30 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import Any, Dict, List, Optional
|
from typing import Annotated, Any, Dict, List, Optional, Union
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||||
|
|
@ -77,12 +77,12 @@ def create_component(
|
||||||
|
|
||||||
@router.get("/components/", response_model=ListComponentResponseModel)
|
@router.get("/components/", response_model=ListComponentResponseModel)
|
||||||
def get_components(
|
def get_components(
|
||||||
search: Optional[str] = Query(None),
|
search: Annotated[Optional[str], Query()] = None,
|
||||||
status: Optional[str] = Query(None),
|
status: Annotated[Optional[str], Query()] = None,
|
||||||
is_component: Optional[bool] = Query(None),
|
is_component: Annotated[Optional[bool], Query()] = None,
|
||||||
tags: Optional[List[str]] = Query(None),
|
tags: Annotated[Optional[list[str]], Query()] = None,
|
||||||
sort: Optional[List[str]] = Query(None),
|
sort: Annotated[Union[list[str], None], Query()] = None,
|
||||||
filter_by_user: bool = Query(False),
|
filter_by_user: Annotated[bool, Query()] = False,
|
||||||
page: int = 1,
|
page: int = 1,
|
||||||
limit: int = 10,
|
limit: int = 10,
|
||||||
store_service: StoreService = Depends(get_store_service),
|
store_service: StoreService = Depends(get_store_service),
|
||||||
|
|
@ -112,11 +112,14 @@ def get_components(
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
if result:
|
if result:
|
||||||
|
if len(result) >= limit:
|
||||||
comp_count = store_service.count_components(
|
comp_count = store_service.count_components(
|
||||||
api_key=store_api_Key,
|
api_key=store_api_Key,
|
||||||
filter_by_user=filter_by_user,
|
filter_by_user=filter_by_user,
|
||||||
filter_conditions=filter_conditions,
|
filter_conditions=filter_conditions,
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
comp_count = len(result)
|
||||||
else:
|
else:
|
||||||
comp_count = 0
|
comp_count = 0
|
||||||
except HTTPStatusError as exc:
|
except HTTPStatusError as exc:
|
||||||
|
|
@ -150,24 +153,6 @@ def get_components(
|
||||||
raise HTTPException(status_code=500, detail=str(exc))
|
raise HTTPException(status_code=500, detail=str(exc))
|
||||||
|
|
||||||
|
|
||||||
@router.get("/components/count", response_model=dict)
|
|
||||||
def count_components(
|
|
||||||
filter_by_user: bool = Query(False),
|
|
||||||
store_service: StoreService = Depends(get_store_service),
|
|
||||||
store_api_Key: str = Depends(get_optional_user_store_api_key),
|
|
||||||
is_component: Optional[bool] = Query(None),
|
|
||||||
):
|
|
||||||
try:
|
|
||||||
result = store_service.count_components(
|
|
||||||
api_key=store_api_Key,
|
|
||||||
filter_by_user=filter_by_user,
|
|
||||||
is_component=is_component,
|
|
||||||
)
|
|
||||||
return {"count": result}
|
|
||||||
except Exception as exc:
|
|
||||||
raise HTTPException(status_code=400, detail=str(exc))
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/components/{component_id}", response_model=DownloadComponentResponse)
|
@router.get("/components/{component_id}", response_model=DownloadComponentResponse)
|
||||||
def read_component(
|
def read_component(
|
||||||
component_id: UUID,
|
component_id: UUID,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue