public static IQueryable <EventCollection> UseOptions(
            this IQueryable <EventCollection> query,
            EventCollectionRetrievalOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (!options.ForUpdate)
            {
                query = query.AsNoTracking();
            }

            if (options.LoadEvents)
            {
                query = query.Include(c => c.Events);
            }

            if (options.LoadMappings)
            {
                query = query.Include(c => c.EventMappings);
            }

            return(query);
        }
        public async Task <EventCollection[]> ListCollectionsAsync(
            EventCollectionListRequest request,
            EventCollectionRetrievalOptions options,
            CancellationToken cancellationToken)
        {
            request ??= new EventCollectionListRequest();
            options ??= new EventCollectionRetrievalOptions();

            var query = _context.EventCollections
                        .UseOptions(options ?? new EventCollectionRetrievalOptions())
                        .UseFilter(request.Filter ?? new EventCollectionFilter())
                        .UseOrder(request.Order, request.Descending);

            query = await _eventCollectionAccessControlService
                    .AddAccessFilterAsync(query, cancellationToken);

            return(await query.ToArrayAsync(cancellationToken));
        }
        public async Task <EventCollection> GetCollectionByIdAsync(int id,
                                                                   EventCollectionRetrievalOptions options,
                                                                   CancellationToken cancellationToken)
        {
            options ??= new EventCollectionRetrievalOptions();

            var collection = await _context.EventCollections
                             .UseOptions(options)
                             .FirstOrDefaultAsync(c => c.CollectionId == id, cancellationToken);

            if (options.ForUpdate)
            {
                await _eventCollectionAccessControlService
                .CheckEventCollectionUpdateAccessAsync(collection, cancellationToken);
            }

            return(collection ?? throw
                   new NotFoundException($"Collection {id} not found."));
        }