Skip to content

Commit

Permalink
Merge pull request #20 from swarmauri/feature/remove_pagination_from_…
Browse files Browse the repository at this point in the history
…query_params

Feature/remove pagination from query params
  • Loading branch information
cobycloud authored Dec 3, 2024
2 parents fd332f1 + d0ee2d4 commit 010b9d3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
23 changes: 16 additions & 7 deletions pkgs/crouton/crouton/core/sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,17 @@ def __init__(
def _parse_query_params(self, query_params: Dict[str, Any]) -> Dict[str, Any]:
"""
Parse query parameters into filters for the database query.
Exclude pagination-related parameters like 'skip' and 'limit'.
"""
filters = {}
accepted_fields = self.db_model.__table__.columns


# Exclude pagination-related parameters
excluded_params = {"skip", "limit"}

for key, value in query_params.items():
if key in excluded_params:
continue # Skip pagination parameters
if key in accepted_fields:
column = getattr(self.db_model, key)
try:
Expand All @@ -94,20 +100,22 @@ def _parse_query_params(self, query_params: Dict[str, Any]) -> Dict[str, Any]:
)
else:
raise HTTPException(400, f"Invalid filter field: {key}")

return filters


def _get_all(self, *args: Any, **kwargs: Any) -> CALLABLE_LIST:
def route(
db: Session = Depends(self.db_func),
pagination: PAGINATION = self.pagination,
query_params: Dict[str, Any] = Depends(query_params),
) -> List[Model]:
skip, limit = pagination.get("skip"), pagination.get("limit")

skip = pagination.get("skip", 0)
limit = pagination.get("limit", 100) # Default values if not provided

# Parse and validate query parameters
filters = self._parse_query_params(query_params)

# Apply filters to the query
db_models: Model = (
db.query(self.db_model)
Expand All @@ -117,14 +125,15 @@ def route(
.offset(skip)
.all()
)

if db_models:
return db_models
else:
raise NOT_FOUND from None

return route


def _get_one(self, *args: Any, **kwargs: Any) -> CALLABLE:
def route(
item_id: self._pk_type, db: Session = Depends(self.db_func) # type: ignore
Expand Down
2 changes: 1 addition & 1 deletion pkgs/crouton/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "crouton"
version = "0.0.4.dev10"
version = "0.0.4.dev11"
description = "A repository to enable API CRUD Routing"
authors = ["Jacob Stewart <[email protected]>"]
license = "Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion pkgs/crouton_client/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "crouton_client"
version = "0.0.4.dev10"
version = "0.0.4.dev11"
description = "A client for Swarmauri's crouton."
authors = ["Jacob Stewart <[email protected]>"]
license = "Apache-2.0"
Expand Down

0 comments on commit 010b9d3

Please sign in to comment.