Exemplo n.º 1
0
        public ActionResult Edit(int taskId)
        {
            try
            {
                Task task;
                using (var context = new TaskManagerContext())
                {
                    task = context.Tasks
                           .Include(x => x.TaskRecipient)
                           .Include(x => x.TaskSender)
                           .Include(x => x.TaskPriority)
                           .FirstOrDefault(x => x.TaskId == taskId);
                    if (task != null)
                    {
                        var taskViewModel = new ChiefTaskEditViewModel
                        {
                            AcceptCompleteDate                               = task.AcceptCpmpleteDate,
                            CompleteDate                                     = task.CompleteDate,
                            CreationDate                                     = task.CreateDate,
                            Deadline                                         = task.Deadline.HasValue ? task.Deadline.Value.ToString(ModelHelper.DateFormat) : string.Empty,
                            PriorityId                                       = task.TaskPriority != null?task.TaskPriority.PriorityId.ToString() : "2",
                                                                PriorityName = task.TaskPriority != null ? task.TaskPriority.PriorityName : string.Empty,
                                                                RecipientId  = task.TaskRecipient != null?task.TaskRecipient.UserId.ToString() : "0",
                                                                                   RecipientName = task.TaskRecipient != null ? task.TaskRecipient.UserFullName : string.Empty,
                                                                                   AssignDate    = task.AssignDateTime.HasValue ? task.AssignDateTime.Value.ToString(ModelHelper.DateTimeFormatFull) : string.Empty,
                                                                                   ResultComment = task.ResultComment,
                                                                                   SenderName    = task.TaskSender.UserFullName,
                                                                                   TaskText      = task.TaskText,
                                                                                   TaskId        = task.TaskId,
                                                                                   CommentsCount = task.Comments.Count
                        };



                        taskViewModel.RecipientsList = ModelHelper.GetRecipientsSelectedList("не назначен", taskViewModel.PriorityId, context);

                        taskViewModel.PrioritiesList = ModelHelper.GetPrioritiesSelectedList(taskViewModel.PriorityId, context);


                        return(View(taskViewModel));
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return(RedirectToAction("Index"));
        }
Exemplo n.º 2
0
        public ActionResult Edit(ChiefTaskEditViewModel model)
        {
            try
            {
                using (var context = new TaskManagerContext())
                {
                    var task = context.Tasks.FirstOrDefault(x => x.TaskId == model.TaskId);
                    if (task != null)
                    {
                        int recipId;
                        if (int.TryParse(model.RecipientId, out recipId))
                        {
                            if ((task.RecipientId == null && recipId != 0) || (task.RecipientId != null && task.RecipientId != recipId))
                            {
                                task.TaskEeventLogs.Add(new TaskEeventLog
                                {
                                    EventDateTime = DateTime.Now,
                                    PropertyName  = "RecipientId",
                                    UserId        = WebSecurity.CurrentUserId,
                                    OldValue      = task.RecipientId.HasValue ? task.RecipientId.Value.ToString() : null,
                                    NewValue      = recipId == 0 ? null : recipId.ToString()
                                });
                                task.RecipientId       = recipId == 0 ? (int?)null : recipId;
                                task.IsRecipientViewed = false;
                            }
                        }
                        if (recipId == 0)
                        {
                            task.AssignDateTime = null;
                            task.Deadline       = null;
                        }
                        else
                        {
                            DateTime date;
                            if (DateTime.TryParse(model.Deadline, out date))
                            {
                                if (task.Deadline != date)
                                {
                                    task.TaskEeventLogs.Add(new TaskEeventLog
                                    {
                                        EventDateTime = DateTime.Now,
                                        PropertyName  = "Deadline",
                                        UserId        = WebSecurity.CurrentUserId,
                                        OldValue      = task.Deadline.HasValue ? task.Deadline.Value.ToString(ModelHelper.DateFormat) : null,
                                        NewValue      = date.ToString(ModelHelper.DateFormat)
                                    });
                                    task.Deadline          = date;
                                    task.AssignDateTime    = DateTime.Now;
                                    task.IsRecipientViewed = false;
                                }
                            }
                            else
                            {
                                ModelState.AddModelError("Deadline", "Назначьте срок исполнения");
                                model.PrioritiesList = ModelHelper.GetPrioritiesSelectedList(model.PriorityId, context);
                                model.RecipientsList = ModelHelper.GetRecipientsSelectedList("не назначен", model.RecipientId, context);
                                return(View(model));
                            }
                        }

                        int priorId;
                        if (int.TryParse(model.PriorityId, out priorId))
                        {
                            if (task.PriorityId != priorId)
                            {
                                task.TaskEeventLogs.Add(new TaskEeventLog
                                {
                                    EventDateTime = DateTime.Now,
                                    PropertyName  = "PriorityId",
                                    OldValue      = task.PriorityId.HasValue ? task.PriorityId.Value.ToString() : null,
                                    NewValue      = priorId.ToString(),
                                    UserId        = WebSecurity.CurrentUserId
                                });
                                task.PriorityId = priorId;
                            }
                        }
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return(RedirectToAction("Index"));
        }