2021-08-05 06:22:19 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Castle.Core.Logging;
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Props.Models.Search;
|
|
|
|
using Props.Options;
|
|
|
|
using Props.Shop.Framework;
|
|
|
|
|
|
|
|
namespace Props.Services.Modules
|
|
|
|
{
|
|
|
|
public class LiveSearchManager : ISearchManager
|
|
|
|
{
|
|
|
|
private ILogger<LiveSearchManager> logger;
|
|
|
|
private SearchOptions searchOptions;
|
2021-08-07 22:20:46 +00:00
|
|
|
public IShopManager ShopManager { get; private set; }
|
2021-08-05 06:22:19 +00:00
|
|
|
private IMetricsManager metricsManager;
|
|
|
|
|
|
|
|
public LiveSearchManager(IMetricsManager metricsManager, IShopManager shopManager, IConfiguration configuration, ILogger<LiveSearchManager> logger)
|
|
|
|
{
|
|
|
|
this.logger = logger;
|
|
|
|
this.metricsManager = metricsManager;
|
2021-08-07 22:20:46 +00:00
|
|
|
this.ShopManager = shopManager;
|
2021-08-05 06:22:19 +00:00
|
|
|
this.searchOptions = configuration.GetSection(SearchOptions.Search).Get<SearchOptions>();
|
|
|
|
}
|
2021-08-07 22:20:46 +00:00
|
|
|
public async Task<IEnumerable<ProductListing>> Search(string query, SearchOutline searchOutline)
|
2021-08-05 06:22:19 +00:00
|
|
|
{
|
|
|
|
if (string.IsNullOrWhiteSpace(query)) throw new ArgumentException($"Query \"{query}\" is null or whitepsace.");
|
|
|
|
if (searchOutline == null) throw new ArgumentNullException("searchOutline");
|
|
|
|
List<ProductListing> results = new List<ProductListing>();
|
|
|
|
metricsManager.RegisterSearchQuery(query);
|
|
|
|
logger.LogDebug("Searching for \"{0}\".", query);
|
|
|
|
|
2021-08-12 04:54:52 +00:00
|
|
|
foreach (string shopName in await ShopManager.GetAllShopNames())
|
2021-08-05 06:22:19 +00:00
|
|
|
{
|
|
|
|
if (searchOutline.Enabled[shopName])
|
|
|
|
{
|
|
|
|
logger.LogDebug("Checking \"{0}\".", shopName);
|
|
|
|
int amount = 0;
|
2021-08-12 04:54:52 +00:00
|
|
|
await foreach (ProductListing product in (await ShopManager.GetShop(shopName)).Search(query, searchOutline.Filters))
|
2021-08-05 06:22:19 +00:00
|
|
|
{
|
|
|
|
if (searchOutline.Filters.Validate(product))
|
|
|
|
{
|
|
|
|
amount += 1;
|
|
|
|
metricsManager.RegisterListing(product, shopName);
|
|
|
|
results.Add(product);
|
|
|
|
}
|
|
|
|
if (amount >= searchOptions.MaxResults) break;
|
|
|
|
}
|
|
|
|
logger.LogDebug("Found {0} listings that satisfy the search filters from {1}.", amount, shopName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|