Skip to content

Commit

Permalink
feat: Add deletion of associated tasks when deleting a project
Browse files Browse the repository at this point in the history
  • Loading branch information
Pradip-p committed Jun 28, 2024
1 parent ceebf44 commit 99d3ebb
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/backend/app/projects/project_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down

0 comments on commit 99d3ebb

Please sign in to comment.