示例#1
0
        public ActionResult Edit(int id, NotificationModel eventNotification)
        {
            try
            {
                var toEdit = _notificationRepository.Query(x => x).Include("NotificationType").FirstOrDefault(x => x.Id.Equals(id));
                if (toEdit != null)
                {
                    var notificationBeforeEdit = _notificationRepository.GetById(toEdit.Id);
                    var notificationType       = _notificationTypeRepository.First(c => c.Id == eventNotification.NotificationTypeId);
                    var receiverChanged        = false;
                    toEdit.NotificationType = notificationType;
                    toEdit.NotificationName = eventNotification.NotificationName;
                    toEdit.Message          = eventNotification.Message;

                    if (toEdit.NotificationType != null && toEdit.NotificationType.Id == Personal)
                    {
                        toEdit.IdGradeAreaUserGeneralSelected    = eventNotification.StudentId;
                        toEdit.GradeIdifNotificationTypePersonal = Convert.ToInt32(eventNotification.IdIsGradeAreaGeneralSelected);
                    }
                    else
                    {
                        toEdit.IdGradeAreaUserGeneralSelected    = Convert.ToInt32(eventNotification.IdIsGradeAreaGeneralSelected);
                        toEdit.GradeIdifNotificationTypePersonal = 0;
                    }
                    if (toEdit.NotificationType != null && toEdit.NotificationType.Id == notificationBeforeEdit.NotificationType.Id)
                    {
                        //Something is weird here. Other than the empty ifs, I mean.

                        receiverChanged = true;
                    }
                    else
                    {
                        receiverChanged = true;
                    }
                    if (receiverChanged)
                    {
                        toEdit.Users = new List <User>();
                        if (toEdit.NotificationType != null && toEdit.NotificationType.Id == Personal)
                        {
                            AddUsersToPersonalNotification(toEdit);
                        }
                        if (toEdit.NotificationType != null && toEdit.NotificationType.Id == Grado)
                        {
                            AddUsersToGradeNotification(toEdit);
                        }
                        if (toEdit.NotificationType != null && toEdit.NotificationType.Id == Area)
                        {
                            AddUsersToLevelNotification(toEdit);
                        }
                    }
                }
                _notificationRepository.Update(toEdit);
                _notificationRepository.SaveChanges();
                _viewMessageLogic.SetNewMessage("Notificación Editada", "La notificación fue editada exitosamente.",
                                                ViewMessageType.SuccessMessage);
            }
            catch
            {
                _viewMessageLogic.SetNewMessage("Error en edición",
                                                "La notificación no pudo ser editada correctamente, por favor intente nuevamente.",
                                                ViewMessageType.ErrorMessage);
            }
            return(RedirectToAction("Index"));
        }