From 99d3ebb142ce3411e54cf306a32cfa5cef2f324a Mon Sep 17 00:00:00 2001 From: Pradip-p Date: Fri, 28 Jun 2024 11:26:08 +0545 Subject: [PATCH 1/2] feat: Add deletion of associated tasks when deleting a project --- src/backend/app/projects/project_routes.py | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/backend/app/projects/project_routes.py b/src/backend/app/projects/project_routes.py index 07125498..6a2a1388 100644 --- a/src/backend/app/projects/project_routes.py +++ b/src/backend/app/projects/project_routes.py @@ -22,6 +22,40 @@ ) +@router.delete('/projects/{project_id}', tags=["Projects"]) +def delete_project_by_id( + project_id: int, + db: Session = Depends(database.get_db) +): + """ + 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. + + 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() + for task in tasks: + db.delete(task) + + # Delete the project + db.delete(project) + db.commit() + return {"message": f"Project ID: {project_id} is deleted successfully."} + + @router.post( "/create_project", tags=["Projects"], response_model=project_schemas.ProjectOut ) From 4a6feb9d4b96bf1b234a9844a32ea7eea58120b8 Mon Sep 17 00:00:00 2001 From: Pradip-p Date: Fri, 28 Jun 2024 11:33:26 +0545 Subject: [PATCH 2/2] fix: api path --- src/backend/app/projects/project_routes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend/app/projects/project_routes.py b/src/backend/app/projects/project_routes.py index 6a2a1388..f2885ad4 100644 --- a/src/backend/app/projects/project_routes.py +++ b/src/backend/app/projects/project_routes.py @@ -22,7 +22,7 @@ ) -@router.delete('/projects/{project_id}', tags=["Projects"]) +@router.delete('/{project_id}', tags=["Projects"]) def delete_project_by_id( project_id: int, db: Session = Depends(database.get_db)