-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DH-4724/adding support for db level instructions (#185)
* DH-4724/adding support for db level instructions * DH-4724/chanign the database tests * DH-4724/changing the find_all() function * DH-4724/changing the tests * DH-4724/adding find_by method to instructionRepo * DH-4724/reformat * DH-4724/add paginating to the db * DH-4724/reformat with black * DH-4724/updating the endpoints to be RESTfull * DH-4724/reformat with black * DH-4724/changing the docs
- Loading branch information
1 parent
680fc68
commit 0e5bce2
Showing
18 changed files
with
570 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from bson.objectid import ObjectId | ||
|
||
from dataherald.types import Instruction | ||
|
||
DB_COLLECTION = "instructions" | ||
|
||
|
||
class InstructionRepository: | ||
def __init__(self, storage): | ||
self.storage = storage | ||
|
||
def insert(self, instruction: Instruction) -> Instruction: | ||
instruction.id = str( | ||
self.storage.insert_one(DB_COLLECTION, instruction.dict(exclude={"id"})) | ||
) | ||
return instruction | ||
|
||
def find_one(self, query: dict) -> Instruction | None: | ||
row = self.storage.find_one(DB_COLLECTION, query) | ||
if not row: | ||
return None | ||
return Instruction(**row) | ||
|
||
def update(self, instruction: Instruction) -> Instruction: | ||
self.storage.update_or_create( | ||
DB_COLLECTION, | ||
{"_id": ObjectId(instruction.id)}, | ||
instruction.dict(exclude={"id"}), | ||
) | ||
return instruction | ||
|
||
def find_by_id(self, id: str) -> Instruction | None: | ||
row = self.storage.find_one(DB_COLLECTION, {"_id": ObjectId(id)}) | ||
if not row: | ||
return None | ||
return Instruction(**row) | ||
|
||
def find_by(self, query: dict, page: int = 1, limit: int = 10) -> list[Instruction]: | ||
rows = self.storage.find(DB_COLLECTION, query, page=page, limit=limit) | ||
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] | ||
|
||
def delete_by_id(self, id: str) -> int: | ||
return self.storage.delete_by_id(DB_COLLECTION, id) |
Oops, something went wrong.