Skip to content

Commit

Permalink
Update api.py
Browse files Browse the repository at this point in the history
  • Loading branch information
michplunkett committed Dec 19, 2024
1 parent fcb3dd7 commit 2ca4be7
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions OpenOversight/app/api/v1/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
from http import HTTPMethod

from flask import Blueprint
from sqlalchemy.orm import joinedload

from OpenOversight.app.models.database import Assignment, Department, Officer, db
from OpenOversight.app.models.database_cache import (
get_database_cache_entry,
put_database_cache_entry,
)
from OpenOversight.app.utils.constants import KEY_DEPT_ALL_OFFICERS


v1 = Blueprint("v1", __name__, url_prefix="/v1")


@v1.route("/departments/<int:department_id>/officers", methods=[HTTPMethod.GET])
def download_dept_officers(department_id: int):
cache_params = (Department(id=department_id), KEY_DEPT_ALL_OFFICERS)
officers = get_database_cache_entry(*cache_params)
if officers is None:
officers = (
db.session.query(Officer)
.options(joinedload(Officer.assignments).joinedload(Assignment.job))
.options(joinedload(Officer.salaries))
.filter_by(department_id=department_id)
.all()
)
put_database_cache_entry(*cache_params, officers)

0 comments on commit 2ca4be7

Please sign in to comment.