Beispiel #1
0
        /// <summary>
        /// Returns an entities response based on custom filtering function applied to the query, as well as
        /// optional select and expand arguments, checking the user permissions along the way
        /// </summary>
        /// <param name="filterFunc">Allows you to apply any filteration you like to the query,</param>
        /// <param name="expand">Optional expand argument</param>
        /// <param name="select">Optional select argument</param>
        protected async Task <EntitiesResponse <TEntity> > GetByCustomQuery(Func <Query <TEntity>, Query <TEntity> > filterFunc, ExpandExpression expand, SelectExpression select, OrderByExpression orderby = null)
        {
            // Prepare a query of the result, and clone it
            var repo  = GetRepository();
            var query = repo.Query <TEntity>();

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

            // Expand the result as specified in the OData agruments and load into memory
            var expandedQuery = query.Expand(expand);

            expandedQuery = expandedQuery.Select(select);
            expandedQuery = expandedQuery.OrderBy(orderby ?? OrderByExpression.Parse("Id")); // Required
            var result = await expandedQuery.ToListAsync();                                  // this is potentially unordered, should that be a concern?

            // Apply the permissions on the result
            var permissions = await UserPermissions(Constants.Read);

            var defaultMask = GetDefaultMask();

            await ApplyReadPermissionsMask(result, query, permissions, defaultMask);

            // Flatten and Trim
            var relatedEntities = FlattenAndTrim(result, expand);

            // Prepare the result in a response object
            return(new EntitiesResponse <TEntity>
            {
                Result = result,
                RelatedEntities = relatedEntities,
                CollectionName = GetCollectionName(typeof(TEntity))
            });
        }
Beispiel #2
0
        /// <summary>
        /// Returns a single entity as per the ID and specifications in the get request
        /// </summary>
        protected virtual async Task <EntitiesResponse <TEntity> > GetChildrenOfAsync(GetChildrenArguments <TKey> args)
        {
            // Parse the parameters
            var expand  = ExpandExpression.Parse(args.Expand);
            var select  = SelectExpression.Parse(args.Select);
            var filter  = FilterExpression.Parse(args.Filter);
            var orderby = OrderByExpression.Parse("Node");
            var ids     = args.I ?? new List <TKey>();

            return(await GetByCustomQuery(q => q.FilterByParentIds(ids, args.Roots).Filter(filter), expand, select, orderby));
        }
Beispiel #3
0
        protected override OrderByExpression DefaultOrderBy()
        {
            // By default: Order currencies by name
            var    tenantInfo   = _repo.GetTenantInfo();
            string nameProperty = nameof(Currency.Name);

            if (tenantInfo.SecondaryLanguageId == CultureInfo.CurrentUICulture.Name)
            {
                nameProperty = $"{nameof(Currency.Name2)},{nameof(Currency.Name)}";
            }
            else if (tenantInfo.TernaryLanguageId == CultureInfo.CurrentUICulture.Name)
            {
                nameProperty = $"{nameof(Currency.Name3)},{nameof(Currency.Name)}";
            }

            return(OrderByExpression.Parse(nameProperty));
        }
Beispiel #4
0
        protected override OrderByExpression DefaultOrderBy()
        {
            // By default: Order report definitions by name
            var    tenantInfo = _repo.GetTenantInfo();
            string orderby    = $"{nameof(ReportDefinition.Title)},{nameof(ReportDefinition.Id)}";

            if (tenantInfo.SecondaryLanguageId == CultureInfo.CurrentUICulture.Name)
            {
                orderby = $"{nameof(ReportDefinition.Title2)},{nameof(ReportDefinition.Title)},{nameof(ReportDefinition.Id)}";
            }
            else if (tenantInfo.TernaryLanguageId == CultureInfo.CurrentUICulture.Name)
            {
                orderby = $"{nameof(ReportDefinition.Title3)},{nameof(ReportDefinition.Title)},{nameof(ReportDefinition.Id)}";
            }

            return(OrderByExpression.Parse(orderby));
        }
Beispiel #5
0
        // Endpoint implementations

        /// <summary>
        /// Returns the entities as per the specifications in the get request
        /// </summary>
        protected virtual async Task <GetResponse <TEntity> > GetImplAsync(GetArguments args, Query <TEntity> queryOverride = null)
        {
            // Parse the parameters
            var filter  = FilterExpression.Parse(args.Filter);
            var orderby = OrderByExpression.Parse(args.OrderBy);
            var expand  = ExpandExpression.Parse(args.Expand);
            var select  = SelectExpression.Parse(args.Select);

            // Prepare the query
            var query = queryOverride ?? GetRepository().Query <TEntity>();

            // Retrieve the user permissions for the current view
            var permissions = await UserPermissions(Constants.Read);

            // Filter out permissions with masks that would be violated by the filter or order by arguments
            var defaultMask = GetDefaultMask() ?? new MaskTree();

            permissions = FilterViolatedPermissionsForFlatQuery(permissions, defaultMask, filter, orderby);

            // Apply read permissions
            var permissionsFilter = GetReadPermissionsCriteria(permissions);

            query = query.Filter(permissionsFilter);

            // Search
            query = Search(query, args, permissions);

            // Filter
            query = query.Filter(filter);

            // Before ordering or paging, retrieve the total count
            int totalCount = await query.CountAsync();

            // OrderBy
            query = OrderBy(query, orderby);

            // Apply the paging (Protect against DOS attacks by enforcing a maximum page size)
            var top  = args.Top;
            var skip = args.Skip;

            top   = Math.Min(top, MaximumPageSize());
            query = query.Skip(skip).Top(top);

            // Apply the expand, which has the general format 'Expand=A,B/C,D'
            var expandedQuery = query.Expand(expand);

            // Apply the select, which has the general format 'Select=A,B/C,D'
            expandedQuery = expandedQuery.Select(select);

            // Load the data in memory
            var result = await expandedQuery.ToListAsync();

            // Apply the permission masks (setting restricted fields to null) and adjust the metadata accordingly
            await ApplyReadPermissionsMask(result, query, permissions, defaultMask);

            // Flatten and Trim
            var relatedEntities = FlattenAndTrim(result, expand);

            // Prepare the result in a response object
            return(new GetResponse <TEntity>
            {
                Skip = skip,
                Top = result.Count(),
                OrderBy = args.OrderBy,
                TotalCount = totalCount,

                Result = result,
                RelatedEntities = relatedEntities,
                CollectionName = GetCollectionName(typeof(TEntity))
            });
        }
Beispiel #6
0
 protected override OrderByExpression DefaultOrderBy()
 {
     return(OrderByExpression.Parse(nameof(DetailsEntry.AccountId)));
 }
Beispiel #7
0
 protected override OrderByExpression DefaultOrderBy()
 {
     return(OrderByExpression.Parse("Id desc"));
 }
Beispiel #8
0
 protected override OrderByExpression DefaultOrderBy()
 {
     return(OrderByExpression.Parse($"{nameof(Document.DocumentDate)} desc"));
 }
Beispiel #9
0
 protected override OrderByExpression DefaultOrderBy()
 {
     return(OrderByExpression.Parse(nameof(Account.Code)));
 }