public async Task <IActionResult> DeleteMeetings([FromQuery] string userId) { if (userId == null) { return(BadRequest("Missing 'userId' query parameter")); } // Check user is a teacher bool isTeacher; try { isTeacher = await _usersService.IsTeacher(userId); } catch { return(StatusCode(500, "Couldn't check the user's role")); } if (!isTeacher) { return(StatusCode(403, "Only teachers can delete meetings")); } // Check they have future meetings to delete List <Meeting> theirFutureMeetings = _meetingsService.GetByOrganizer(userId) .FindAll(m => m.StartTime > DateTime.Today); if (!theirFutureMeetings.Any()) { return(NotFound("The user has no future meetings to delete")); } foreach (Meeting m in theirFutureMeetings) { _meetingsService.Remove(m); } // Publish serialized event for each attendee of each meeting List <string> studentIds = await _usersService.GetAllStudents(); foreach (String studentId in studentIds) { foreach (Meeting meeting in theirFutureMeetings) { RemoveTaskEvent _event = new RemoveTaskEvent( studentId, meeting.Id); var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; string jsonString = JsonSerializer.Serialize(_event, options); _msgQueueService.ProduceMessage(jsonString); } } return(NoContent()); }
public async Task <IActionResult> DeleteMeeting(string id, [FromQuery] string userId) { if (userId == null) { return(BadRequest("Missing 'userId' query parameter")); } var meeting = _meetingsService.Get(id); if (meeting == null) { return(NotFound("The id does not belong to any existing meeting")); } // Check user is the meeting's organizer if (!meeting.Organizer.Equals(userId)) { return(StatusCode(403, "You must be the meeting's organizer to remove it")); } // Check it is a future meeting if (meeting.StartTime <= DateTime.Today) { return(StatusCode(403, "Only future meetings can be removed")); } _meetingsService.Remove(meeting.Id); // Publish serialized event for each attendee List <string> studentIds = await _usersService.GetAllStudents(); foreach (String studentId in studentIds) { RemoveTaskEvent _event = new RemoveTaskEvent( studentId, meeting.Id); var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; string jsonString = JsonSerializer.Serialize(_event, options); _msgQueueService.ProduceMessage(jsonString); } return(NoContent()); }
public async Task <ActionResult <Meeting> > AddAttendee( string id, [FromQuery] string userId, BookingDTO dto) { if (userId == null) { return(BadRequest("Missing 'userId' query parameter")); } // Check role of attendee inside DTO bool attendeeIsStudent; try { attendeeIsStudent = await _usersService.IsStudent(dto.StudentId); } catch { return(StatusCode(500, "Couldn't check the user's role")); } var meeting = _meetingsService.Get(id); // Check attendee is a student if (!attendeeIsStudent) { return(StatusCode(403, "Only students can attend meetings")); } // Check user performing request can add attendees if (!userId.Equals(meeting.Organizer) && !userId.Equals(dto.StudentId)) { return(StatusCode(403, "To add an attendee, you must be the organizer or be the student being added")); } // Check meeting and interval exist int indexOfInterval = Array.FindIndex(meeting.Intervals, i => i.StartTime.Equals(dto.IntervalStartTime.ToUniversalTime())); if (meeting == null || indexOfInterval == -1) { return(NotFound("The meeting or interval do not exist")); } // Check it is a future meeting if (meeting.StartTime <= DateTime.Today || meeting.EndTime <= DateTime.Today) { return(StatusCode(403, "You can only add attendees to future meetings")); } // If student, check booking period is still open bool isStudent = userId.Equals(dto.StudentId); if (isStudent) { if (meeting.BookingEndTime <= DateTime.Today || meeting.BookingStartTime > DateTime.Today) { return(StatusCode(403, "Students can only book for meetings during the booking period")); } } // Check interval has a free spot int indexOfSpot = Array.FindIndex( meeting.Intervals[indexOfInterval].Attendees, booking => booking == null); if (indexOfSpot == -1) { return(StatusCode(403, "The meeting is fully booked")); } // Check student hasn't booked for this meeting yet bool alreadyBooked = meeting.Intervals.ToList().Any(interval => interval.Attendees.ToList().Any(booking => booking != null && booking.StudentId.Equals(dto.StudentId))); if (alreadyBooked) { return(StatusCode(403, "Student already has a booking for this meeting")); } // Check student hasn't booked for another meeting // overlapping in time with this one List <Meeting> bookedMeetings = _meetingsService.Filter( "{ \"Intervals.Attendees.StudentId\": \"" + dto.StudentId + "\" }"); foreach (Meeting m in bookedMeetings) { foreach (Interval i in m.Intervals) { foreach (Booking b in i.Attendees) { if (TimeOverlap(i.StartTime, i.EndTime, dto.IntervalStartTime, meeting.Intervals[indexOfInterval].EndTime)) { return(StatusCode(403, "Student has a booking for another meeting that overlaps in time with this one")); } } } } // Fill spot Booking newBooking = new Booking(); newBooking.StudentId = dto.StudentId; if (isStudent) { newBooking.Comment = dto.Comment; } meeting.Intervals[indexOfInterval].Attendees[indexOfSpot] = newBooking; _meetingsService.Update(id, meeting); RemoveTaskEvent _event = new RemoveTaskEvent( dto.StudentId, meeting.Id); var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; string jsonString = JsonSerializer.Serialize(_event, options); _msgQueueService.ProduceMessage(jsonString); return(Ok(meeting)); }