Skip to content

Commit

Permalink
Add '/edit' and '/delete' routes
Browse files Browse the repository at this point in the history
models/presets: add get_preset_id, update_preset, delete_preset
		and PresetsDatabaseException

Signed-off-by: Vallari Agrawal <[email protected]>
  • Loading branch information
VallariAg committed Sep 8, 2023
1 parent 9dd3f88 commit c5229f2
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
45 changes: 40 additions & 5 deletions src/models/presets.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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)
Expand All @@ -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()
49 changes: 41 additions & 8 deletions src/routes/presets.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)

0 comments on commit c5229f2

Please sign in to comment.