diff --git a/app/articles/schemas.py b/app/articles/schemas.py index b9fabac..162bd75 100644 --- a/app/articles/schemas.py +++ b/app/articles/schemas.py @@ -18,8 +18,6 @@ def toJSON(self): class ArticleSearchResults(APIResponse): model_config = ConfigDict(arbitrary_types_allowed=True) - count: int = 0 - pages: int = 0 results: list[Article] = [] diff --git a/app/records/schemas.py b/app/records/schemas.py index 97407f5..f6f329f 100644 --- a/app/records/schemas.py +++ b/app/records/schemas.py @@ -15,6 +15,4 @@ def toJSON(self): class RecordSearchResults(APIResponse): model_config = ConfigDict(arbitrary_types_allowed=True) - count: int = 0 - pages: int = 0 results: list[Record] = [] diff --git a/app/schemas/api_response.py b/app/schemas/api_response.py index b3c715e..a880337 100644 --- a/app/schemas/api_response.py +++ b/app/schemas/api_response.py @@ -1,3 +1,6 @@ +import math + +from fastapi import HTTPException from pydantic import BaseModel, ConfigDict from .api_result import APIResult @@ -7,10 +10,38 @@ class APIResponse(BaseModel): model_config = ConfigDict(arbitrary_types_allowed=True) count: int = 0 - pages: int = 0 + page: int = 1 + pages: int = 1 + result_range_min: int = 0 + result_range_max: int = 0 + results_per_page: int = 0 results: list[APIResult] = [] + def get_pages(self): + return ( + math.ceil(self.count / self.results_per_page) + if self.results_per_page + else 0 + ) + + def get_result_range_min(self): + return (self.results_per_page * (self.page - 1)) + 1 + + def get_result_range_max(self): + return self.results_per_page * self.page + + def page_in_range(self): + return self.page > 0 and self.page <= self.get_pages() + def toJSON(self): - return self.__dict__ | { - "results": [result.toJSON() for result in self.results] - } + if self.page_in_range(): + return self.__dict__ | { + "pages": self.get_pages(), + "result_range_min": self.get_result_range_min(), + "result_range_max": self.get_result_range_max(), + "foo": "bar", + "results": [result.toJSON() for result in self.results], + } + raise HTTPException( + status_code=404, detail=f"Page {self.page} out of range" + ) diff --git a/app/sources/discovery.py b/app/sources/discovery.py index 090996c..875611d 100644 --- a/app/sources/discovery.py +++ b/app/sources/discovery.py @@ -27,5 +27,8 @@ def get_results(self, page: int | None = 1) -> dict: record.title = r["title"] response.results.append(record) response.count = raw_results["count"] - response.pages = math.ceil(response.count / self.results_per_page) - return response.toJSON() + response.results_per_page = self.results_per_page + response.page = page + return ( + response.toJSON() if response.page_in_range() else {"None": "None"} + ) diff --git a/app/sources/website.py b/app/sources/website.py index 03bc70b..ffb3da5 100644 --- a/app/sources/website.py +++ b/app/sources/website.py @@ -46,7 +46,8 @@ def get_results(self, page: int | None = 1) -> dict: article.image = page_details["teaser_image_jpg"] response.results.append(article) response.count = raw_results["meta"]["total_count"] - response.pages = math.ceil(response.count / self.results_per_page) + response.results_per_page = self.results_per_page + response.page = page return response.toJSON()