Skip to content

Commit

Permalink
Add presets routes, schema, model helper functions
Browse files Browse the repository at this point in the history
creates the following:
models/presets.py: create_preset, get_user_presets, get_preset
routes/presets.py: GET /, GET list/, POST /add
schemas/presets.py: PresetSchema

Signed-off-by: Vallari Agrawal <[email protected]>
  • Loading branch information
VallariAg committed Sep 7, 2023
1 parent 68fb9e9 commit 00bdd9c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from starlette.middleware.sessions import SessionMiddleware
from routes import suite, kill, login, logout
from routes import suite, kill, login, logout, presets
from dotenv import load_dotenv

load_dotenv()
Expand Down Expand Up @@ -37,3 +37,4 @@ def read_root(request: Request):
app.include_router(kill.router)
app.include_router(login.router)
app.include_router(logout.router)
app.include_router(presets.router)
15 changes: 15 additions & 0 deletions src/models/presets.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from sqlalchemy import Column, Integer, String, UniqueConstraint
from sqlalchemy.orm import Session
from . import Base
from schemas.presets import PresetSchema

class Presets(Base):
__tablename__ = 'presets'
Expand All @@ -12,3 +14,16 @@ class Presets(Base):
__table_args__ = (
UniqueConstraint("username", "name"),
)

def create_preset(db: Session, preset: PresetSchema):
new_preset = Presets(**preset.model_dump())
db.add(new_preset)
db.commit()
db.refresh(new_preset)
return new_preset

def get_user_presets(db: Session, username: str):
return db.query(Presets).filter(Presets.username == username).all()

def get_preset(db: Session, username: str, preset_name: str):
return db.query(Presets).filter(Presets.username == username, Presets.name == preset_name).first()
30 changes: 30 additions & 0 deletions src/routes/presets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from fastapi import APIRouter, HTTPException, Depends
from models import get_db
from models.presets import get_preset, get_user_presets, create_preset
from schemas.presets import PresetSchema
from sqlalchemy.orm import Session
import logging

log = logging.getLogger(__name__)

router = APIRouter(
prefix="/presets",
tags=["presets"],
)

@router.get("/", status_code=200)
def read_preset(username: str, name: str, db: Session = Depends(get_db)):
db_preset = get_preset(db, username, name)
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)
return db_presets

@router.post("/add", status_code=200, response_model=PresetSchema)
def add_preset(preset: PresetSchema, db: Session = Depends(get_db)):
db_preset = get_preset(db, username=preset.username, preset_name=preset.name)
if db_preset:
raise HTTPException(status_code=400, detail=f"Preset '{preset.name}' already exists.")
return create_preset(db, preset)
13 changes: 13 additions & 0 deletions src/schemas/presets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from pydantic import BaseModel, Field
from typing import Union


class PresetSchema(BaseModel):
# pylint: disable=too-few-public-methods
"""
Class for Base Args.
"""
username: Union[str, None] = Field(default=None)
name: Union[str, None] = Field(default=None)
suite: Union[str, None] = Field(default=None)
cmd: Union[str, None] = Field(default=None)

0 comments on commit 00bdd9c

Please sign in to comment.