コード例 #1
0
        public ActionResult <IEnumerable <MunroModel> > Get([FromQuery] MunroQuery sortQuery)
        {
            if ((sortQuery?.MinHeightInMetres ?? 0) > (sortQuery?.MaxHeightInMetres ?? double.MaxValue))
            {
                return(BadRequest("Provided maximum height should be greater than the provided minimum height or zero."));
            }

            return(Ok(munroService.GetMunros(sortQuery)));
        }
コード例 #2
0
        public IEnumerable <MunroModel> GetMunros(MunroQuery sortQuery)
        {
            var allMunros = dataContext.Munros
                            .Where(m => !string.IsNullOrEmpty(m.HillCategoryPost1997))
                            .Select(mapper.Map <MunroModel>).ToList();

            var filteredMunros = allMunros.Where(m => m.HeightInMetres >= (sortQuery.MinHeightInMetres ?? 0) &&
                                                 m.HeightInMetres <= (sortQuery.MaxHeightInMetres ?? double.MaxValue) &&
                                                 (sortQuery.Category == HillCategory.Either || GetHillCategoryStringAsEnum(m.HillCategory) == sortQuery.Category)).ToList();

            if (sortQuery.SortingCriterias.Any())
            {
                var distinctSortingCriterias = sortQuery.SortingCriterias
                                               .GroupBy(c => c.PropertyName)
                                               .Select(g => g.OrderBy(c => c.Priority).First()).OrderBy(c => c.Priority).ToList();

                var sortedMunros = filteredMunros.AsQueryable().OrderBySortingCriterias(distinctSortingCriterias);

                filteredMunros = sortedMunros.ToList();
            }

            return(sortQuery.Limit != null?filteredMunros.Take(sortQuery.Limit.Value) : filteredMunros);
        }
コード例 #3
0
        public void GetMunros_WhenCalledSortByWithEmptyStringBetweenCriteria_ReturnsMunrosInCorrectOrder()
        {
            var fakeMunros = FakeMunroDataContext.GenerateFakeMunros(20, 15, 10, 500, 2000);

            var orderedFakeMunros = fakeMunros.Where(m => !string.IsNullOrEmpty(m.HillCategoryPost1997))
                                    .OrderByDescending(m => m.Name).ToList();

            var fakeDataContext = new FakeMunroDataContext(fakeMunros);

            var service = new MunroService(fakeDataContext, testMapper);

            var query = new MunroQuery
            {
                SortBy = "desc(name),,asc(name)"
            };

            var result = service.GetMunros(query).ToList();

            Assert.Equal(2, query.SortingCriterias.Count);

            Assert.Equal(orderedFakeMunros.Count, result.Count);

            Assert.Equal(orderedFakeMunros.Select(m => m.Name), result.Select(m => m.Name));
        }