/// <summary>
        /// Will load the specific task from database and convert it to the <c>UserTaskFormViewModel</c>.
        /// </summary>
        /// <param name="taskId">to get perticular task from database.</param>
        /// <returns>Model for the view.</returns>
        public UserTaskFormViewModel LoadViewModel(int taskId)
        {
            var           task          = db.Tasks.Find(taskId);
            List <string> selectedUsers = new List <string>();

            selectedUsers = task.Users.Select(u => u.Id).ToList();
            UserTaskFormViewModel userTaskFormViewModel = new UserTaskFormViewModel()
            {
                Task = new UserTask()
                {
                    Id                  = task.Id,
                    Name                = task.Name,
                    Description         = task.Description,
                    ProjectId           = task.ProjectId,
                    CompletedPercentage = task.CompletedPercentage,
                    Deadline            = task.Deadline,
                    Priority            = task.Priority,
                },
                UsersList = db.Users.Where(u => u.PersonType == PersonType.Developer)
                            .Select(u => new SelectListItem
                {
                    Text  = u.Name,
                    Value = u.Id
                }),
                SelectedId = selectedUsers.ToArray(),
            };

            return(userTaskFormViewModel);
        }
Esempio n. 2
0
 public ActionResult CreateOrUpdate(UserTaskFormViewModel userTaskFormViewModel)
 {
     if (ModelState.IsValid)
     {
         if (userTaskFormViewModel.Task.Id == 0)
         {
             tasksManagement.CreateTask(userTaskFormViewModel);
         }
         else
         {
             tasksManagement.UpdateTask(userTaskFormViewModel);
         }
     }
     return(RedirectToAction("List", new { projectId = userTaskFormViewModel.Task.ProjectId }));
 }
        /// <summary>
        /// Collect task, project, and selected users from <c>UserTaskFormViewModel</c> and create new record in database.
        /// </summary>
        /// <param name="userTaskFormView">to get required task, project, and selected users from ViewModel to make record in database.</param>
        public void CreateTask(UserTaskFormViewModel userTaskFormView)
        {
            var task = userTaskFormView.Task;

            task.Project = db.Projects.Find(task.ProjectId);
            var selectedUsers = userTaskFormView.SelectedId;

            if (userTaskFormView.Task != null)
            {
                db.Tasks.Add(task);
                db.SaveChanges();
                foreach (string developerId in selectedUsers)
                {
                    var user = db.Users.Find(developerId);
                    task.Users.Add(user);
                    db.SaveChanges();
                }
                projectManagement.UpdateCompletedWork(task.Project);
            }
        }
        /// <summary>
        /// Collect task, project, and selected users from <c>UserTaskFormViewModel</c> and update existing Task record in database.
        /// </summary>
        /// <param name="userTaskFormView">to get required task, project, and selected users from ViewModel to update record in database.</param>
        public void UpdateTask(UserTaskFormViewModel userTaskFormView)
        {
            var task = userTaskFormView.Task;

            task.Project = db.Projects.Find(task.ProjectId);
            var selectedUsersId = userTaskFormView.SelectedId;

            if (task != null)
            {
                var taskInDb = db.Tasks.Find(task.Id);

                taskInDb.Name                = task.Name;
                taskInDb.ProjectId           = task.ProjectId;
                taskInDb.CompletedPercentage = task.CompletedPercentage;
                taskInDb.Deadline            = task.Deadline;
                taskInDb.Priority            = task.Priority;

                List <User> selectedUsers = new List <User>();
                //Get all selected users from view model
                foreach (string developerId in selectedUsersId)
                {
                    var user = db.Users.Find(developerId);
                    selectedUsers.Add(user);
                }

                //Get all users to remove in removed list
                List <User> removedUsers = new List <User>();
                foreach (User user in taskInDb.Users)
                {
                    if (!selectedUsers.Contains(user))
                    {
                        removedUsers.Add(user);
                    }
                }

                //Remove it from task
                foreach (var user in removedUsers)
                {
                    taskInDb.Users.Remove(user);
                    db.SaveChanges();
                }


                //add all new user selected from view model
                foreach (var user in selectedUsers)
                {
                    if (!taskInDb.Users.Contains(user))
                    {
                        taskInDb.Users.Add(user);
                    }
                }
                db.SaveChanges();

                //Update completed % of work in project
                projectManagement.UpdateCompletedWork(taskInDb.Project);

                if (task.CompletedPercentage == 100)
                {
                    //if task completed add notification
                    notificationManagement.AddCompletedNotification(taskInDb, NotificationType.Completed);
                }
            }
        }