Skip to content

Commit

Permalink
DH-4724/adding find_by method to instructionRepo
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammadrezaPourreza committed Sep 27, 2023
1 parent 659aae9 commit e8e445a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
8 changes: 2 additions & 6 deletions dataherald/api/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,14 +332,10 @@ def get_instructions(
self, db_connection_id: str, page: int = 1, limit: int = 10
) -> List[Instruction]:
instruction_repository = InstructionRepository(self.storage)
instructions = instruction_repository.find_all()
filtered_instructions = []
for instruction in instructions:
if instruction.db_connection_id == db_connection_id:
filtered_instructions.append(instruction)
instructions = instruction_repository.find_by({"db_connection_id": db_connection_id})
start_idx = (page - 1) * limit
end_idx = start_idx + limit
return filtered_instructions[start_idx:end_idx]
return instructions[start_idx:end_idx]

@override
def delete_instruction(self, db_connection_id: str, instruction_id: str) -> dict:
Expand Down
9 changes: 9 additions & 0 deletions dataherald/repositories/instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ def find_by_id(self, id: str) -> Instruction | None:
return None
return Instruction(**row)

def find_by(self, query: dict) -> list[Instruction]:
rows = self.storage.find(DB_COLLECTION, query)
result = []
for row in rows:
obj = Instruction(**row)
obj.id = str(row["_id"])
result.append(obj)
return result

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

0 comments on commit e8e445a

Please sign in to comment.