Esempio n. 1
0
        public EventNotificationInformation DeleteEventLocally(IDatabaseContext context, Event @event)
        {
            // Backup data relevant for sending notifications
            EventNotificationInformation notificationInformation = ExtractNotificationInformation(@event);

            // Participations for the event
            List <EventParticipation> participations = @event.EventParticipations.ToList();

            context.EventParticipations.RemoveRange(participations);

            // Appointments of the even
            foreach (Appointment eventAppointment in @event.Appointments.ToList())
            {
                // Ignore notification information as we don't send appointment canceled if the whole event gets deleted
                DeleteAppointmentLocally(context, eventAppointment);
            }

            // Invitations to the event
            context.EventInvitations.RemoveRange(@event.EventInvitations);

            // Event itself
            context.Events.Remove(@event);

            return(notificationInformation);
        }
        public async Task <IActionResult> DeleteEvent(int eventId)
        {
            IDatabaseContext context = _getDatabaseContext();
            Event?           @event  = await context.Events
                                       .Include(e => e.Organizer)
                                       .Include(e => e.EventInvitations)
                                       .Include(e => e.EventParticipations)
                                       .ThenInclude(ep => ep.Participant)
                                       .Include(e => e.Appointments)
                                       .ThenInclude(a => a.AppointmentParticipations)
                                       .Include(e => e.ChatMessages)
                                       .FirstOrDefaultAsync(e => e.Id == eventId);

            if (@event == null)
            {
                return(BadRequest(RequestStringMessages.EventNotFound));
            }

            if (@event.OrganizerId != HttpContext.GetUserId())
            {
                _logger.LogInformation("{0}(): Tried to delete event {1}, which he's not organizing", nameof(DeleteEvent), @event.Id);

                return(BadRequest(RequestStringMessages.OrganizerRequired));
            }

            EventNotificationInformation notificationInformation = _deleteService.DeleteEventLocally(context, @event);

            await context.SaveChangesAsync();

            _auditLogger.LogInformation("{0}(): Deleted event {1}", nameof(DeleteEvent), @event.Id);

            await _notificationService.NotifyEventDeletedAsync(notificationInformation);

            return(Ok());
        }
Esempio n. 3
0
        public List <EventNotificationInformation> DeleteUserLocally(IDatabaseContext context, User user)
        {
            // Backup data relevant for sending notifications
            var deletedEventNotificationInfos = new List <EventNotificationInformation>();

            foreach (Event organizedEvent in user.OrganizedEvents.ToList())
            {
                EventNotificationInformation deletedInfo = DeleteEventLocally(context, organizedEvent);
                deletedEventNotificationInfos.Add(deletedInfo);
            }

            // Token based relations
            context.Sessions.RemoveRange(user.Sessions);
            context.PasswordResets.RemoveRange(user.PasswordResets);

            // Event participations the user is part of
            context.EventParticipations.RemoveRange(user.EventParticipations);

            // Appointment participations the user is part of
            List <AppointmentParticipation> userAppointmentParticipations = user.AppointmentParticipations.ToList();

            context.AppointmentParticipations.RemoveRange(userAppointmentParticipations);

            // User himself
            context.Users.Remove(user);

            return(deletedEventNotificationInfos);
        }