Added primitive search mechanism in backend.
Began implementing search mechanism for frontend.
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Props.Shop.Adafruit.Api;
|
||||
using Props.Shop.Framework;
|
||||
|
||||
namespace Props.Shop.Adafruit.Tests.Api
|
||||
{
|
||||
public class FakeProductListingManager : IProductListingManager
|
||||
{
|
||||
private ProductListingsParser parser;
|
||||
private Timer updateTimer;
|
||||
private bool disposedValue;
|
||||
|
||||
private volatile Dictionary<string, IList<ProductListing>> activeListings;
|
||||
|
||||
public IDictionary<string, IList<ProductListing>> ActiveListings => activeListings;
|
||||
|
||||
public event EventHandler DataUpdateEvent;
|
||||
|
||||
public FakeProductListingManager()
|
||||
{
|
||||
parser = new ProductListingsParser();
|
||||
}
|
||||
|
||||
public Task DownloadListings()
|
||||
{
|
||||
if (disposedValue) throw new ObjectDisposedException("FakeProductListingManager");
|
||||
using (Stream stream = File.OpenRead("./Assets/products.json"))
|
||||
{
|
||||
parser.BuildProductListings(stream);
|
||||
}
|
||||
|
||||
Dictionary<string, IList<ProductListing>> results = new Dictionary<string, IList<ProductListing>>();
|
||||
foreach (ProductListing product in parser.ProductListings)
|
||||
{
|
||||
IList<ProductListing> sameProducts = results.GetValueOrDefault(product.Name);
|
||||
if (sameProducts == null) {
|
||||
sameProducts = new List<ProductListing>();
|
||||
results.Add(product.Name, sameProducts);
|
||||
}
|
||||
|
||||
sameProducts.Add(product);
|
||||
}
|
||||
activeListings = results;
|
||||
DataUpdateEvent?.Invoke(this, null);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public void StartUpdateTimer(int delay = 300000, int period = 300000)
|
||||
{
|
||||
if (disposedValue) throw new ObjectDisposedException("FakeProductListingManager");
|
||||
if (updateTimer != null) throw new InvalidOperationException("Update timer already started.");
|
||||
updateTimer = new Timer((state) => DownloadListings(), null, delay, period);
|
||||
}
|
||||
|
||||
public void StopUpdateTimer()
|
||||
{
|
||||
if (disposedValue) throw new ObjectDisposedException("FakeProductListingManager");
|
||||
if (updateTimer == null) throw new InvalidOperationException("Update timer not started.");
|
||||
updateTimer.Dispose();
|
||||
updateTimer = null;
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!disposedValue)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
updateTimer?.Dispose();
|
||||
updateTimer = null;
|
||||
}
|
||||
|
||||
disposedValue = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(disposing: true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
}
|
@@ -10,7 +10,11 @@ namespace Props.Shop.Adafruit.Tests
|
||||
[Fact]
|
||||
public void TestParsing()
|
||||
{
|
||||
ListingsParser mockParser = new ListingsParser(File.ReadAllText("./Assets/products.json"));
|
||||
ProductListingsParser mockParser = new ProductListingsParser();
|
||||
using (Stream stream = File.OpenRead("./Assets/products.json"))
|
||||
{
|
||||
mockParser.BuildProductListings(stream);
|
||||
}
|
||||
Assert.NotEmpty(mockParser.ProductListings);
|
||||
}
|
||||
}
|
||||
|
@@ -1,25 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using Props.Shop.Adafruit.Api;
|
||||
using Props.Shop.Framework;
|
||||
using Xunit;
|
||||
|
||||
namespace Props.Shop.Adafruit.Tests.Api
|
||||
{
|
||||
public class ProductListingManagerTest
|
||||
{
|
||||
[Fact]
|
||||
public async Task TestSearch()
|
||||
{
|
||||
ProductListingManager mockProductListingManager = new ProductListingManager();
|
||||
mockProductListingManager.SetListingsData(File.ReadAllText("./Assets/products.json"));
|
||||
List<ProductListing> results = new List<ProductListing>();
|
||||
await foreach (ProductListing item in mockProductListingManager.Search("arduino", 0.5f))
|
||||
{
|
||||
results.Add(item);
|
||||
}
|
||||
Assert.NotEmpty(results);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,20 @@
|
||||
using System.Threading.Tasks;
|
||||
using Props.Shop.Adafruit.Api;
|
||||
using Xunit;
|
||||
|
||||
namespace Props.Shop.Adafruit.Tests.Api
|
||||
{
|
||||
public class SearchManagerTest
|
||||
{
|
||||
[Fact]
|
||||
public async Task SearchTest()
|
||||
{
|
||||
FakeProductListingManager stubProductListingManager = new FakeProductListingManager();
|
||||
SearchManager searchManager = new SearchManager(stubProductListingManager);
|
||||
await stubProductListingManager.DownloadListings();
|
||||
searchManager.Similarity = 0.8f;
|
||||
Assert.NotEmpty(searchManager.Search("Raspberry Pi"));
|
||||
searchManager.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user