Implemented groundwork for search configuration.
This commit is contained in:
@@ -15,7 +15,7 @@ namespace Props.Shop.Adafruit.Api
|
||||
private Dictionary<string, List<ProductListing>> listings = new Dictionary<string, List<ProductListing>>();
|
||||
private bool requested = false;
|
||||
public DateTime TimeOfLastRequest { get; private set; }
|
||||
public bool RequestReady => DateTime.Now - TimeOfLastRequest > TimeSpan.FromMinutes(minutesPerRequest);
|
||||
public bool RequestReady => !requested || DateTime.Now - TimeOfLastRequest > TimeSpan.FromMinutes(minutesPerRequest);
|
||||
|
||||
public ProductListingManager(int requestsPerMinute = 5)
|
||||
{
|
||||
@@ -27,10 +27,10 @@ namespace Props.Shop.Adafruit.Api
|
||||
requested = true;
|
||||
TimeOfLastRequest = DateTime.Now;
|
||||
HttpResponseMessage response = await http.GetAsync("/products");
|
||||
SetListings(await response.Content.ReadAsStringAsync());
|
||||
SetListingsData(await response.Content.ReadAsStringAsync());
|
||||
}
|
||||
|
||||
public void SetListings(string data)
|
||||
public void SetListingsData(string data)
|
||||
{
|
||||
ListingsParser listingsParser = new ListingsParser(data);
|
||||
foreach (ProductListing listing in listingsParser.ProductListings)
|
||||
|
@@ -25,7 +25,7 @@ namespace Props.Shop.Framework
|
||||
public int MinPurchases { get; set; }
|
||||
public bool KeepUnknownPurchaseCount { get; set; } = true;
|
||||
public int MinReviews { get; set; }
|
||||
public bool KeepUnknownRatingCount { get; set; } = true;
|
||||
public bool KeepUnknownReviewCount { get; set; } = true;
|
||||
public bool EnableMaxShippingFee { get; set; }
|
||||
private int maxShippingFee;
|
||||
|
||||
@@ -59,7 +59,7 @@ namespace Props.Shop.Framework
|
||||
MinPurchases == other.MinPurchases &&
|
||||
KeepUnknownPurchaseCount == other.KeepUnknownPurchaseCount &&
|
||||
MinReviews == other.MinReviews &&
|
||||
KeepUnknownRatingCount == other.KeepUnknownRatingCount &&
|
||||
KeepUnknownReviewCount == other.KeepUnknownReviewCount &&
|
||||
EnableMaxShippingFee == other.EnableMaxShippingFee &&
|
||||
MaxShippingFee == other.MaxShippingFee &&
|
||||
KeepUnknownShipping == other.KeepUnknownShipping;
|
||||
@@ -81,5 +81,16 @@ namespace Props.Shop.Framework
|
||||
{
|
||||
return (Filters)this.MemberwiseClone();
|
||||
}
|
||||
|
||||
public bool Validate(ProductListing listing)
|
||||
{
|
||||
if (listing.Shipping == null && !KeepUnknownShipping || (EnableMaxShippingFee && listing.Shipping > MaxShippingFee)) return false;
|
||||
float shippingDifference = listing.Shipping != null ? listing.Shipping.Value : 0;
|
||||
if (!(listing.LowerPrice + shippingDifference >= LowerPrice && (!EnableUpperPrice || listing.UpperPrice + shippingDifference <= UpperPrice))) return false;
|
||||
if ((listing.Rating == null && !KeepUnrated) && MinRating > (listing.Rating == null ? 0 : listing.Rating)) return false;
|
||||
if ((listing.PurchaseCount == null && !KeepUnknownPurchaseCount) || MinPurchases > (listing.PurchaseCount == null ? 0 : listing.PurchaseCount)) return false;
|
||||
if ((listing.ReviewCount == null && !KeepUnknownReviewCount) || MinReviews > (listing.ReviewCount == null ? 0 : listing.ReviewCount)) return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@@ -13,7 +13,7 @@ namespace Props.Shop.Adafruit.Tests.Api
|
||||
public async Task TestSearch()
|
||||
{
|
||||
ProductListingManager mockProductListingManager = new ProductListingManager();
|
||||
mockProductListingManager.SetListings(File.ReadAllText("./Assets/products.json"));
|
||||
mockProductListingManager.SetListingsData(File.ReadAllText("./Assets/products.json"));
|
||||
List<ProductListing> results = new List<ProductListing>();
|
||||
await foreach (ProductListing item in mockProductListingManager.Search("arduino", 0.5f))
|
||||
{
|
||||
|
Reference in New Issue
Block a user