public async Task CreateRequestAsync(Entreaty request, int userId) { request.EntreatyType = EntreatyType.REQUEST; NotificationContext notificationContext = new NotificationContext() { CreatedBy = await _context.Users.FindAsync(userId), Data = string.Format("{0} has requested to join your group", request.Student.FirstName), NotificationType = NotificationType.ENTREATY, Time = DateTime.UtcNow }; foreach (Student s in request.Group.Students) { notificationContext.NotificationsSent.Add(new Notification() { NotificationContext = notificationContext, Read = false, Receiver = await _context.Users.FindAsync(s.Id) }); } request.NotificationContext = notificationContext; await _context.AddAsync(request); 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 EntreatyResponse(Entreaty entreaty) { Id = entreaty.Id; Message = entreaty.Message; Accepted = entreaty.Accepted; Student = entreaty.Student.Id; Group = entreaty.Group.Id; EntreatyType = entreaty.EntreatyType; Time = entreaty.NotificationContext.Time; }
public async Task <IActionResult> RejectEntreaty([FromRoute] int groupId, [FromQuery][Required] int?studentId) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Student user = await _studentService.GetByIdAsync(Int32.Parse(User.Identity.Name)); Student student = await _studentService.GetByIdAsync(studentId ?? 0); if (student == null) { return(BadRequest("Student does not exist")); } Group group = await _groupService.GetByIdAsync(groupId); if (group == null) { return(BadRequest("Group does not exist")); } Student groupAdmin = group.Students.Where(s => s.GroupAdmin == true).First(); if (user.Id != groupAdmin.Id && user.Id != student.Id) { return(BadRequest("Action not allowed")); } if (!await _entreatyService.ExistsAsync(studentId ?? 0, groupId)) { return(BadRequest("The entreaty does not exist")); } Entreaty entreaty = group.Entreaties.Where(e => e.StudentId == studentId).First(); if (student.Group != null) { return(BadRequest("Student is already part of a group")); } if (group.Students.Count >= 4) { return(BadRequest("More members not allowed")); } if (entreaty.EntreatyType == EntreatyType.REQUEST && user.Id != groupAdmin.Id) { return(BadRequest("Only Group Admins allowed to reject")); } if (entreaty.EntreatyType == EntreatyType.INVITE && user.Id != student.Id) { return(BadRequest("Action not allowed")); } await _entreatyService.RejectAsync(entreaty); return(Ok()); }
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 <IActionResult> CreateInviteAsync([FromRoute] int studentId, [FromBody][Required] string message) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Student groupAdmin = await _studentService.GetByIdAsync(Int32.Parse(User.Identity.Name)); if (!groupAdmin.GroupAdmin) { return(BadRequest("Only group admins allowed")); } Student student = await _studentService.GetByIdAsync(studentId); if (student == null) { return(BadRequest("Student does not exist")); } if (!student.Program.Equals(groupAdmin.Program)) { return(BadRequest("Not in your program")); } if (student.Group != null) { return(BadRequest("Student is already part of a group")); } if (groupAdmin.Group.Students.Count >= 4) { return(BadRequest("More members not allowed")); } if (await _entreatyService.ExistsAsync(studentId, groupAdmin.Group.Id)) { return(BadRequest("A request/invite between this group and student already exists")); } Entreaty invite = new Entreaty() { Accepted = false, Group = groupAdmin.Group, Message = message, Student = student, }; await _entreatyService.CreateInviteAsync(invite, groupAdmin.Id); return(Ok()); }
public async Task <IActionResult> CreateRequestAsync([FromRoute] int groupId, [FromBody][Required] string message) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Student student = await _studentService.GetByIdAsync(Int32.Parse(User.Identity.Name)); Group group = await _groupService.GetByIdAsync(groupId); if (group == null) { return(BadRequest("Group does not exist")); } Student groupAdmin = group.Students.Where(s => s.GroupAdmin == true).FirstOrDefault(); if (!student.Program.Equals(groupAdmin.Program)) { return(BadRequest("Not in your program")); } if (student.Group != null) { return(BadRequest("You are already part of a group")); } if (group.Students.Count >= 4) { return(BadRequest("More members not allowed")); } if (await _entreatyService.ExistsAsync(student.Id, groupId)) { return(BadRequest("A request/invite between this group and student already exists")); } Entreaty request = new Entreaty() { Accepted = false, Group = group, Message = message, Student = student, }; await _entreatyService.CreateRequestAsync(request, student.Id); return(Ok()); }
public async Task CreateInviteAsync(Entreaty invite, int userId) { invite.EntreatyType = EntreatyType.INVITE; NotificationContext notificationContext = new NotificationContext() { CreatedBy = await _context.Users.FindAsync(userId), Data = string.Format("{0} has sent an invite", invite.Group.Name), NotificationType = NotificationType.ENTREATY, Time = DateTime.UtcNow }; foreach (Student s in invite.Group.Students) { notificationContext.NotificationsSent.Add(new Notification() { NotificationContext = notificationContext, Read = false, Receiver = await _context.Users.FindAsync(s.Id) }); } notificationContext.NotificationsSent.Add(new Notification() { NotificationContext = notificationContext, Read = false, Receiver = await _context.Users.FindAsync(invite.Student.Id) }); invite.NotificationContext = notificationContext; await _context.AddAsync(invite); 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"); } } }