public ActionResult EditPost(int?id, TaskViewModel task) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } var taskDto = _taskService.GetTask(id); var newUsers = new List <string>(); foreach (var item in task.SelectedUsers) { newUsers.Add(item); } var oldUsers = new List <string>(); oldUsers = GetUsersForTask(id); var UsersForDeleteTask = oldUsers.Except(oldUsers).ToList(); var UsersForAddTask = newUsers.Except(oldUsers).ToList(); if (TryUpdateModel(taskDto)) { try { _taskService.UpdateTask(taskDto); task = _mapper.Map <TaskDTO, TaskViewModel>(taskDto); foreach (var item in UsersForAddTask) { var userTask = new UserTaskViewModel { UserId = Convert.ToInt32(item), TaskId = task.TaskId, TaskName = task.Name, StartDate = task.StartDate, DeadlineDate = task.DeadLineDate, State = TaskStates.Active.ToString() }; var userTaskDto = _mapper.Map <UserTaskViewModel, UserTaskDTO>(userTask); _vUserTaskService.SaveTaskForUser(userTaskDto); } foreach (var item in UsersForDeleteTask) { _vUserTaskService.DeleteUserTask(Convert.ToInt32(item), id); } return(RedirectToAction("Index")); } catch (RetryLimitExceededException /* dex */) { //Log the error (uncomment dex variable name and add a line here to write a log. ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator."); } } return(View(task)); }
public IHttpActionResult EditTask(int?id, TaskViewModel task) { if (ModelState.IsValid && id != null) { var taskDto = _taskService.GetTask(id); var newUsers = new List <string>(); foreach (var item in task.SelectedUsers) { newUsers.Add(item); } var oldUsers = new List <string>(); //oldUsers = GetUsersForTask(id); var UsersForDeleteTask = oldUsers.Except(oldUsers).ToList(); var UsersForAddTask = newUsers.Except(oldUsers).ToList(); _taskService.UpdateTask(taskDto); task = Mapper.Map <TaskDTO, TaskViewModel>(taskDto); foreach (var item in UsersForAddTask) { var userTask = new UserTaskViewModel { UserId = Convert.ToInt32(item), TaskId = task.TaskId, TaskName = task.Name, StartDate = task.StartDate, DeadlineDate = task.DeadLineDate, State = "Active" }; var userTaskDto = Mapper.Map <UserTaskViewModel, UserTaskDTO>(userTask); _vUserTaskService.SaveTaskForUser(userTaskDto); } foreach (var item in UsersForDeleteTask) { _vUserTaskService.DeleteUserTask(Convert.ToInt32(item), id); } } return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Something went wrong! Please try again."))); }