Esempio n. 1
0
 public async Task <IActionResult> Get([FromQuery] GridifyQuery gridifyQuery)
 {
     // General usage of Gridify with AutoMapper
     return(Ok(await _context.People.AsNoTracking()
               .ProjectTo <PersonDto>(_mapper.ConfigurationProvider)
               .GridifyAsync(gridifyQuery)));
 }
Esempio n. 2
0
    /// <summary>
    /// gets a query or collection,
    /// adds filtering, ordering and paging
    /// loads filtered and sorted data
    /// return pagination ready result
    /// </summary>
    /// <param name="query">the original(target) queryable object</param>
    /// <param name="queryOption">the configuration to apply paging, filtering and ordering</param>
    /// <param name="mapper">this is an optional parameter to apply filtering and ordering using a custom mapping configuration</param>
    /// <typeparam name="T">type of target entity</typeparam>
    /// <returns>returns a loaded <c>Paging<T><c /> after applying filtering, ordering and paging </returns>
    /// <returns></returns>
    public static Paging <T> Gridify <T>(this IQueryable <T> query, Action <IGridifyQuery> queryOption, IGridifyMapper <T>?mapper = null)
    {
        var gridifyQuery = new GridifyQuery();

        queryOption.Invoke(gridifyQuery);
        var(count, queryable) = query.GridifyQueryable(gridifyQuery, mapper);
        return(new Paging <T>(count, queryable.ToList()));
    }
Esempio n. 3
0
        public async Task <Paging <UserDTO> > GetUsersPagingAsync(GridifyQuery gridifyQuery)
        {
            var queryable = await _userManager.Users.AsNoTracking().GridifyQueryableAsync(gridifyQuery, null);

            return(new Paging <UserDTO> {
                Items = queryable.Query.ProjectTo <UserDTO>(_mapper.ConfigurationProvider).ToList(),
                TotalItems = queryable.TotalItems
            });
        }
Esempio n. 4
0
        public async Task <Paging <CountryDTO> > GetCountriesListAsync(GridifyQuery gridifyQuery)
        {
            var queryable = await _dbContext.Countries.AsNoTracking().GridifyQueryableAsync(gridifyQuery, null);

            return(new Paging <CountryDTO> {
                Items = queryable.Query.ProjectTo <CountryDTO>(_mapper.ConfigurationProvider).ToList(),
                TotalItems = queryable.TotalItems
            });
        }
Esempio n. 5
0
        public async Task <IActionResult> GetPaginatedList([FromQuery] GridifyQuery gridifyQuery)
        {
            var result = _context.People.ApplyPaging(gridifyQuery);

            return(Ok(new Paging <PersonDto>()
            {
                Items = await result.AsQueryable().ProjectTo <PersonDto>(_mapper.ConfigurationProvider).ToListAsync(),
                TotalItems = await _context.People.CountAsync()
            }));
        }
Esempio n. 6
0
        public async Task <Paging <PersonDTO> > GetPeoplePagingAsync(GridifyQuery gridifyQuery)
        {
            var queryable = await _dbContext.People.AsNoTracking()
                            .Include(x => x.Categories)
                            .GridifyQueryableAsync(gridifyQuery, null);

            return(new Paging <PersonDTO> {
                Items = queryable.Query.ProjectTo <PersonDTO>(_mapper.ConfigurationProvider).ToList(),
                TotalItems = queryable.TotalItems
            });
        }
Esempio n. 7
0
        public async Task <ActionResult <Paging <GenreDetailsDTO> > > Get([FromQuery] GridifyQuery gridifyQuery)
        {
            var genres = await _genresService.GetGenresDetailsPagingAsync(gridifyQuery);

            if (genres == null)
            {
                return(UnprocessableEntity("Failed to get Genres from service."));
            }

            return(genres);
        }
Esempio n. 8
0
        public async Task <Paging <SerieDTO> > GetSeriesByGenreIdPagingAsync(int id, GridifyQuery gridifyQuery)
        {
            var queryable = await _dbContext.Series.AsNoTracking()
                            .Include(x => x.Genres)
                            .Where(x => x.Genres.Any(x => x.Id == id))
                            .GridifyQueryableAsync(gridifyQuery, null);

            return(new Paging <SerieDTO> {
                Items = queryable.Query.ProjectTo <SerieDTO>(_mapper.ConfigurationProvider).ToList(),
                TotalItems = queryable.TotalItems
            });
        }
Esempio n. 9
0
        public async Task <Paging <MovieDTO> > GetInTheatersAsync(GridifyQuery gridifyQuery)
        {
            var today     = DateTime.Today;
            var queryable = await _dbContext.Movies
                            .AsNoTracking()
                            .Where(x => x.InTheaters)
                            .GridifyQueryableAsync(gridifyQuery, null);

            return(new Paging <MovieDTO> {
                Items = queryable.Query.ProjectTo <MovieDTO>(_mapper.ConfigurationProvider).ToList(),
                TotalItems = queryable.TotalItems
            });
        }
    public void GridifyQueryableDateTimeShouldNotThrowException()
    {
        // arrange
        var gq = new GridifyQuery {
            OrderBy = "CreateDate"
        };

        // act
        var exception = Record.Exception(() => _ctx.Users.GridifyQueryable(gq));

        // assert
        Assert.Null(exception);
    }
Esempio n. 11
0
        public async Task <Paging <MovieDetailsDTO> > GetCastMoviesListAsync(int id, GridifyQuery gridifyQuery)
        {
            var queryable = await _dbContext.Movies.AsNoTracking()
                            .Where(x => x.Casts.Any(y => y.PersonId == id))
                            .GridifyQueryableAsync(gridifyQuery, null);

            return(new Paging <MovieDetailsDTO> {
                Items = queryable.Query
                        .ProjectTo <MovieDetailsDTO>(_mapper.ConfigurationProvider)
                        .ToList(),
                TotalItems = queryable.TotalItems
            });
        }
Esempio n. 12
0
        public async Task <Paging <SerieDTO> > GetUpcomingReleasesAsync(GridifyQuery gridifyQuery)
        {
            var today     = DateTime.Today;
            var queryable = await _dbContext.Series
                            .AsNoTracking()
                            .Where(x => x.StartDate > today)
                            .GridifyQueryableAsync(gridifyQuery, null);

            return(new Paging <SerieDTO> {
                Items = queryable.Query.ProjectTo <SerieDTO>(_mapper.ConfigurationProvider).ToList(),
                TotalItems = queryable.TotalItems
            });
        }
Esempio n. 13
0
    public void NestedFiltering_OnChildElement_ShouldNotThrowException()
    {
        // Arrange
        var mapping = new GridifyMapper <Root>()
                      .AddMap("Child2Name", x => x.Child2List.Select(c => c.Name))               // Works fine
                      .AddMap("Child2NameNested", x => x.Child1.Child2List.Select(c => c.Name)); // Throws exception

        // Act
        var exp = new GridifyQuery {
            Filter = "Child2NameNested=name"
        }.GetFilteringExpression(mapping);

        // Assert
        Assert.NotNull(exp);
    }
    [Fact] // issue #69
    public void ApplyOrdering_IsNotNull()
    {
        // Arrange
        var expected = _dbContext.Users.OrderByDescending(q => q.CreateDate.HasValue).ThenBy(w => w.CreateDate).ToQueryString();
        var gq       = new GridifyQuery()
        {
            OrderBy = "CreateDate? desc, CreateDate"
        };

        // Act
        var actual = _dbContext.Users.ApplyOrdering(gq).ToQueryString();

        // Assert
        Assert.Equal(expected, actual);
    }
    [Fact] // issue #69
    public void ApplyOrdering_IsNull()
    {
        // Arrange
        var expected = _dbContext.Users.OrderBy(q => !q.CreateDate.HasValue).ToQueryString();
        var gq       = new GridifyQuery()
        {
            OrderBy = "CreateDate!"
        };

        // Act
        var actual = _dbContext.Users.ApplyOrdering(gq).ToQueryString();

        // Assert
        Assert.Equal(expected, actual);
    }
Esempio n. 16
0
        public async Task <Paging <MovieDTO> > GetMoviesPagingAsync(int?genreId, GridifyQuery gridifyQuery)
        {
            var movies = _dbContext.Movies.AsNoTracking()
                         .Include(x => x.Genres);

            var queryable = await movies.GridifyQueryableAsync(gridifyQuery, null);

            if (genreId != null)
            {
                queryable = await movies.Where(x => x.Genres.Any(x => x.Id == genreId))
                            .GridifyQueryableAsync(gridifyQuery, null);
            }

            return(new Paging <MovieDTO> {
                Items = queryable.Query.ProjectTo <MovieDTO>(_mapper.ConfigurationProvider).ToList(),
                TotalItems = queryable.TotalItems
            });
        }
    public void EntityFrameworkServiceProviderCachingShouldNotThrowException()
    {
        // System.ArgumentException: An item with the same key has already been added. Key: Param_0

        // arrange
        var gq = new GridifyQuery {
            Filter = "name=n1|name=n2"
        };

        _ctx.Users.Gridify(gq);
        _ctx.Users.Gridify(gq);

        //act
        var exception = Record.Exception(() => _ctx.Users.GridifyQueryable(gq));

        // assert
        Assert.Null(exception);
    }
Esempio n. 18
0
    public void NestedFiltering_OnChildElement_ShouldReturnAValidExpression()
    {
        // Arrange
        var mapping = new GridifyMapper <Root>()
                      .AddMap("x1", x => x.Child1.Child2List.Select(c => c.Name));

        // ReSharper disable once ConditionIsAlwaysTrueOrFalse
        Expression <Func <Root, bool> > expectedExpression = x => (x.Child1.Child2List != null) && (x.Child1.Child2List.Any(c => c.Name == "name"));
        var expected = expectedExpression.ToString();

        // Act
        var actual = new GridifyQuery {
            Filter = "x1=name"
        }.GetFilteringExpression(mapping).ToString();

        // Assert
        // "x => ((x.Child1.Child2List != null) AndAlso x.Child1.Child2List.Any(c => (c.Name == \"name\")))";
        Assert.Equal(expected, actual);
    }
Esempio n. 19
0
        public void EntityFramework6_OrderBySupportAscending() // Issue #8
        {
            var connection = GetConnection();

            using (var context = new EntityContext(connection))
            {
                var gm = new GridifyQuery()
                {
                    OrderBy = "name"
                };
                var expected = context.Customers.ApplyOrdering(gm).ToList();
                var actual   = context.Customers
                               .OrderBy(q => q.Name)
                               .ToList();
                Assert.True(actual.Any());
                Assert.Equal(expected.First(), actual.First());
                Assert.Equal(expected, actual);
            }
        }
Esempio n. 20
0
        public async Task <IActionResult> GetCustomMappedList([FromQuery] GridifyQuery gridifyQuery)
        {
            // Case sensetive is false by default. But we can enable it here.
            var customMappings = new GridifyMapper <Person>(false)
                                 // Because properties with same name exists in both DTO and Entity classes, we can generate them.
                                 .GenerateMappings()
                                 // Add custom mappings
                                 .AddMap("livingAddress", q => q.Contact.Address)
                                 .AddMap("phone", q => q.Contact.PhoneNumber.ToString());

            // GridifyQueryable return a QueryablePaging<T>
            var result = await _context.People.GridifyQueryableAsync(gridifyQuery, customMappings);

            // We then apply AutoMapper to the query result and return a Paging.
            return(Ok(new Paging <PersonDto>()
            {
                Items = result.Query.ProjectTo <PersonDto>(_mapper.ConfigurationProvider).ToList(),
                TotalItems = result.TotalItems
            }));
        }
Esempio n. 21
0
        public void EntityFramework6_FilteringAndOrdering() // Issue #8
        {
            var connection = GetConnection();

            using (var context = new EntityContext(connection))
            {
                var gm = new GridifyQuery()
                {
                    OrderBy = "name desc", Filter = "id > 3"
                };
                var expected = context.Customers.ApplyFilteringAndOrdering(gm).ToList();
                var actual   = context.Customers
                               .Where(q => q.Id > 3)
                               .OrderByDescending(q => q.Name)
                               .ToList();
                Assert.True(actual.Any());
                Assert.Equal(expected.First(), actual.First());
                Assert.Equal(expected, actual);
            }
        }
Esempio n. 22
0
    private void UserReportTest1()
    {
        List <Level1> level1List = new List <Level1>();

        level1List.Add(new Level1());

        var gridifyMapper = new GridifyMapper <Level1>().GenerateMappings()
                            .AddMap("level4_property1", (l1, index) => l1.Level2List.Select(x => x.Level3.Level4List[index].Property1));

        var gridifyQuery = new GridifyQuery()
        {
            Filter = "level4_property1[0] > 5"
        };
        var query      = gridifyQuery.GetFilteringExpression(gridifyMapper);
        var expression = query.Compile();

        var actual = level1List.Where(expression).ToList();

        Assert.Single(actual);
        Assert.True(actual.Any());
    }
Esempio n. 23
0
 public async Task <ActionResult <Paging <UserDTO> > > GetUsers([FromQuery] GridifyQuery gridifyQuery) =>
 await _accountsService.GetUsersPagingAsync(gridifyQuery);
Esempio n. 24
0
 public async Task <ActionResult <Paging <MovieDTO> > > Get(int?genreId, [FromQuery] GridifyQuery gridifyQuery) =>
 await _moviesService.GetMoviesPagingAsync(genreId, gridifyQuery);
Esempio n. 25
0
 public async Task <ActionResult <Paging <MovieDTO> > > GetUpcomingReleases([FromQuery] GridifyQuery gridifyQuery) =>
 await _moviesService.GetUpcomingReleasesAsync(gridifyQuery);
Esempio n. 26
0
 public async Task <ActionResult <Paging <MovieDTO> > > GetInTheaters([FromQuery] GridifyQuery gridifyQuery) =>
 await _moviesService.GetInTheatersAsync(gridifyQuery);
Esempio n. 27
0
 public async Task <ActionResult <Paging <RequestDTO> > > GetRequests([FromQuery] GridifyQuery gridifyQuery) =>
 await _requestsService.GetAllRequestsPagingAsync(gridifyQuery);
Esempio n. 28
0
        public async Task <Paging <UserActivityDTO> > GetUserActivitiesPagingAsync(string userEmail, string ipAddress, GridifyQuery gridifyQuery)
        {
            QueryablePaging <UserActivity> queryable;

            if (string.IsNullOrWhiteSpace(userEmail))
            {
                queryable = await _dbContext.UserActivities.AsNoTracking().Where(x => x.IpAddress == ipAddress && x.UserName == null)
                            .GridifyQueryableAsync(gridifyQuery, null);
            }
            else
            {
                queryable = await _dbContext.UserActivities.AsNoTracking().Where(x => x.UserName == userEmail)
                            .GridifyQueryableAsync(gridifyQuery, null);
            }

            return(new Paging <UserActivityDTO> {
                Items = queryable.Query.ProjectTo <UserActivityDTO>(_mapper.ConfigurationProvider).ToList(),
                TotalItems = queryable.TotalItems
            });
        }
Esempio n. 29
0
 public async Task <ActionResult <Paging <CountryDTO> > > Get([FromQuery] GridifyQuery gridifyQuery) =>
 await _countriesService.GetCountriesListAsync(gridifyQuery);
Esempio n. 30
0
        public async Task <IActionResult> GetEverythingList([FromQuery] GridifyQuery gridifyQuery)
        {
            var result = _context.People.ApplyEverything(gridifyQuery);

            return(Ok(await result.ProjectTo <PersonDto>(_mapper.ConfigurationProvider).ToListAsync()));
        }