Example #1
0
        public async Task <IActionResult> Create(NotificationsDetails viewModel)
        {
            if (ModelState.IsValid)
            {
                var notification = viewModel.Notification;

                notification.CreatedOn = DateTime.Now;
                notification.Sender    = await _userManager.GetUserAsync(HttpContext.User);

                _context.Notifications.Add(notification);

                var userForId = viewModel.UserForId;
                var userFor   = _context.ApplicationUsers.Single(x => x.Id == userForId);

                var notificationForUser = new NotificationsForUsers
                {
                    Notification = notification,
                    User         = userFor,
                    IsRead       = false,
                    IsArchived   = false,
                };

                _context.NotificationsForUsers.Add(notificationForUser);

                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Details), new { id = notification.Id }));
            }

            return(RedirectToAction(nameof(Index)));
        }
        public bool InsertNotification(Model.NotificationPostModel notificationPostModel)
        {
            bool result = false;

            try
            {
                Entity.NotificationsDetails notificationDetails = new NotificationsDetails
                {
                    DocumentUrl            = notificationPostModel.DocURL,
                    ImageUrl               = notificationPostModel.ImageURL,
                    Note                   = notificationPostModel.Notes,
                    NotificationDate       = notificationPostModel.NotificationDate,
                    NotificationsDetailsId = 0,
                    NotificationTypeId     = notificationPostModel.NotificationType.NotificationTypeId,
                    StaffInfoId            = notificationPostModel.SchoolInfoId,
                    IsActive               = true,
                };

                _schoolContext.NotificationsDetails.Add(notificationDetails);
                _schoolContext.SaveChanges();

                if (notificationDetails.NotificationsDetailsId > 0)
                {
                    foreach (var studentId in notificationPostModel.StudentIds)
                    {
                        Entity.StudentNotificationsMapping studentNotificationsMapping = new StudentNotificationsMapping
                        {
                            NotificationsDetailsId = notificationDetails.NotificationsDetailsId,
                            IsClassLevel           = notificationPostModel.IsClassLevel,
                            IsSchoolLevel          = notificationPostModel.IsSchoolLevel,
                            IsStudentLevel         = notificationPostModel.IsStudentLevel,
                            StudentId = studentId,
                            ClassId   = notificationPostModel.ClassId
                        };

                        _schoolContext.StudentNotificationsMapping.Add(studentNotificationsMapping);
                    }

                    var recordsAffect = _schoolContext.SaveChanges();
                    if (recordsAffect > 0)
                    {
                        result = true;
                    }
                }

                return(result);
            }
            catch (Exception ex)
            {
                return(result);

                throw;
            }
        }
Example #3
0
        public async Task <IActionResult> Details(int id)
        {
            var notification = _context.Notifications
                               .Include(x => x.NotificationsForUsers).ThenInclude(u => u.User)
                               .Include(x => x.NotificationType)
                               .Include(x => x.Reservation).ThenInclude(b => b.EquipmentBundle)
                               .Include(x => x.Equipment)
                               .Include(x => x.EquipmentBundle)
                               .Include(x => x.Event)
                               .FirstOrDefault(x => x.Id == id);

            if (notification == null)
            {
                return(RedirectToAction("Index"));
            }

            if (notification.NotificationsForUsers
                .Where(u => u.UserId == _userManager.GetUserId(User))
                .Any(x => !x.IsRead))
            {
                var newNotification = notification.NotificationsForUsers
                                      .Where(u => u.UserId == _userManager.GetUserId(User))
                                      .Single(x => !x.IsRead);

                newNotification.IsRead = true;
            }

            await _context.SaveChangesAsync();

            var model = new NotificationsDetails
            {
                Notification = notification,
                Users        = _context.ApplicationUsers
                               .OrderBy(x => x.UserName)
                               .ToList(),
                NotificationTypes = _context.NotificationTypes
                                    .Where(x => !x.IsArchived)
                                    .OrderBy(x => x.Title)
                                    .ToList()
            };

            return(View(model));
        }