示例#1
0
        /// <summary>
        /// Returns an <see cref="List{TEntity}"/> based on a custom filtering function applied to the query, as well as
        /// optional select and expand arguments, checking the user READ permissions along the way
        /// </summary>
        /// <param name="filterFunc">Allows any kind of filtering on the query</param>
        /// <param name="expand">Optional expand argument</param>
        /// <param name="select">Optional select argument</param>
        /// <param name="orderby">Optional select argument</param>
        /// <param name="permissionsFilter">Optional filter argument, if null is passed the q</param>
        /// <param name="cancellation">Optional select argument</param>
        protected async Task <List <TEntity> > GetEntitiesByCustomQuery(
            Func <Query <TEntity>, Query <TEntity> > filterFunc,
            ExpressionExpand expand,
            ExpressionSelect select,
            ExpressionOrderBy orderby,
            ExpressionFilter permissionsFilter,
            CancellationToken cancellation)
        {
            // Prepare a query of the result, and clone it
            var repo  = GetRepository();
            var query = repo.Query <TEntity>();

            // Apply custom filter function
            query = filterFunc(query);

            // Apply read permissions
            permissionsFilter ??= await UserPermissionsFilter(Constants.Read, cancellation);

            query = query.Filter(permissionsFilter);

            // Expand, Select and Order the result as specified in the OData agruments
            var expandedQuery = query.Expand(expand).Select(select).OrderBy(orderby ?? ExpressionOrderBy.Parse("Id")); // Required

            // Load the result into memory
            var data = await expandedQuery.ToListAsync(cancellation); // this is potentially unordered, should that be a concern?

            // TODO: This is slow and unused
            // Apply the permission masks (setting restricted fields to null) and adjust the metadata accordingly
            // await ApplyReadPermissionsMask(data, query, permissions, GetDefaultMask(), cancellation);

            // Return
            return(data);
        }
        /// <summary>
        /// Returns an <see cref="List{TEntity}"/> based on a custom <paramref name="filterFunc"/> applied to the query, as well as
        /// optional <paramref name="expand"/> and <paramref name="select"/> arguments, checking the user's READ permissions along the way.
        /// </summary>
        /// <param name="filterFunc">Allows any kind of filtering on the query</param>
        /// <param name="expand">Optional expand argument.</param>
        /// <param name="select">Optional select argument.</param>
        /// <param name="orderby">Optional orderby argument.</param>
        /// <param name="permissionsFilter">Optional filter argument, if null is passed the query uses the read permissions filter of the current user.</param>
        /// <param name="cancellation">The cancellation instruction.</param>
        protected async Task <List <TEntity> > GetEntitiesByCustomQuery(
            Func <EntityQuery <TEntity>, EntityQuery <TEntity> > filterFunc,
            ExpressionExpand expand,
            ExpressionSelect select,
            ExpressionOrderBy orderby,
            ExpressionFilter permissionsFilter,
            CancellationToken cancellation)
        {
            // Prepare a query of the result, and clone it
            var factory = QueryFactory();
            var query   = factory.EntityQuery <TEntity>();

            // Apply custom filter function
            query = filterFunc(query);

            // Apply read permissions
            permissionsFilter ??= await UserPermissionsFilter(PermissionActions.Read, cancellation);

            query = query.Filter(permissionsFilter);

            // Expand, Select and Order the result as specified in the Queryex agruments
            var expandedQuery = query.Expand(expand).Select(select).OrderBy(orderby ?? ExpressionOrderBy.Parse("Id")); // Required

            // Load the result into memory
            var data = await expandedQuery.ToListAsync(QueryContext(), cancellation); // this is potentially unordered, should that be a concern?

            // Return
            return(data);
        }
示例#3
0
        public async Task <IEnumerable <TDto> > QueryAsync(string[] fields)
        {
            CheckNull(fields);

            var tEntities = await Repository.QueryAsync();

            var typeMap = Mapper.ConfigurationProvider.ResolveTypeMap(typeof(TDto), typeof(TEntity));

            var selector = ExpressionSelect.CreateSelecter <TEntity, TDto>(typeMap, fields.ToList());

            return(tEntities.Select(selector).AsEnumerable());

            // return Mapper.Map<IEnumerable<TDto>>(tEntities.AsEnumerable());
        }
示例#4
0
        public async Task <IEnumerable <TDto> > QueryAsync(Expression <Func <TDto, bool> > whereExpression, string[] fields)
        {
            CheckNull(fields);

            ExpressionConverter <TDto, TEntity> expressionConverter = new ExpressionConverter <TDto, TEntity>(Mapper);

            var newExperssion = (Expression <Func <TEntity, bool> >)expressionConverter.Visit(whereExpression);

            var tEntities = await Repository.QueryAsync(newExperssion);

            var typeMap = Mapper.ConfigurationProvider.ResolveTypeMap(typeof(TDto), typeof(TEntity));

            var selector = ExpressionSelect.CreateSelecter <TEntity, TDto>(typeMap, fields.ToList());

            return(tEntities.Select(selector).AsEnumerable());
        }
示例#5
0
        /// <summary>
        /// Returns a <see cref="List{TEntity}"/> as per the Ids and the specifications in the <see cref="ExpressionExpand"/> and <see cref="ExpressionSelect"/>,
        /// after verifying the user's permissions, returns the entities in the same order as the supplied Ids.
        /// If null was supplied for permission filter, the function by default uses the filter for the read permissions filter
        /// </summary>
        protected virtual async Task <List <TEntity> > GetEntitiesByIds(
            List <TKey> ids,
            ExpressionExpand expand,
            ExpressionSelect select,
            ExpressionFilter permissionsFilter,
            CancellationToken cancellation)
        {
            if (ids == null || ids.Count == 0)
            {
                return(new List <TEntity>());
            }
            else
            {
                var data = await GetEntitiesByCustomQuery(q => q.FilterByIds(ids), expand, select, null, permissionsFilter, cancellation);

                // If the data is only
                if (ids.Count == 1 && data.Count == 1)
                {
                    // No need to sort
                    return(data);
                }
                else
                {
                    // Sort the entities according to the original Ids, as a good practice
                    TEntity[] dataSorted = new TEntity[ids.Count];
                    Dictionary <TKey, TEntity> dataDic = data.ToDictionary(e => e.Id);
                    for (int i = 0; i < ids.Count; i++)
                    {
                        var id = ids[i];
                        if (dataDic.TryGetValue(id, out TEntity entity))
                        {
                            dataSorted[i] = entity;
                        }
                    }

                    return(dataSorted.Where(e => e != null).ToList());
                }
            }
        }