Esempio n. 1
0
        public EventListViewModel Search(EventSearchSettings searchSettings)
        {
            Throw.IfNull(searchSettings, nameof(searchSettings));

            _logger.Debug(
                $"{nameof(Search)} has been called with name {searchSettings.Name}," +
                $" difficulty {searchSettings.Difficulty}, starDate {searchSettings.StartDate}," +
                $" orderBy {searchSettings.OrderBy}, page number {searchSettings.PageNumber}," +
                $" page size {searchSettings.PageSize}.");

            _eventPipeline
            .Register(new NameFilter())
            .Register(new DifficultyFilter())
            .Register(new DateFilter())
            .Register(new TagFilter())
            .Register(new SortByFilter());

            var filteredEvents          = _eventsRepository.SearchEvents(_eventPipeline, searchSettings);
            var filteredEventsViewModel = filteredEvents.ToViewModel();

            return(new EventListViewModel
            {
                FilteredItems = filteredEventsViewModel,
                TotalItems = _eventPipeline.TotalItems,
                PageNumber = searchSettings.PageNumber,
                PageSize = searchSettings.PageSize
            });
        }
        public IEnumerable <IEventDetailsPage> SearchEvents(
            IEventPipeline pipeline,
            EventSearchSettings searchSettings)
        {
            Throw.IfNull(searchSettings, nameof(searchSettings));
            Throw.IfNull(pipeline, nameof(pipeline));

            using (var ctx = ContentSearchManager.GetIndex(IndexName).CreateSearchContext())
            {
                var queryable     = ctx.GetQueryable <EventDetailsSearchItem>();
                var searchResults = pipeline.Process(searchSettings, queryable).GetResults();

                if (searchResults != null && searchResults.TotalSearchResults > 0)
                {
                    var data = searchResults.Hits
                               .Where(i => i.Document != null)
                               .Select(i => i.Document);

                    var listResult = new List <IEventDetailsPage>();

                    foreach (var searchItem in data)
                    {
                        listResult.Add(_sitecoreService.GetItem <IEventDetailsPage>(searchItem.GetItem()));
                    }

                    return(listResult);
                }
            }

            return(Enumerable.Empty <IEventDetailsPage>());
        }
        public void DifficultyFilter_WhenItemsExist_ExpectNotEmptyList()
        {
            var searchSettings = new EventSearchSettings {
                Difficulty = 5, PageSize = 3
            };
            var pipeline = new EventPipeline().Register(new DifficultyFilter());

            var result = pipeline.Process(searchSettings, CreateItems());

            Assert.IsTrue(result.All(x => x.Difficulty == searchSettings.Difficulty));
        }
        public void DateFilter_WhenItemsExist_ExpectNotEmptyList()
        {
            var searchSettings = new EventSearchSettings {
                StartDate = DateTime.UtcNow.AddDays(4), PageSize = 3
            };
            var pipeline = new EventPipeline().Register(new DateFilter());

            var result = pipeline.Process(searchSettings, CreateItems());

            Assert.IsTrue(result.All(x => x.StartDate.Date >= searchSettings.StartDate.Date));
        }
        public void NameFilter_WhenItemsExist_ExpectNotEmptyList()
        {
            var searchSettings = new EventSearchSettings {
                Name = "Walking", PageSize = 3
            };
            var pipeline = new EventPipeline().Register(new NameFilter());

            var result = pipeline.Process(searchSettings, CreateItems());

            Assert.IsTrue(result.All(x => x.Name == searchSettings.Name));
        }
        public JsonResult Search(EventSearchSettings searchSettings)
        {
            Throw.IfNull(searchSettings, nameof(searchSettings));

            _logger.Debug(
                $"{nameof(Search)} has been called with name {searchSettings.Name}," +
                $" difficulty {searchSettings.Difficulty}, starDate {searchSettings.StartDate}," +
                $" orderBy {searchSettings.OrderBy}, page number {searchSettings.PageNumber}," +
                $" page size {searchSettings.PageSize}.");

            return(Json(_eventSearchService.Search(searchSettings)));
        }
        public void SortByFilter_WhenOrderByIsDate_ExpectSortedListByName()
        {
            var searchSettings = new EventSearchSettings {
                OrderBy = "date", PageSize = 3
            };
            var pipeline       = new EventPipeline().Register(new SortByFilter());
            var expectedResult = CreateItems().OrderBy(i => i.StartDate);

            var result = pipeline.Process(searchSettings, CreateItems());

            CollectionAssert.AreEqual(
                expectedResult.Select(x => x.StartDate.Date).ToList(),
                expectedResult.Select(x => x.StartDate.Date).ToList());
        }
        public void Search_WhenItemsNotExist_ExpectEmptyList()
        {
            var searchSettings = new EventSearchSettings {
                Name = "Some name", Difficulty = 3, StartDate = DateTime.UtcNow.AddDays(6)
            };
            var pipeline = new EventPipeline()
                           .Register(new NameFilter())
                           .Register(new DateFilter())
                           .Register(new DifficultyFilter());

            var result = pipeline.Process(searchSettings, CreateItems());

            Assert.AreEqual(0, result.Count());
        }
        public void Pagination_ExpectCorrectItemPage(int pageNumber)
        {
            var searchSettingsPage = new EventSearchSettings {
                PageNumber = pageNumber, PageSize = 2
            };
            var expectedResult = new Dictionary <int, int> {
                { 1, 2 }, { 2, 1 }
            };
            var pipeline = new EventPipeline().Register(new SortByFilter());
            var allItems = CreateItems();

            var resultPage = pipeline.Process(searchSettingsPage, allItems);

            Assert.AreEqual(expectedResult[pageNumber], resultPage.Count());
        }