Skip to content

Commit

Permalink
Add services/presets.py
Browse files Browse the repository at this point in the history
Create presets service for database operations

Signed-off-by: Devansh Singh <[email protected]>
  • Loading branch information
Devansh3712 committed Jun 17, 2024
1 parent ec83c58 commit 7c7ec98
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/teuthology_api/services/presets.py
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()

0 comments on commit 7c7ec98

Please sign in to comment.