Skip to content

Commit

Permalink
feat: add project_deps with get_project_by_id logic
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Jan 16, 2024
1 parent ab44d6d commit 77a5555
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/backend/app/projects/project_deps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright (c) 2022, 2023 Humanitarian OpenStreetMap Team
#
# This file is part of FMTM.
#
# FMTM is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# FMTM is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with FMTM. If not, see <https:#www.gnu.org/licenses/>.
#

"""Project dependencies for use in Depends."""

from fastapi import Depends
from fastapi.exceptions import HTTPException
from sqlalchemy.orm import Session

from app.db.database import get_db
from app.db.db_models import DbProject
from app.models.enums import HTTPStatus


async def get_project_by_id(
db: Session = Depends(get_db), project_id: int = None
) -> DbProject:
"""Get a single project by id."""
if not project_id:
# Skip if no project id passed
return None

db_project = db.query(DbProject).filter(DbProject.id == project_id).first()
if not db_project:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND,
detail=f"Project with ID {project_id} does not exist",
)

return db_project

0 comments on commit 77a5555

Please sign in to comment.