From c3506eb0d9d5d70e778551870c3a15fecd17b2da Mon Sep 17 00:00:00 2001 From: Devansh Singh Date: Thu, 1 Aug 2024 21:04:17 +0530 Subject: [PATCH] Add check for preset limit Added a limit on how many presets a user can store at a time (currently 10) Signed-off-by: Devansh Singh --- src/teuthology_api/routes/presets.py | 13 +++++++++++++ src/teuthology_api/services/presets.py | 4 +++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/teuthology_api/routes/presets.py b/src/teuthology_api/routes/presets.py index b6cb32e..8f9f147 100644 --- a/src/teuthology_api/routes/presets.py +++ b/src/teuthology_api/routes/presets.py @@ -37,6 +37,7 @@ def read_all_presets(username: str, db: Session = Depends(get_db)): @router.post("/add", status_code=status.HTTP_201_CREATED) def add_preset( preset: Presets, + replace: bool = False, db: Session = Depends(get_db), access_token: str = Depends(get_token), ): @@ -46,6 +47,18 @@ def add_preset( detail="You need to be logged in", headers={"WWW-Authenticate": "Bearer"}, ) + + db_presets = PresetsService(db).get_by_username(preset.username) + if len(db_presets) == 1: + if not replace: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="Only 10 presets for a user can be stored at a time.", + ) + # If replace parameter is set true, delete the + # oldest preset of the user and create a new one + PresetsService(db).delete(db_presets[0].id) + db_preset_exists = PresetsService(db).get_by_username_and_name( preset.username, preset.name ) diff --git a/src/teuthology_api/services/presets.py b/src/teuthology_api/services/presets.py index 5136ea6..9571735 100644 --- a/src/teuthology_api/services/presets.py +++ b/src/teuthology_api/services/presets.py @@ -16,7 +16,9 @@ def __init__(self, db: Session) -> None: self.db = db def get_by_username(self, username: str): - statement = select(Presets).where(Presets.username == username) + statement = ( + select(Presets).where(Presets.username == username).order_by(Presets.id) + ) db_presets = self.db.exec(statement).all() return db_presets