public ActionResult Add()
 {
     var notification = new NotificationModel();
     LoadTypeNotification(ref notification);
     ViewBag.Section = new SelectList(new List<string> { "Todos", "A", "B", "C" }, "Todos");
     return View("Add", notification);
 }
        public ActionResult Add(NotificationModel eventNotification, string addressedTo)
        {
            var notificationIdentity = Mapper.Map<Notification>(eventNotification);
            notificationIdentity.Created = DateTime.Now;
            var notificationType = _notificationTypeRepository.GetById(eventNotification.NotificationTypeId);
            notificationIdentity.NotificationType = notificationType;
            notificationIdentity.Section = eventNotification.GradeSection;
            if (notificationIdentity.NotificationType != null && notificationIdentity.NotificationType.Id == Personal)
            {
                notificationIdentity.IdGradeAreaUserGeneralSelected = eventNotification.StudentId;
                notificationIdentity.GradeIdifNotificationTypePersonal = Convert.ToInt32(eventNotification.IdIsGradeAreaGeneralSelected);
                notificationIdentity.TargetStudent = _studentRepository.GetById(eventNotification.StudentId);
            }
            else if (notificationIdentity.NotificationType != null && notificationIdentity.NotificationType.Id == Area)
            {
                notificationIdentity.IdGradeAreaUserGeneralSelected = Convert.ToInt32(eventNotification.IdIsGradeAreaGeneralSelected);
                notificationIdentity.GradeIdifNotificationTypePersonal = Convert.ToInt32(eventNotification.IdIsGradeAreaGeneralSelected);
            }
            if (addressedTo.IsEmpty() || addressedTo == null)
                addressedTo = "0";
            notificationIdentity.GradeIdifNotificationTypePersonal = Convert.ToInt32(addressedTo);
            notificationIdentity.Users = new List<User>();
            if (notificationIdentity.NotificationType != null && notificationIdentity.NotificationType.Id == Personal)
            {
                AddUsersToPersonalNotification(notificationIdentity);
            }
            if (notificationIdentity.NotificationType != null && notificationIdentity.NotificationType.Id == Grado)
            {
                AddUsersToGradeNotification(notificationIdentity);
            }
            if (notificationIdentity.NotificationType != null && notificationIdentity.NotificationType.Id == Area)
            {
                AddUsersToLevelNotification(notificationIdentity);
            }
            var user =
                _userRepository.GetAllUsers()
                    .FirstOrDefault(x => x.Email == _sessionManagement.GetUserLoggedEmail());
            if (user != null)
            {
                notificationIdentity.UserCreatorId = user.Id;
                notificationIdentity.UserCreatorName = user.DisplayName;
            }
            notificationIdentity.Approved = false;
            if (notificationIdentity.NotificationType != null && notificationIdentity.NotificationType.Id == Personal)
            {
                notificationIdentity.Approved = false;
            }

            if (notificationIdentity.NotificationType != null &&
                _sessionManagement.GetUserLoggedRole().Equals("Administrador"))
                notificationIdentity.Approved = true;

            var email = _sessionManagement.GetUserLoggedEmail();
            notificationIdentity.NotificationCreator =
                _userRepository.FirstOrDefault(x => x.Email.Equals(email));
            _notificationRepository.Create(notificationIdentity);
            const string title = "Notificación Agregado";
            var content = "El evento " + eventNotification.NotificationName + " ha sido agregado exitosamente.";
            _viewMessageLogic.SetNewMessage(title, content, ViewMessageType.SuccessMessage);
            return RedirectToAction("Index");
        }
        private void LoadTypeNotification(ref NotificationModel model)
        {
            var items = _notificationTypeRepository.Query(c => c).Select(c => new SelectListItem
            {
                Text = c.Description,
                Value = c.Id.ToString()
            }).ToList();

            model.NotificationTypeSelectList = new SelectList(items, "Value", "Text", model.NotificationTypeId);
            if (model.NotificationTypeId == Personal)
            {
                var list = GetListOpcionTypeNotification(model.NotificationTypeId.ToString(CultureInfo.InvariantCulture));
                model.NotificationTypeOpionSelectList = new SelectList(list, "Value", "Text",
                    model.GradeIdifNotificationTypePersonal);
                IEnumerable<SelectListItem> listStudent = LoadListStudent(model.NotificationTypeId == Personal ? Convert.ToInt32(model.IdIsGradeAreaGeneralSelected) : 0);
                model.StudentOptionSelectList = new SelectList(listStudent, "Value", "Text", model.IdIsGradeAreaGeneralSelected);
            }
            else
            {
                var list = GetListOpcionTypeNotification(model.NotificationTypeId.ToString(CultureInfo.InvariantCulture));
                model.NotificationTypeOpionSelectList = new SelectList(list, "Value", "Text",
                    model.IdIsGradeAreaGeneralSelected);
                var lis2 = new List<SelectListItem> {new SelectListItem {Value = "0", Text = "N/A"}};
                model.StudentOptionSelectList = new SelectList(lis2, "Value", "Text", model.IdIsGradeAreaGeneralSelected);
            }
        }
        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");
        }