Skip to content

Commit

Permalink
fix(backend): raw sql project deletion using encode/databases
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Jul 26, 2024
1 parent 0e2b512 commit 963479c
Showing 1 changed file with 27 additions and 28 deletions.
55 changes: 27 additions & 28 deletions src/backend/app/projects/project_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import geojson
from datetime import timedelta
from fastapi import APIRouter, HTTPException, Depends, UploadFile, File, Form
from sqlalchemy.orm import Session
from loguru import logger as log
from app.projects import project_schemas, project_crud
from app.db import database
Expand All @@ -26,52 +25,52 @@


@router.delete("/{project_id}", tags=["Projects"])
def delete_project_by_id(
async def delete_project_by_id(
project_id: uuid.UUID,
db: Session = Depends(database.get_db),
db: Database = Depends(database.get_db),
user: AuthUser = Depends(login_required),
):
"""
Delete a project by its ID, along with all associated tasks.
Args:
project_id (int): The ID of the project to delete.
db (Session): The database session dependency.
db (Database): The database session dependency.
Returns:
dict: A confirmation message.
Raises:
HTTPException: If the project is not found.
"""
# Query for the project
project = (
db.query(db_models.DbProject)
.filter(db_models.DbProject.id == project_id)
.first()
)
if not project:
raise HTTPException(status_code=404, detail="Project not found.")

# Query and delete associated tasks
tasks = (
db.query(db_models.DbTask)
.filter(db_models.DbTask.project_id == project_id)
.all()
delete_query = """
WITH deleted_project AS (
DELETE FROM projects
WHERE id = :project_id
RETURNING id
), deleted_tasks AS (
DELETE FROM tasks
WHERE project_id = :project_id
RETURNING project_id
)
SELECT id FROM deleted_project
"""

result = await db.fetch_one(
query=delete_query,
values={"project_id": project_id}
)
for task in tasks:
db.delete(task)

# Delete the project
db.delete(project)
db.commit()

if not result:
raise HTTPException(status_code=404)

return {"message": f"Project ID: {project_id} is deleted successfully."}


@router.post("/create_project", tags=["Projects"])
async def create_project(
project_info: project_schemas.ProjectIn,
db: Database = Depends(database.encode_db),
db: Database = Depends(database.get_db),
user_data: AuthUser = Depends(login_required),
):
"""Create a project in database."""
Expand All @@ -90,7 +89,7 @@ async def create_project(
async def upload_project_task_boundaries(
project_id: uuid.UUID,
task_geojson: UploadFile = File(...),
db: Database = Depends(database.encode_db),
db: Database = Depends(database.get_db),
user: AuthUser = Depends(login_required),
):
"""Set project task boundaries using split GeoJSON from frontend.
Expand Down Expand Up @@ -209,7 +208,7 @@ async def generate_presigned_url(
async def read_projects(
skip: int = 0,
limit: int = 100,
db: Database = Depends(database.encode_db),
db: Database = Depends(database.get_db),
user_data: AuthUser = Depends(login_required),
):
"Return all projects"
Expand All @@ -222,7 +221,7 @@ async def read_projects(
)
async def read_project(
project_id: uuid.UUID,
db: Database = Depends(database.encode_db),
db: Database = Depends(database.get_db),
user_data: AuthUser = Depends(login_required),
):
"""Get a specific project and all associated tasks by ID."""
Expand Down

0 comments on commit 963479c

Please sign in to comment.