-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathArticlesDbSearchEngine.cs
More file actions
58 lines (51 loc) · 1.72 KB
/
ArticlesDbSearchEngine.cs
File metadata and controls
58 lines (51 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using AutoMapper;
using CoreWiki.Application.Articles.Search.Dto;
using CoreWiki.Core.Domain;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CoreWiki.Application.Articles.Search.Impl
{
public class ArticlesDbSearchEngine : IArticlesSearchEngine
{
private readonly ISearchProvider<Article> _searchProvider;
private readonly IMapper _mapper;
public ArticlesDbSearchEngine(ISearchProvider<Article> searchProvider, IMapper mapper)
{
_searchProvider = searchProvider;
_mapper = mapper;
}
public async Task<SearchResultDto<ArticleSearchDto>> SearchAsync(string query, int pageNumber, int resultsPerPage)
{
var filteredQuery = query.Trim();
var (articles, totalFound) = await _searchProvider.SearchAsync(filteredQuery, pageNumber, resultsPerPage).ConfigureAwait(false);
// TODO maybe make this searchproviders problem
var total = int.TryParse(totalFound.ToString(), out var inttotal);
if (!total)
{
inttotal = int.MaxValue;
}
return _mapper.CreateArticleResultDTO(filteredQuery, articles, pageNumber, resultsPerPage, inttotal);
}
}
internal static class SearchResultFactory
{
internal static SearchResultDto<ArticleSearchDto> CreateArticleResultDTO(this IMapper mapper, string query, IEnumerable<Article> articles, int currenPage, int resultsPerPage, int totalResults)
{
var results = new List<Article>();
if (articles?.Any() == true)
{
results = articles.ToList();
}
var result = new SearchResult<Article>
{
Query = query,
Results = results,
CurrentPage = currenPage,
ResultsPerPage = resultsPerPage,
TotalResults = totalResults
};
return mapper.Map<SearchResultDto<ArticleSearchDto>>(result);
}
}
}