Skip to content

Commit 2bceb8a

Browse files
committed
Add '/edit' and '/delete' routes
models/presets: add get_preset_id, update_preset, delete_preset and PresetsDatabaseException Signed-off-by: Vallari Agrawal <[email protected]>
1 parent 677bf68 commit 2bceb8a

File tree

2 files changed

+81
-13
lines changed

2 files changed

+81
-13
lines changed

Diff for: src/models/presets.py

+40-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from sqlalchemy import Column, Integer, String, UniqueConstraint
22
from sqlalchemy.orm import Session
3-
from . import Base
43
from schemas.presets import PresetSchema
4+
from . import Base
55

66

77
class Presets(Base):
@@ -15,6 +15,12 @@ class Presets(Base):
1515
__table_args__ = (UniqueConstraint("username", "name"),)
1616

1717

18+
class PresetsDatabaseException(Exception):
19+
def __init__(self, message, code):
20+
super().__init__(message)
21+
self.code = code
22+
23+
1824
def create_preset(db: Session, preset: PresetSchema):
1925
new_preset = Presets(**preset.model_dump())
2026
db.add(new_preset)
@@ -23,13 +29,42 @@ def create_preset(db: Session, preset: PresetSchema):
2329
return new_preset
2430

2531

26-
def get_user_presets(db: Session, username: str):
27-
return db.query(Presets).filter(Presets.username == username).all()
32+
def get_presets_by_username(db: Session, username: str):
33+
db_preset = db.query(Presets).filter(Presets.username == username).all()
34+
return db_preset
2835

2936

30-
def get_preset(db: Session, username: str, preset_name: str):
31-
return (
37+
def get_preset_by_username_name(db: Session, username: str, preset_name: str):
38+
db_preset = (
3239
db.query(Presets)
3340
.filter(Presets.username == username, Presets.name == preset_name)
3441
.first()
3542
)
43+
return db_preset
44+
45+
46+
def get_preset_id(db: Session, preset_id: int):
47+
db_preset = db.query(Presets).filter(Presets.id == preset_id).first()
48+
return db_preset
49+
50+
51+
def update_preset(db: Session, preset_id: int, update_data):
52+
preset_query = db.query(Presets).filter(Presets.id == preset_id)
53+
db_preset = preset_query.first()
54+
if not db_preset:
55+
raise PresetsDatabaseException("Preset does not exist - unable to update.", 404)
56+
preset_query.filter(Presets.id == preset_id).update(
57+
update_data, synchronize_session=False
58+
)
59+
db.commit()
60+
db.refresh(db_preset)
61+
return db_preset
62+
63+
64+
def delete_preset(db: Session, preset_id: int):
65+
preset_query = db.query(Presets).filter(Presets.id == preset_id)
66+
db_preset = preset_query.first()
67+
if not db_preset:
68+
raise PresetsDatabaseException("Preset does not exist - unable to delete.", 404)
69+
preset_query.delete(synchronize_session=False)
70+
db.commit()

Diff for: src/routes/presets.py

+41-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from fastapi import APIRouter, HTTPException, Depends
1+
from fastapi import APIRouter, HTTPException, Depends, Response
22
from models import get_db
3-
from models.presets import get_preset, get_user_presets, create_preset
3+
from models.presets import PresetsDatabaseException
4+
from models import presets as presets_model
45
from schemas.presets import PresetSchema
56
from sqlalchemy.orm import Session
67
import logging
@@ -15,21 +16,53 @@
1516

1617
@router.get("/", status_code=200)
1718
def read_preset(username: str, name: str, db: Session = Depends(get_db)):
18-
db_preset = get_preset(db, username, name)
19+
db_preset = presets_model.get_preset_by_username_name(db, username, name)
20+
if not db_preset:
21+
raise HTTPException(status_code=404, detail=f"Preset does not exist.")
1922
return db_preset
2023

2124

2225
@router.get("/list", status_code=200)
2326
def read_preset(username: str, db: Session = Depends(get_db)):
24-
db_presets = get_user_presets(db, username)
27+
db_presets = presets_model.get_presets_by_username(db, username)
28+
if not db_presets:
29+
raise HTTPException(status_code=404, detail=f"User has no presets saved.")
2530
return db_presets
2631

2732

28-
@router.post("/add", status_code=200, response_model=PresetSchema)
33+
@router.post("/add", status_code=200)
2934
def add_preset(preset: PresetSchema, db: Session = Depends(get_db)):
30-
db_preset = get_preset(db, username=preset.username, preset_name=preset.name)
35+
db_preset = presets_model.get_preset_by_username_name(
36+
db, username=preset.username, preset_name=preset.name
37+
)
3138
if db_preset:
3239
raise HTTPException(
33-
status_code=400, detail=f"Preset '{preset.name}' already exists."
40+
status_code=400, detail=f"Preset of this username & name already exists."
41+
)
42+
return presets_model.create_preset(db, preset)
43+
44+
45+
@router.put("/edit/{preset_id}", status_code=200)
46+
def update_preset(
47+
preset_id: int, updated_data: PresetSchema, db: Session = Depends(get_db)
48+
):
49+
try:
50+
return presets_model.update_preset(
51+
db, preset_id, updated_data.model_dump(exclude_unset=True)
52+
)
53+
except PresetsDatabaseException as exc:
54+
raise HTTPException(
55+
status_code=exc.code,
56+
detail=str(exc),
57+
)
58+
59+
60+
@router.delete("/delete/{preset_id}", status_code=204)
61+
def delete_preset(preset_id: int, db: Session = Depends(get_db)):
62+
try:
63+
presets_model.delete_preset(db, preset_id)
64+
except PresetsDatabaseException as exc:
65+
raise HTTPException(
66+
status_code=exc.code,
67+
detail=str(exc),
3468
)
35-
return create_preset(db, preset)

0 commit comments

Comments
 (0)