public async Task <IActionResult> DeleteAppointmentFinder(int appointmentFinderId) { IDatabaseContext context = _getDatabaseContext(); AppointmentFinder appointmentFinder = await context.AppointmentFinders .Include(a => a.Event) .ThenInclude(e => e.Organizer) .Include(a => a.TimeSlots) .FirstOrDefaultAsync(e => e.Id == appointmentFinderId); if (appointmentFinder == null) { return(BadRequest(RequestStringMessages.AppointmentNotFound)); } User currentUser = await HttpContext.GetCurrentUserAsync(context); Event @event = appointmentFinder.Event; if (@event.Organizer != currentUser) { _logger.LogInformation("{0}(): Tried to delete appointmentFinder {1}, which he's not organizing", nameof(DeleteAppointmentFinder), appointmentFinder.Id); return(BadRequest(RequestStringMessages.OrganizerRequired)); } context.TimeSlots.RemoveRange(appointmentFinder.TimeSlots); context.AppointmentFinders.Remove(appointmentFinder); await context.SaveChangesAsync(); _auditLogger.LogInformation("{0}(): Canceled appointmentFinder {1}", nameof(DeleteAppointmentFinder), appointmentFinder.Id); return(Ok()); }
public async Task <IActionResult> CreateAppointmentFinder(AppointmentFinderInformationDto appointmentFinderInformationDto) { // TODO Input validation if (appointmentFinderInformationDto.TimeSlots.Any(a => a.FromDate < DateTime.UtcNow)) { return(BadRequest(RequestStringMessages.AppointmentsHaveToStartInTheFuture)); } IDatabaseContext context = _getDatabaseContext(); Event @event = await context.Events.Include(e => e.Organizer).FirstOrDefaultAsync(e => e.Id == appointmentFinderInformationDto.EventId); if (@event == null) { return(BadRequest(RequestStringMessages.EventNotFound)); } User currentUser = await HttpContext.GetCurrentUserAsync(context); if (@event.Organizer != currentUser) { _logger.LogInformation("{0}(): Tried to add appointments to the event {1}, which he's not organizing", nameof(CreateAppointmentFinder), @event.Id); return(BadRequest(RequestStringMessages.OrganizerRequired)); } var finder = new AppointmentFinder { Event = @event, FromDate = appointmentFinderInformationDto.FromDate, ToDate = appointmentFinderInformationDto.ToDate }; context.AppointmentFinders.Add(finder); foreach (var timeSlot in appointmentFinderInformationDto.TimeSlots) { context.TimeSlots.Add(new Database.Models.TimeSlot { FromDate = timeSlot.FromDate, ToDate = timeSlot.ToDate, AppointmentFinder = finder }); } await context.SaveChangesAsync(); _auditLogger.LogInformation("{0}(): Added {1} appointment finders to event {2}", nameof(CreateAppointmentFinder), appointmentFinderInformationDto.TimeSlots.Length, @event.Id); return(Ok()); }