Skip to content

Commit

Permalink
feat: avoid external product fetching when product is already local
Browse files Browse the repository at this point in the history
  • Loading branch information
manuel-rw committed Jan 27, 2024
1 parent 397ce64 commit 0785ae6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
34 changes: 34 additions & 0 deletions GrocyScanner.Core/GrocyClient/GrocyClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,40 @@ public GrocyClient(IHttpClientFactory httpClientFactory, ILogger<GrocyClient> lo
return productBarcodes.SingleOrDefault(productBarcode => productBarcode.Barcode.Equals(gtin))?.ProductId;
}

public async Task<Product?> GetProductByBarcode(string gtin)
{
int? productId = await GetProductIdByBarcode(gtin);
if (productId == null)
{
return null;
}

using HttpClient httpClient = _httpClientFactory.CreateClient();
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Get, $"{_grocyConfiguration.Value.BaseUrl}/api/objects/products?query%5B%5D=id%3D{productId}");
httpRequestMessage.Headers.Add("GROCY-API-KEY", _grocyConfiguration.Value.ApiKey);
httpRequestMessage.Headers.Add("accept", "application/json");
HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage);
httpResponseMessage.EnsureSuccessStatusCode();
string content = await httpResponseMessage.Content.ReadAsStringAsync();
JsonDocument jsonDocument = JsonDocument.Parse(content);

if (jsonDocument.RootElement.GetArrayLength() != 1)
{
_logger.LogError("Unexpected array length from Grocy for product id {ProductId}: {Length}", productId, jsonDocument.RootElement.GetArrayLength());
return null;
}

JsonElement.ArrayEnumerator arrayEnumerator = jsonDocument.RootElement.EnumerateArray();
arrayEnumerator.MoveNext();

return new Product
{
Gtin = gtin,
Name = arrayEnumerator.Current.GetProperty("name").GetString()!,
ImageUrl = $"{_grocyConfiguration.Value.BaseUrl}/api/files/productpictures/{Base64Encode(arrayEnumerator.Current.GetProperty("picture_file_name").GetString()!)}"
};
}

public async Task<bool> UpsertProduct(Product product, int amount, DateOnly? bestBefore, double? price)
{
_logger.LogInformation("Adding product with date {Date} and Price {Price}", bestBefore, price);
Expand Down
2 changes: 2 additions & 0 deletions GrocyScanner.Core/GrocyClient/IGrocyClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public interface IGrocyClient
{
public Task<int?> GetProductIdByBarcode(string gtin);

public Task<Product?> GetProductByBarcode(string gtin);

public Task<bool> UpsertProduct(Product product, int amount, DateOnly? bestBefore, double? price);

public Task AddProductToStock(int productId, int amount, DateOnly? bestBefore, double? price);
Expand Down
17 changes: 15 additions & 2 deletions GrocyScanner.Service/Shared/Components/AddProductDialog.razor
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,21 @@
protected override async Task OnInitializedAsync()
{
IsLoading = true;
var products = await Task.WhenAll(ProductProviders.Select(async provider => await provider.GetProductByGtin(Barcode)));
Product = BestValueCalculator.GetProductWithMostValue(products.Where(product => product != null).Cast<Product>().ToList());

Product? productFromGrocy = await GrocyClient.GetProductByBarcode(Barcode);

// product is not known, must be searched in the providers
if (productFromGrocy == null)
{
var products = await Task.WhenAll(ProductProviders.Select(async provider => await provider.GetProductByGtin(Barcode)));
Product = BestValueCalculator.GetProductWithMostValue(products.Where(product => product != null).Cast<Product>().ToList());
}
else
{
// product is known and can be fetched from Grocy
Product = productFromGrocy;
}

IsLoading = false;
}

Expand Down

0 comments on commit 0785ae6

Please sign in to comment.