-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathLocalDbArticleSearchProviderAdapter.cs
More file actions
48 lines (42 loc) · 1.61 KB
/
LocalDbArticleSearchProviderAdapter.cs
File metadata and controls
48 lines (42 loc) · 1.61 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CoreWiki.Core.Domain;
using CoreWiki.Data.Abstractions.Interfaces;
using Microsoft.Extensions.Logging;
namespace CoreWiki.Application.Articles.Search.Impl
{
/// <summary>
/// Adapter pattern: When using local DB, convert Concrete Articlesearch to Generic ISearchProvider<T>
/// </summary>
/// <typeparam name="T"></typeparam>
public class LocalDbArticleSearchProviderAdapter<T> : ISearchProvider<T> where T : Article
{
private readonly ILogger _logger;
private readonly IArticleRepository _articleRepo;
public LocalDbArticleSearchProviderAdapter(ILogger<LocalDbArticleSearchProviderAdapter<T>> logger, Func<int, IArticleRepository> articleRepo)
{
_logger = logger;
_articleRepo = articleRepo(1);
}
public Task<int> IndexElementsAsync(params T[] items)
{
// For LocalDB DB itself is responsible for "Indexing"
return Task.Run(() => items.Length);
}
public async Task<(IEnumerable<T> results, long total)> SearchAsync(string Query, int pageNumber, int resultsPerPage)
{
var offset = (pageNumber - 1) * resultsPerPage;
var (articles, totalFound) = _articleRepo.GetArticlesForSearchQuery(Query, offset, resultsPerPage);
var supportedType = articles.GetType().GetGenericArguments()[0];
if (typeof(T) == supportedType)
{
var tlist = articles.Cast<T>();
return (results: tlist, total: totalFound);
}
_logger.LogWarning($"{nameof(SearchAsync)}: Only supports search for {nameof(supportedType)} but asked for {typeof(T).FullName}");
return (Enumerable.Empty<T>(), 0);
}
}
}