Skip to content

Commit

Permalink
refactor: ♻️ Improve CRUD functions
Browse files Browse the repository at this point in the history
  • Loading branch information
egrelier committed Jul 12, 2024
1 parent 682b0cc commit ba89ed1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
10 changes: 4 additions & 6 deletions thermal_events/crud/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,18 @@ def get(self, id: Any) -> Optional[ModelType]:
with session_scope() as session:
return session.query(self.model).filter(self.model.id == id).first()

def get_multi(self, *, skip: int = 0, limit: int = 100) -> List[ModelType]:
"""Get multiple objects with optional pagination.
def get_multi(self, ids) -> List[ModelType]:
"""Get multiple objects given their ids.
Args:
skip (int, optional): The number of objects to skip. Defaults to 0.
limit (int, optional): The maximum number of objects to retrieve. Defaults
to 100.
ids (list): The list of ids of objects to query.
Returns:
List[ModelType]: The list of retrieved objects.
"""
with session_scope() as session:
return session.query(self.model).offset(skip).limit(limit).all()
return session.query(self.model).filter(self.model.id.in_(ids)).all()

def create(self, obj_in: Union[list, ModelType]) -> None:
"""Create a new object or a list of objects.
Expand Down
16 changes: 16 additions & 0 deletions thermal_events/crud/thermal_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,22 @@ def change_analysis_status(self, event_id: int, new_status: str):
)
session.commit()

def change_severity(self, event_id: int, new_severity: str):
"""Change the severity of a ThermalEvent.
Args:
event_id (int):
id of the ThermalEvent to update.
new_severity (str):
New severity value.
"""
with session_scope() as session:
session.query(ThermalEvent).filter_by(id=event_id).update(
{"severity": new_severity}
)
session.commit()

def update(self, obj_in: Union[ThermalEvent, List[ThermalEvent]]) -> None:
"""Update an existing object or a list of ThermalEvent objects.
Expand Down

0 comments on commit ba89ed1

Please sign in to comment.