From c5229f275ba779d1a7155400b7a517f0b46f914c Mon Sep 17 00:00:00 2001 From: Vallari Agrawal Date: Sat, 9 Sep 2023 00:42:53 +0530 Subject: [PATCH] Add '/edit' and '/delete' routes models/presets: add get_preset_id, update_preset, delete_preset and PresetsDatabaseException Signed-off-by: Vallari Agrawal --- README.md | 1 + src/models/presets.py | 45 ++++++++++++++++++++++++++++++++++----- src/routes/presets.py | 49 ++++++++++++++++++++++++++++++++++++------- 3 files changed, 82 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index a84812d..55de883 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ A REST API to execute [teuthology commands](https://docs.ceph.com/projects/teuth Start in development mode with: ``` pip3 install -r requirements.txt +cd src/ uvicorn main:app --reload --port 8080 --host 0.0.0.0 ``` diff --git a/src/models/presets.py b/src/models/presets.py index b7e0935..f51d2d0 100644 --- a/src/models/presets.py +++ b/src/models/presets.py @@ -1,7 +1,7 @@ from sqlalchemy import Column, Integer, String, UniqueConstraint from sqlalchemy.orm import Session -from . import Base from schemas.presets import PresetSchema +from . import Base class Presets(Base): @@ -15,6 +15,12 @@ class Presets(Base): __table_args__ = (UniqueConstraint("username", "name"),) +class PresetsDatabaseException(Exception): + def __init__(self, message, code): + super().__init__(message) + self.code = code + + def create_preset(db: Session, preset: PresetSchema): new_preset = Presets(**preset.model_dump()) db.add(new_preset) @@ -23,13 +29,42 @@ def create_preset(db: Session, preset: PresetSchema): return new_preset -def get_user_presets(db: Session, username: str): - return db.query(Presets).filter(Presets.username == username).all() +def get_presets_by_username(db: Session, username: str): + db_preset = db.query(Presets).filter(Presets.username == username).all() + return db_preset -def get_preset(db: Session, username: str, preset_name: str): - return ( +def get_preset_by_username_name(db: Session, username: str, preset_name: str): + db_preset = ( db.query(Presets) .filter(Presets.username == username, Presets.name == preset_name) .first() ) + return db_preset + + +def get_preset_id(db: Session, preset_id: int): + db_preset = db.query(Presets).filter(Presets.id == preset_id).first() + return db_preset + + +def update_preset(db: Session, preset_id: int, update_data): + preset_query = db.query(Presets).filter(Presets.id == preset_id) + db_preset = preset_query.first() + if not db_preset: + raise PresetsDatabaseException("Preset does not exist - unable to update.", 404) + preset_query.filter(Presets.id == preset_id).update( + update_data, synchronize_session=False + ) + db.commit() + db.refresh(db_preset) + return db_preset + + +def delete_preset(db: Session, preset_id: int): + preset_query = db.query(Presets).filter(Presets.id == preset_id) + db_preset = preset_query.first() + if not db_preset: + raise PresetsDatabaseException("Preset does not exist - unable to delete.", 404) + preset_query.delete(synchronize_session=False) + db.commit() diff --git a/src/routes/presets.py b/src/routes/presets.py index 4c417a5..98356e3 100644 --- a/src/routes/presets.py +++ b/src/routes/presets.py @@ -1,6 +1,7 @@ -from fastapi import APIRouter, HTTPException, Depends +from fastapi import APIRouter, HTTPException, Depends, Response from models import get_db -from models.presets import get_preset, get_user_presets, create_preset +from models.presets import PresetsDatabaseException +from models import presets as presets_model from schemas.presets import PresetSchema from sqlalchemy.orm import Session import logging @@ -15,21 +16,53 @@ @router.get("/", status_code=200) def read_preset(username: str, name: str, db: Session = Depends(get_db)): - db_preset = get_preset(db, username, name) + db_preset = presets_model.get_preset_by_username_name(db, username, name) + if not db_preset: + raise HTTPException(status_code=404, detail=f"Preset does not exist.") return db_preset @router.get("/list", status_code=200) def read_preset(username: str, db: Session = Depends(get_db)): - db_presets = get_user_presets(db, username) + db_presets = presets_model.get_presets_by_username(db, username) + if not db_presets: + raise HTTPException(status_code=404, detail=f"User has no presets saved.") return db_presets -@router.post("/add", status_code=200, response_model=PresetSchema) +@router.post("/add", status_code=200) def add_preset(preset: PresetSchema, db: Session = Depends(get_db)): - db_preset = get_preset(db, username=preset.username, preset_name=preset.name) + db_preset = presets_model.get_preset_by_username_name( + db, username=preset.username, preset_name=preset.name + ) if db_preset: raise HTTPException( - status_code=400, detail=f"Preset '{preset.name}' already exists." + status_code=400, detail=f"Preset of this username & name already exists." + ) + return presets_model.create_preset(db, preset) + + +@router.put("/edit/{preset_id}", status_code=200) +def update_preset( + preset_id: int, updated_data: PresetSchema, db: Session = Depends(get_db) +): + try: + return presets_model.update_preset( + db, preset_id, updated_data.model_dump(exclude_unset=True) + ) + except PresetsDatabaseException as exc: + raise HTTPException( + status_code=exc.code, + detail=str(exc), + ) + + +@router.delete("/delete/{preset_id}", status_code=204) +def delete_preset(preset_id: int, db: Session = Depends(get_db)): + try: + presets_model.delete_preset(db, preset_id) + except PresetsDatabaseException as exc: + raise HTTPException( + status_code=exc.code, + detail=str(exc), ) - return create_preset(db, preset)