Skip to content

Commit

Permalink
Add github auth to /add /edit /delete
Browse files Browse the repository at this point in the history
Signed-off-by: Vallari Agrawal <[email protected]>
  • Loading branch information
VallariAg committed Sep 12, 2023
1 parent 2bceb8a commit f056242
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
5 changes: 2 additions & 3 deletions src/models/presets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from sqlalchemy import Column, Integer, String, UniqueConstraint
from sqlalchemy.orm import Session
from schemas.presets import PresetSchema
from . import Base


Expand All @@ -21,8 +20,8 @@ def __init__(self, message, code):
self.code = code


def create_preset(db: Session, preset: PresetSchema):
new_preset = Presets(**preset.model_dump())
def create_preset(db: Session, preset):
new_preset = Presets(**preset)
db.add(new_preset)
db.commit()
db.refresh(new_preset)
Expand Down
43 changes: 37 additions & 6 deletions src/routes/presets.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from fastapi import APIRouter, HTTPException, Depends, Response
from sqlalchemy.orm import Session
import logging

from services.helpers import get_token
from models import get_db
from models.presets import PresetsDatabaseException
from models import presets as presets_model
from schemas.presets import PresetSchema
from sqlalchemy.orm import Session
import logging

log = logging.getLogger(__name__)

Expand All @@ -31,21 +33,40 @@ def read_preset(username: str, db: Session = Depends(get_db)):


@router.post("/add", status_code=200)
def add_preset(preset: PresetSchema, db: Session = Depends(get_db)):
def add_preset(
preset: PresetSchema,
db: Session = Depends(get_db),
access_token: str = Depends(get_token),
):
if not access_token:
raise HTTPException(
status_code=401,
detail="You need to be logged in",
headers={"WWW-Authenticate": "Bearer"},
)
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 of this username & name already exists."
)
return presets_model.create_preset(db, preset)
return presets_model.create_preset(db, preset.model_dump())


@router.put("/edit/{preset_id}", status_code=200)
def update_preset(
preset_id: int, updated_data: PresetSchema, db: Session = Depends(get_db)
preset_id: int,
updated_data: PresetSchema,
db: Session = Depends(get_db),
access_token: str = Depends(get_token),
):
if not access_token:
raise HTTPException(
status_code=401,
detail="You need to be logged in",
headers={"WWW-Authenticate": "Bearer"},
)
try:
return presets_model.update_preset(
db, preset_id, updated_data.model_dump(exclude_unset=True)
Expand All @@ -58,7 +79,17 @@ def update_preset(


@router.delete("/delete/{preset_id}", status_code=204)
def delete_preset(preset_id: int, db: Session = Depends(get_db)):
def delete_preset(
preset_id: int,
db: Session = Depends(get_db),
access_token: str = Depends(get_token),
):
if not access_token:
raise HTTPException(
status_code=401,
detail="You need to be logged in",
headers={"WWW-Authenticate": "Bearer"},
)
try:
presets_model.delete_preset(db, preset_id)
except PresetsDatabaseException as exc:
Expand Down

0 comments on commit f056242

Please sign in to comment.