From 2b529541bfca5aec087f925eee8a6ff61b35afb8 Mon Sep 17 00:00:00 2001 From: Lian Drake Date: Sun, 22 Sep 2024 01:06:47 -0400 Subject: [PATCH] Fixed bugs that allowed tasks or project to be assigned to a non existing parent --- controllers/project_controller.go | 63 ++++++++++++++++++++----------- controllers/task_controller.go | 8 +++- 2 files changed, 47 insertions(+), 24 deletions(-) diff --git a/controllers/project_controller.go b/controllers/project_controller.go index fcff8b7..a55f615 100644 --- a/controllers/project_controller.go +++ b/controllers/project_controller.go @@ -10,8 +10,9 @@ import ( // Error constants for project operations. var ( - ErrNoProjectName = errors.New("project name is required") - ErrParentProjectNotFound = errors.New("parent project not found") + ErrNoProjectName = errors.New("project name is required") + ErrParentProjectNotFound = errors.New("parent project not found") + ErrNoParentProjectProvided = errors.New("no parent project provided") ) // ProjectController manages the project-related business logic. @@ -34,17 +35,9 @@ func (pc *ProjectController) CreateProject( } // Retrieve the parent project ID (if any) - var parentProjectID *int - if parentProjectIdentifier != "" { - parentID, projectErr := utils.ParseIntOrError(parentProjectIdentifier) - if projectErr != nil { - project, lookupErr := pc.repo.GetProjectByName(parentProjectIdentifier) - if lookupErr != nil { - return ErrParentProjectNotFound - } - parentID = project.ID - } - parentProjectID = &parentID + parentProjectID, err := pc.getParentProjectID(parentProjectIdentifier) + if err != nil && !errors.Is(err, ErrNoParentProjectProvided) { + return err } // Create a new project @@ -76,17 +69,13 @@ func (pc *ProjectController) EditProject( if description != "" { project.Description = description } - if parentProjectIdentifier != "" { - parentID, projectErr := utils.ParseIntOrError(parentProjectIdentifier) - if projectErr != nil { - projectByName, lookupErr := pc.repo.GetProjectByName(parentProjectIdentifier) - if lookupErr != nil { - return ErrParentProjectNotFound - } - parentID = projectByName.ID - } - project.ParentProjectID = &parentID + + // Retrieve and apply the parent project ID (if any) + parentProjectID, err := pc.getParentProjectID(parentProjectIdentifier) + if err != nil && !errors.Is(err, ErrNoParentProjectProvided) { + return err } + project.ParentProjectID = parentProjectID // Update the project in the repository return pc.repo.UpdateProject(project) @@ -130,3 +119,31 @@ func (pc *ProjectController) RemoveProject(id int) error { // Remove the parent project return pc.repo.DeleteProject(id) } + +// getParentProjectID checks and retrieves the parent project ID based on the identifier (name or ID). +func (pc *ProjectController) getParentProjectID(parentProjectIdentifier string) (*int, error) { + if parentProjectIdentifier == "" { + // No parent project identifier provided, so no parent project ID is needed + return nil, ErrNoParentProjectProvided + } + + // Try to parse the parent project identifier as an integer ID + parentID, err := utils.ParseIntOrError(parentProjectIdentifier) + if err == nil { + // Successfully parsed as ID, now check if the project exists by ID + project, getProjectErr := pc.repo.GetProjectByID(parentID) + if getProjectErr != nil || project == nil { + return nil, ErrParentProjectNotFound + } + return &parentID, nil + } + + // If parsing failed, treat it as a project name and search by name + project, lookupErr := pc.repo.GetProjectByName(parentProjectIdentifier) + if lookupErr != nil || project == nil { + return nil, ErrParentProjectNotFound + } + + // Return the found project ID + return &project.ID, nil +} diff --git a/controllers/task_controller.go b/controllers/task_controller.go index ec9dec7..7ade423 100644 --- a/controllers/task_controller.go +++ b/controllers/task_controller.go @@ -47,10 +47,16 @@ func (tc *TaskController) CreateTask( projectID, projectErr := utils.ParseIntOrError(projectIdentifier) if projectErr != nil { project, lookupErr := tc.repo.GetProjectByName(projectIdentifier) - if lookupErr != nil { + if lookupErr != nil || project == nil { return ErrNoProjectFound } projectID = project.ID + } else { + // Check if the project exists using the ID + project, getProjectErr := tc.repo.GetProjectByID(projectID) + if getProjectErr != nil || project == nil { + return ErrNoProjectFound + } } // Get parent task ID (optional)