Пример #1
0
        public ActionResult UserProjectAssignment(int?projectId)
        {
            var project = ProjectHelper.GetProjectById(DbContext, (int)projectId);

            if (project is null)
            {
                return(RedirectToAction("Index"));
            }

            var projectViewModel = new UserProjectAssignmentViewModel()
            {
                Title     = project.Title,
                projectId = project.Id,
                Users     = DbContext.Users.Select(user => new UserInProjectViewModel()
                {
                    DisplayName = user.DisplayName,
                    Email       = user.Email,
                    Selected    = user.Projects.Any(p => p.Id == project.Id),
                    UserId      = user.Id
                }).ToList()
            };

            return(View(projectViewModel));
        }
Пример #2
0
        public ActionResult UserProjectAssignment(UserProjectAssignmentViewModel assignedUsers)
        {
            // Get the project we're working on
            var project = ProjectHelper.GetProjectById(DbContext, assignedUsers.projectId);

            if (project is null)
            {
                return(RedirectToAction("Index"));
            }

            var userIds = assignedUsers.Users
                          .Where(user => user.Selected)
                          .Select(user => user.UserId)
                          .ToList();
            var removedIds = assignedUsers.Users
                             .Where(user => !user.Selected)
                             .Select(user => user.UserId)
                             .ToList();

            // Removing users not selected
            project.Users.RemoveAll(p => removedIds.Contains(p.Id));

            // A list of users that were selected
            var UserListLocal = UserManager.Users
                                .Where(user => userIds.Contains(user.Id))
                                .ToList();

            foreach (var item in assignedUsers.Users)
            {
                // If user exists on the project
                // And is selected
                // do nothing

                // if the user is NOT on the project and
                // is selected
                // Add

                // if the user is NOT on the project and
                // is not selected
                // do nothing

                // if the user is on the project and
                // is NOT selected
                // remove it

                // If the user is selected and is not already on the project, add them.
                if (item.Selected && !project.Users.Any(p => p.Id == item.UserId))
                {
                    project.Users.Add(UserListLocal.FirstOrDefault(p => p.Id == item.UserId));
                }
                //else if (!item.Selected && project.Users.Any(p => p.Id == item.UserId))
                //{
                //    //project.Users.Remove(UserListLocal.FirstOrDefault(p => p.Id == item.UserId));
                //    project.Users.RemoveAll(p => p.Id == item.UserId);
                //}
            }

            DbContext.SaveChanges();

            return(RedirectToAction("Index"));
        }