Example #1
0
        public bool AssignUserToAProject(string projectMgrId, string userId, int projectId)
        {
            if (UserAndRolesRepository.CheckIfUserIsInRole(projectMgrId, "project manager"))
            {
                var result = ProjectRepository.AssignUserToProject(userId, projectId);

                if (result)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
        [Route("Project/Edit")] //! this is needed because the get action has a route
        public ActionResult Edit(ProjectEditViewModel formData)
        {
            try
            {
                formData.Name = formData.Name?.Trim();

                #region Valid FormData Checks
                if (string.IsNullOrWhiteSpace(formData.Name))
                {
                    ModelState.AddModelError(nameof(formData.Name), "Project Name can't be left empty");
                }

                if (ProjectRepository.IsProjectNameAlreadyTaken(formData.Name, formData.Id))
                {
                    ModelState.AddModelError("", "Error - Project name is already taken");
                }

                if (!ModelState.IsValid)
                {
                    ProjectEditViewModel model;

                    #region Fixes a bug where project add/remove users lists were null (when name was just whitespace)
                    if (formData.UsersAddList == null || formData.UsersRemoveList == null)
                    {
                        Project project = ProjectRepository.GetProject(formData.Id);
                        if (project == null)
                        {
                            return(RedirectToAction(nameof(Index)));
                        }
                        model = ProjectEditViewModel.CreateNewViewModel(project, UserRepository);
                    }
                    else
                    {
                        model = formData;
                    }
                    #endregion
                    return(View(model));
                }
                #endregion

                Project foundProject = ProjectRepository.GetProject(formData.Id);

                if (foundProject == null)
                {
                    return(RedirectToAction(nameof(Index)));
                }

                bool isAddingUsers   = formData.SelectedUsersToAdd != null;
                bool isRemovingUsers = formData.SelectedUsersToRemove != null;

                #region Adding and Removing Users
                if (isAddingUsers)
                {
                    foreach (string userId in formData.SelectedUsersToAdd)
                    {
                        bool isUserAlreadyAssignedToProject = ProjectRepository.IsUserAssignedToProject(userId, foundProject);

                        if (!isUserAlreadyAssignedToProject)
                        {
                            ApplicationUser foundUser = UserRepository.GetUserById(userId);
                            bool            didUserGetAssignedToProject = ProjectRepository.AssignUserToProject(foundUser, foundProject.Id);

                            if (!didUserGetAssignedToProject)
                            {
                                return(RedirectToAction(nameof(Index)));
                            }
                        }
                    }
                }

                if (isRemovingUsers)
                {
                    foreach (string userId in formData.SelectedUsersToRemove)
                    {
                        bool isUserAssignedToProject = ProjectRepository.IsUserAssignedToProject(userId, foundProject);

                        if (isUserAssignedToProject)
                        {
                            //ApplicationUser foundUser = UserRepository.GetUserById(userId);
                            bool didUserGetUnassignedFromProject = ProjectRepository.UnassignUserFromProject(userId, foundProject.Id);

                            if (!didUserGetUnassignedFromProject)
                            {
                                return(RedirectToAction(nameof(Index)));
                            }
                        }
                    }
                }
                #endregion

                foundProject.Name        = formData.Name;
                foundProject.DateUpdated = DateTime.Now;

                DbContext.SaveChanges();

                return(RedirectToAction(nameof(Details), new { Id = formData.Id }));
            }
            catch (Exception e)
            {
                ProjectEditViewModel model = GenerateEditViewModel(formData.Id);

                if (model == null)
                {
                    return(RedirectToAction(nameof(Index)));
                }

                ModelState.AddModelError("", e.Message); // TODO: Remove after project completion (on staging phase)
                return(View(model));
            }
        }