Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

Commit

Permalink
Handle pages and limits
Browse files Browse the repository at this point in the history
  • Loading branch information
ahosgood committed Jan 9, 2024
1 parent 3a9ccab commit 56dc9d8
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 11 deletions.
2 changes: 0 additions & 2 deletions app/articles/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = []


Expand Down
2 changes: 0 additions & 2 deletions app/records/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = []
39 changes: 35 additions & 4 deletions app/schemas/api_response.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import math

from fastapi import HTTPException
from pydantic import BaseModel, ConfigDict

from .api_result import APIResult
Expand All @@ -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"
)
7 changes: 5 additions & 2 deletions app/sources/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
)
3 changes: 2 additions & 1 deletion app/sources/website.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down

0 comments on commit 56dc9d8

Please sign in to comment.