public async Task <IActionResult> DeleteAppointment(int appointmentId) { IDatabaseContext context = _getDatabaseContext(); Appointment? appointment = await context.Appointments .Include(a => a.Event) .ThenInclude(e => e.Organizer) .Include(a => a.AppointmentParticipations) .FirstOrDefaultAsync(e => e.Id == appointmentId); if (appointment == null) { return(BadRequest(RequestStringMessages.AppointmentNotFound)); } User currentUser = await HttpContext.GetCurrentUserAsync(context); Event @event = appointment.Event; if (@event.Organizer != currentUser) { _logger.LogInformation("{0}(): Tried to delete appointment {1}, which he's not organizing", nameof(DeleteAppointment), appointment.Id); return(BadRequest(RequestStringMessages.OrganizerRequired)); } AppointmentNotificationInformation notificationInformation = _deleteService.DeleteAppointmentLocally(context, appointment); await context.SaveChangesAsync(); _auditLogger.LogInformation("{0}(): Canceled appointment {1}", nameof(DeleteAppointment), appointment.Id); await _notificationService.NotifyAppointmentExplicitlyCanceledAsync(notificationInformation, @event); return(Ok()); }
public AppointmentNotificationInformation DeleteAppointmentLocally(IDatabaseContext context, Appointment appointment) { // Backup data relevant for sending notifications var notificationInformation = new AppointmentNotificationInformation(appointment.Id, appointment.StartTime, appointment.AppointmentParticipations.Select(ExtractNotificationInformation).ToList()); List <AppointmentParticipation> participations = appointment.AppointmentParticipations.ToList(); context.AppointmentParticipations.RemoveRange(participations); context.Appointments.Remove(appointment); return(notificationInformation); }