public IActionResult Filter([FromBody] UsersFilteringModel model) { var query = _filteringServices.Filter(_services.Get(), model); var list = query.ToList(); return(new JsonResult(list)); }
public void InvalidFilteringModel(bool descending, UsersSortingCriterion criterion) { var model = new UsersFilteringModel("", criterion, descending, 1, 1); var query = Context.Users.AsQueryable(); Assert.Throws <ArgumentOutOfRangeException>(() => FilteringServices.Filter(query, model)); }
public void ReturnsProperlyFilteredCategories(string searchText, UsersSortingCriterion criterion, bool descending, int pageIndex, int pageSize) { var model = new UsersFilteringModel(searchText, criterion, descending, pageIndex, pageSize); var users = AddUsers(31); var categories = AddCategories(); var flashards = AddFlashcards(categories[0], 50); var userProgress = AddUserProgress(flashards, users.GetRange(0, 20)); var query = Context.Users.AsQueryable(); var filteredUserDtos = FilteringServices.Filter(query, model).ToList(); var expected = users.Where(u => u.Name.Contains(model.SearchText) || u.Surname.Contains(model.SearchText) || u.UserName.Contains(model.SearchText)); var lambda = GetLambda(criterion); expected = descending ? expected.OrderByDescending(lambda) : expected.OrderBy(lambda); var expectedList = expected.Skip(model.PageIndex * model.PageSize).Take(model.PageSize) .Select(u => new UserDto(u.Name, u.Surname, u.UserName, new Score(userProgress.Where(up => up.UserId == u.Id).Sum(progress => progress.Progress)))) .ToList(); Assert.Equal(expectedList.Count, filteredUserDtos.Count); for (var i = 0; i < expectedList.Count; i++) { Assert.True(expectedList[i].UserName.Equals(filteredUserDtos[i].UserName)); } }
public IQueryable <UserDto> Filter(IQueryable <User> q, UsersFilteringModel model) { var query = q.Include(u => u.UserProgress).AsQueryable(); query = query.Where(u => u.Name.Contains(model.SearchText) || u.Surname.Contains(model.SearchText) || u.UserName.Contains(model.SearchText)); if (model.Descending) { switch (model.SortingCriterion) { case UsersSortingCriterion.None: query = query.OrderByDescending(u => u.UserName); break; case UsersSortingCriterion.Name: query = query.OrderByDescending(u => u.Name); break; case UsersSortingCriterion.Surname: query = query.OrderByDescending(u => u.Surname); break; case UsersSortingCriterion.UserName: query = query.OrderByDescending(u => u.UserName); break; case UsersSortingCriterion.Points: query = query.OrderByDescending(u => u.UserProgress.Sum(up => up.Progress)); break; default: throw new ArgumentOutOfRangeException(); } } else { switch (model.SortingCriterion) { case UsersSortingCriterion.None: query = query.OrderBy(u => u.UserName); break; case UsersSortingCriterion.Name: query = query.OrderBy(u => u.Name); break; case UsersSortingCriterion.Surname: query = query.OrderBy(u => u.Surname); break; case UsersSortingCriterion.UserName: query = query.OrderBy(u => u.UserName); break; case UsersSortingCriterion.Points: query = query.OrderBy(u => u.UserProgress.Sum(up => up.Progress)); break; default: throw new ArgumentOutOfRangeException(); } } return(query.Skip(model.PageIndex * model.PageSize).Take(model.PageSize) .Select(u => new UserDto(u.Name, u.Surname, u.UserName, new Score(u.UserProgress.Sum(up => up.Progress))))); }