Example #1
0
        public async Task SearchItemTemplateAsync_ShouldTryToFindItemsInDatabaseWithMoreAccurateSearchFirstThenWithFuzzyFilter()
        {
            const string filter      = "some-filter";
            const string cleanFilter = "SOME-CLEAN-FILTER";
            const string cleanFilterWithoutSeparator = "SOME-CLEAN-FILTER-WITH-NO-SEPARATOR";
            var          itemTemplateId1             = Guid.NewGuid();
            var          itemTemplateId2             = Guid.NewGuid();
            var          itemTemplateId3             = Guid.NewGuid();

            var item1 = new ItemTemplate {
                Id = itemTemplateId1
            };
            var item2 = new ItemTemplate {
                Id = itemTemplateId2
            };
            var item3 = new ItemTemplate {
                Id = itemTemplateId3
            };

            _stringCleanupUtil.RemoveAccents(filter)
            .Returns(cleanFilter);
            _stringCleanupUtil.RemoveSeparators(cleanFilter)
            .Returns(cleanFilterWithoutSeparator);
            _unitOfWorkFactory.GetUnitOfWork().ItemTemplates.GetItemByCleanNameWithAllDataAsync(cleanFilter, Arg.Any <int>(), Arg.Any <int?>(), Arg.Any <bool>())
            .Returns(new List <ItemTemplate> {
                item1
            });
            _unitOfWorkFactory.GetUnitOfWork().ItemTemplates.GetItemByPartialCleanNameWithAllDataAsync(cleanFilter, Arg.Any <int>(), Arg.Any <IEnumerable <Guid> >(), Arg.Any <int?>(), Arg.Any <bool>())
            .Returns(new List <ItemTemplate> {
                item2
            });
            _unitOfWorkFactory.GetUnitOfWork().ItemTemplates.GetItemByPartialCleanNameWithoutSeparatorWithAllDataAsync(cleanFilterWithoutSeparator, Arg.Any <int>(), Arg.Any <IEnumerable <Guid> >(), Arg.Any <int?>(), Arg.Any <bool>())
            .Returns(new List <ItemTemplate> {
                item3
            });

            var actualItemTemplates = await _service.SearchItemTemplateAsync(filter, 40, null);

            actualItemTemplates.Should().BeEquivalentTo(item1, item2, item3);
        }
Example #2
0
        public async Task <List <ItemTemplate> > SearchItemTemplateAsync(string filter, int maxResultCount, int?currentUserId)
        {
            var matchingItemTemplates = new List <ItemTemplate>();

            using (var uow = _unitOfWorkFactory.CreateUnitOfWork())
            {
                var cleanFilter = _stringCleanupUtil.RemoveAccents(filter).ToUpperInvariant();

                var exactMatchingItems = await uow.ItemTemplates.GetItemByCleanNameWithAllDataAsync(cleanFilter, maxResultCount, currentUserId, true);

                matchingItemTemplates.AddRange(exactMatchingItems);

                var partialMatchingItems = await uow.ItemTemplates.GetItemByPartialCleanNameWithAllDataAsync(cleanFilter, maxResultCount - matchingItemTemplates.Count, matchingItemTemplates.Select(i => i.Id), currentUserId, true);

                matchingItemTemplates.AddRange(partialMatchingItems);

                var noSeparatorFilter = _stringCleanupUtil.RemoveSeparators(cleanFilter);
                var partialMatchingIgnoreSpacesItems = await uow.ItemTemplates.GetItemByPartialCleanNameWithoutSeparatorWithAllDataAsync(noSeparatorFilter, maxResultCount - matchingItemTemplates.Count, matchingItemTemplates.Select(i => i.Id), currentUserId, true);

                matchingItemTemplates.AddRange(partialMatchingIgnoreSpacesItems);
            }

            return(matchingItemTemplates);
        }