Ejemplo n.º 1
0
    public Task <CrudSearchResult <T> > Search(CrudSearchOptions options)
    {
        var result = new CrudSearchResult <T>
        {
            Skip = options.Skip,
            Take = options.Take,
        };

        var query = Table.Values.AsQueryable();

        if (!string.IsNullOrWhiteSpace(options.SearchQuery))
        {
            Expression?condition = null;
            var        paramater = Parameter(typeof(T), "x");

            // TODO: Configureable in Entity Defintion
            foreach (var property in typeof(T).GetProperties())
            {
                if (property.PropertyType.IsAssignableTo(typeof(string)))
                {
                    // TODO: I'd hate to make people conform to this paradigm when implementing their own repos
                    // Maybe there is a way for the expressions to be build when the entity definition is built
                    // Then they can just use a where clause in their repo.
                    var match = Call(
                        Property(paramater, property),
                        typeof(string).GetMethod("Contains", new [] { typeof(string), typeof(StringComparison) }),
                        new Expression[] {
                        Constant(options.SearchQuery, typeof(string)),
                        Constant(StringComparison.InvariantCultureIgnoreCase, typeof(StringComparison))
                    });

                    condition = condition == null ? match : Or(condition, match);
                }
            }

            if (condition != null)
            {
                query = query.Where((Expression <Func <T, bool> >)Lambda(condition, "WhereClause", new [] { paramater }));
            }
        }

        result.Count = query.Count();

        if (options.Skip > 0)
        {
            query = query.Skip(options.Skip);
        }

        if (options.Take > 0)
        {
            query = query.Take(options.Take);
        }

        result.Results = query.ToAsyncEnumerable();

        return(Task.FromResult(result));
    }
Ejemplo n.º 2
0
    public async Task <ListViewModel> Build(CrudSearchResult <TEntity> result, int page, int pageSize)
    {
        var definition = entityDefinitionFactory.CreateFor <TEntity>();

        return(new ListViewModel
        {
            DisplayName = definition.DisplayName,
            Columns = CreateListViewColumnFromType(definition),
            PageCount = ((result.Count - 1) / pageSize) + 1,
            CurrentPage = page,
            Results = await result.Results.Select(e => e.ToDictionary()).ToListAsync(),
        });
    }
    public async Task Search_RetrievesEntitiesFromRepositoryUsingSearchOptions()
    {
        // Arrange
        var searchOptions = new CrudSearchOptions();
        var expected      = new CrudSearchResult <BlankEntity>();

        repository.Setup(x => x.Search(searchOptions)).ReturnsAsync(expected);

        // Act
        var response = await service.Search(searchOptions);

        // Assert
        Assert.Equal(expected, response);
    }