Exemple #1
0
        public async Task AnnounceToAllStudentsAsync(string message, int userId)
        {
            NotificationContext notificationContext = new NotificationContext()
            {
                CreatedBy        = await _context.Users.Where(u => u.Admin == true).FirstAsync(),
                Data             = message,
                NotificationType = NotificationType.ANNOUNCEMENT,
                Time             = DateTime.UtcNow
            };

            foreach (Student s in _context.Students)
            {
                notificationContext.NotificationsSent.Add(new Notification()
                {
                    Read     = false,
                    Receiver = await _context.Users.FindAsync(s.Id)
                });
            }

            _context.Add(notificationContext);
            await SaveAsync();

            foreach (Notification n in notificationContext.NotificationsSent)
            {
                if (_hubContext.Clients.Group(n.ReceiverId.ToString()) != null)
                {
                    await _hubContext.Clients.Group(n.ReceiverId.ToString()).SendAsync("ReceiveNotification");
                }
            }
        }
        public async Task RejectAsync(Entreaty entreaty)
        {
            Group        group      = entreaty.Group;
            Student      student    = entreaty.Student;
            Student      groupAdmin = group.Students.Where(s => s.GroupAdmin == true).FirstOrDefault();
            EntreatyType type       = entreaty.EntreatyType;

            _context.Remove(entreaty);
            await SaveAsync();

            NotificationContext notificationContext = new NotificationContext()
            {
                NotificationType = NotificationType.ENTREATY,
                Time             = DateTime.UtcNow
            };

            foreach (Student s in group.Students)
            {
                notificationContext.NotificationsSent.Add(new Notification()
                {
                    NotificationContext = notificationContext,
                    Read     = false,
                    Receiver = await _context.Users.FindAsync(s.Id)
                });
            }
            if (type == EntreatyType.REQUEST)
            {
                notificationContext.CreatedBy = _context.Users.Find(groupAdmin.Id);
                notificationContext.Data      = string.Format("{0} has rejected request", group.Name);
                notificationContext.NotificationsSent.Add(new Notification()
                {
                    NotificationContext = notificationContext,
                    Read     = false,
                    Receiver = student.User
                });
            }
            else
            {
                notificationContext.CreatedBy = _context.Users.Find(student.Id);
                notificationContext.Data      = string.Format("{0} {1} has rejected invite", student.FirstName, student.LastName);
            }
            _context.Add(notificationContext);
            await SaveAsync();

            foreach (Notification n in notificationContext.NotificationsSent)
            {
                if (_hubContext.Clients.Group(n.ReceiverId.ToString()) != null)
                {
                    await _hubContext.Clients.Group(n.ReceiverId.ToString()).SendAsync("ReceiveNotification");
                }
            }
        }
        public async Task SendNotificationsToRangeOfStudentsAsync(NotificationContext notificationContext, ICollection <Student> students)
        {
            foreach (Student s in students)
            {
                notificationContext.NotificationsSent.Add(new Notification()
                {
                    Read     = false,
                    Receiver = await _context.Users.FindAsync(s.Id),
                });
            }
            _context.Add(notificationContext);
            await SaveAsync();

            foreach (Notification n in notificationContext.NotificationsSent)
            {
                if (_hubContext.Clients.Group(n.ReceiverId.ToString()) != null)
                {
                    await _hubContext.Clients.Group(n.ReceiverId.ToString()).SendAsync("ReceiveNotification");
                }
            }
        }