Skip to content

Commit

Permalink
routes: move turnilo routes to dedicated router
Browse files Browse the repository at this point in the history
This commit moves turnilo dashboard routes to a dedicated module.
  • Loading branch information
msune committed Jun 12, 2024
1 parent c07fe17 commit 7b4b662
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 75 deletions.
78 changes: 3 additions & 75 deletions src/routes/routes.py
Original file line number Diff line number Diff line change
@@ -1,77 +1,5 @@
from fastapi import APIRouter, Depends, HTTPException
from typing import List
from sqlalchemy.orm import Session
from models.turnilo_dashboard import TurniloDashboard
from services import turnilo_dashboards as td

import constants
import data.database as db
from fastapi import APIRouter
from routes.turnilo_dashboard_routes import turnilo_router

api_router = APIRouter()

### Dashboards ###

# GET


@api_router.get(
constants.URL_PATH + "/turnilo/dashboards/",
response_model=List[TurniloDashboard],
summary="Gets all Turnilo dashboards"
)
def turnilo_get_dashboards(db_session: Session = Depends(db.get_session)):
return td.dashboards_get_all(db_session)


@api_router.get(
constants.URL_PATH + "/turnilo/dashboards/{id}",
response_model=TurniloDashboard,
summary="Get a Turnilo dashboard by id (integer)"
)
def turnilo_get_dashboard_id(id: str, db_session: Session = Depends(db.get_session)):
try:
int_id = int(id)
except BaseException:
raise HTTPException(status_code=400, detail="Id is not an integer")
return td.dashboards_get_id(db_session, int_id)

# POST


@api_router.post(
constants.URL_PATH + "/turnilo/dashboards/",
response_model=TurniloDashboard,
summary="Create a Turnilo dashboard. A unique id will be assigned."
)
def turnilo_create_dashboard(dashboard: TurniloDashboard, db_session: Session = Depends(db.get_session)):
return td.dashboards_create(db_session, dashboard)


# PUT
@api_router.put(
constants.URL_PATH + "/turnilo/dashboards/{id}",
response_model=TurniloDashboard,
summary="Update/replace a Turnilo dashboard. The dashboard (id) must exist"
)
def turnilo_update_dashboard(id: str, dashboard: TurniloDashboard, db_session: Session = Depends(db.get_session)):
try:
int_id = int(id)
dashboard.id = int_id
except BaseException:
raise HTTPException(status_code=400, detail="Id is not an integer")
return td.dashboards_update(db_session, dashboard)

# DELETE


@api_router.delete(
constants.URL_PATH + "/turnilo/dashboards/{id}",
response_model=TurniloDashboard,
summary="Delete a Turnilo dashboard"
)
def turnilo_delete_dashboard(id: str, db_session: Session = Depends(db.get_session)):
try:
int_id = int(id)
except BaseException:
raise HTTPException(status_code=400, detail="Id is not an integer")
return td.dashboards_delete(db_session, int_id)
api_router.include_router(turnilo_router)
70 changes: 70 additions & 0 deletions src/routes/turnilo_dashboard_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from fastapi import APIRouter, Depends, HTTPException
from typing import List
from sqlalchemy.orm import Session
from models.turnilo_dashboard import TurniloDashboard
from services import turnilo_dashboards as td

import constants
import data.database as db

turnilo_router = APIRouter()

### Dashboards ###


@turnilo_router.get(
constants.URL_PATH + "/turnilo/dashboards/",
response_model=List[TurniloDashboard],
summary="Gets all Turnilo dashboards"
)
def turnilo_get_dashboards(db_session: Session = Depends(db.get_session)):
return td.dashboards_get_all(db_session)


@turnilo_router.get(
constants.URL_PATH + "/turnilo/dashboards/{id}",
response_model=TurniloDashboard,
summary="Get a Turnilo dashboard by id (integer)"
)
def turnilo_get_dashboard_id(id: str, db_session: Session = Depends(db.get_session)):
try:
int_id = int(id)
except BaseException:
raise HTTPException(status_code=400, detail="Id is not an integer")
return td.dashboards_get_id(db_session, int_id)


@turnilo_router.post(
constants.URL_PATH + "/turnilo/dashboards/",
response_model=TurniloDashboard,
summary="Create a Turnilo dashboard. A unique id will be assigned."
)
def turnilo_create_dashboard(dashboard: TurniloDashboard, db_session: Session = Depends(db.get_session)):
return td.dashboards_create(db_session, dashboard)


@turnilo_router.put(
constants.URL_PATH + "/turnilo/dashboards/{id}",
response_model=TurniloDashboard,
summary="Update/replace a Turnilo dashboard. The dashboard (id) must exist"
)
def turnilo_update_dashboard(id: str, dashboard: TurniloDashboard, db_session: Session = Depends(db.get_session)):
try:
int_id = int(id)
dashboard.id = int_id
except BaseException:
raise HTTPException(status_code=400, detail="Id is not an integer")
return td.dashboards_update(db_session, dashboard)


@turnilo_router.delete(
constants.URL_PATH + "/turnilo/dashboards/{id}",
response_model=TurniloDashboard,
summary="Delete a Turnilo dashboard"
)
def turnilo_delete_dashboard(id: str, db_session: Session = Depends(db.get_session)):
try:
int_id = int(id)
except BaseException:
raise HTTPException(status_code=400, detail="Id is not an integer")
return td.dashboards_delete(db_session, int_id)

0 comments on commit 7b4b662

Please sign in to comment.