forked from VallariAg/teuthology-api
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create presets service for database operations Signed-off-by: Devansh Singh <[email protected]>
- Loading branch information
1 parent
ec83c58
commit 7c7ec98
Showing
1 changed file
with
57 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from sqlmodel import select, Session | ||
|
||
from teuthology_api.models.presets import Presets | ||
|
||
|
||
class PresetsDatabaseException(Exception): | ||
def __init__(self, message: str, code: int) -> None: | ||
super().__init__(message) | ||
self.code = code | ||
|
||
|
||
class PresetsService: | ||
def __init__(self, db: Session) -> None: | ||
self.db = db | ||
|
||
def get_by_username(self, username: str): | ||
statement = select(Presets).where(Presets.username == username) | ||
db_presets = self.db.exec(statement).all() | ||
return db_presets | ||
|
||
def get_by_username_and_name(self, username: str, preset_name: str): | ||
statement = select(Presets).where( | ||
Presets.username == username, Presets.name == preset_name | ||
) | ||
db_preset = self.db.exec(statement).first() | ||
return db_preset | ||
|
||
def get_by_id(self, preset_id: int): | ||
statement = select(Presets).where(Presets.id == preset_id) | ||
db_preset = self.db.exec(statement).first() | ||
return db_preset | ||
|
||
def create(self, preset: Presets) -> Presets: | ||
self.db.add(preset) | ||
self.db.commit() | ||
self.db.refresh(preset) | ||
return preset | ||
|
||
def update(self, preset_id: int, updated_data: dict) -> Presets: | ||
db_preset = self.get_by_id(preset_id) | ||
if db_preset is None: | ||
raise PresetsDatabaseException( | ||
"Preset does not exist, unable to update", 404 | ||
) | ||
|
||
db_preset.sqlmodel_update(updated_data) | ||
return self.create(db_preset) | ||
|
||
def delete(self, preset_id: int): | ||
db_preset = self.get_by_id(preset_id) | ||
if db_preset is None: | ||
raise PresetsDatabaseException( | ||
"Preset does not exist, unable to delete", 404 | ||
) | ||
|
||
self.db.delete(db_preset) | ||
self.db.commit() |