Skip to content

Commit

Permalink
DH-4747/adding paginating to find all methods
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammadrezaPourreza committed Sep 28, 2023
1 parent fa73fef commit 3670de2
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 8 deletions.
4 changes: 2 additions & 2 deletions dataherald/api/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ def get_golden_records(
page=page,
limit=limit,
)
return golden_records_repository.find_all()
return golden_records_repository.find_all(page=page, limit=limit)

@override
def add_instruction(self, instruction_request: InstructionRequest) -> Instruction:
Expand All @@ -382,7 +382,7 @@ def get_instructions(
page=page,
limit=limit,
)
return instruction_repository.find_all()
return instruction_repository.find_all(page=page, limit=limit)

@override
def delete_instruction(self, instruction_id: str) -> dict:
Expand Down
2 changes: 1 addition & 1 deletion dataherald/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def find(
pass

@abstractmethod
def find_all(self, collection: str) -> list:
def find_all(self, collection: str, page: int = 0, limit: int = 0) -> list:
pass

@abstractmethod
Expand Down
7 changes: 6 additions & 1 deletion dataherald/db/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ def find(
return list(cursor)

@override
def find_all(self, collection: str) -> list:
def find_all(self, collection: str, page: int = 0, limit: int = 0) -> list:
if page > 0 and limit > 0:
skip_count = (page - 1) * limit
return list(
self._data_store[collection].find({}).skip(skip_count).limit(limit)
)
return list(self._data_store[collection].find({}))

@override
Expand Down
4 changes: 2 additions & 2 deletions dataherald/repositories/golden_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def find_by(
rows = self.storage.find(DB_COLLECTION, query, page=page, limit=limit)
return [GoldenRecord(id=str(row["_id"]), **row) for row in rows]

def find_all(self) -> list[GoldenRecord]:
rows = self.storage.find_all(DB_COLLECTION)
def find_all(self, page: int = 0, limit: int = 0) -> list[GoldenRecord]:
rows = self.storage.find_all(DB_COLLECTION, page=page, limit=limit)
return [GoldenRecord(id=str(row["_id"]), **row) for row in rows]

def delete_by_id(self, id: str) -> int:
Expand Down
4 changes: 2 additions & 2 deletions dataherald/repositories/instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def find_by(self, query: dict, page: int = 1, limit: int = 10) -> list[Instructi
rows = self.storage.find(DB_COLLECTION, query, page=page, limit=limit)
return [Instruction(id=str(row["_id"]), **row) for row in rows]

def find_all(self) -> list[Instruction]:
rows = self.storage.find_all(DB_COLLECTION)
def find_all(self, page: int = 0, limit: int = 0) -> list[Instruction]:
rows = self.storage.find_all(DB_COLLECTION, page=page, limit=limit)
return [Instruction(id=str(row["_id"]), **row) for row in rows]

def delete_by_id(self, id: str) -> int:
Expand Down

0 comments on commit 3670de2

Please sign in to comment.