Add Products gRPC client and update ProductService for fetching products

This commit is contained in:
masoodafar-web
2025-11-17 23:58:50 +03:30
parent 52b8298a18
commit ddb47fda08
4 changed files with 83 additions and 20 deletions

View File

@@ -1,4 +1,5 @@
using System.Collections.Concurrent;
using FrontOffice.BFF.Products.Protobuf.Protos.Products;
namespace FrontOffice.Main.Utilities;
@@ -15,30 +16,76 @@ public class ProductService
new(5, "فشارسنج دیجیتال", "فشارسنج بازویی دیجیتال با حافظه داخلی.", "/images/store/bp-monitor.jpg", 1259000)
};
private readonly ConcurrentDictionary<long, Product> _products = new();
private readonly ConcurrentDictionary<long, Product> _cache = new();
private readonly ProductsContract.ProductsContractClient _client;
public ProductService()
public ProductService(ProductsContract.ProductsContractClient client)
{
foreach (var p in _seed) _products[p.Id] = p;
_client = client;
foreach (var p in _seed) _cache[p.Id] = p;
}
public Task<List<Product>> GetProductsAsync(string? query = null)
public async Task<List<Product>> GetProductsAsync(string? query = null)
{
IEnumerable<Product> list = _products.Values.OrderBy(p => p.Id);
if (!string.IsNullOrWhiteSpace(query))
try
{
query = query.Trim();
list = list.Where(p =>
p.Title.Contains(query, StringComparison.OrdinalIgnoreCase) ||
p.Description.Contains(query, StringComparison.OrdinalIgnoreCase));
var resp = await _client.GetAllProductsByFilterAsync(new GetAllProductsByFilterRequest
{
Filter = new GetAllProductsByFilterFilter
{
Title = query ?? string.Empty,
Description = query ?? string.Empty,
ShortInfomation = query ?? string.Empty,
FullInformation = query ?? string.Empty
}
});
return MapAndCache(resp.Models);
}
catch
{
IEnumerable<Product> list = _cache.Values;
if (!string.IsNullOrWhiteSpace(query))
{
var q = query.Trim();
list = list.Where(p => p.Title.Contains(q, StringComparison.OrdinalIgnoreCase) || p.Description.Contains(q, StringComparison.OrdinalIgnoreCase));
}
return list.OrderBy(p => p.Id).ToList();
}
return Task.FromResult(list.ToList());
}
public Task<Product?> GetByIdAsync(long id)
public async Task<Product?> GetByIdAsync(long id)
{
_products.TryGetValue(id, out var result);
return Task.FromResult(result);
if (_cache.TryGetValue(id, out var cached)) return cached;
try
{
// No single-get endpoint exposed: fetch list and pick
var resp = await _client.GetAllProductsByFilterAsync(new GetAllProductsByFilterRequest { Filter = new GetAllProductsByFilterFilter() });
var list = MapAndCache(resp.Models);
return list.FirstOrDefault(x => x.Id == id);
}
catch
{
_cache.TryGetValue(id, out var result);
return result;
}
}
private List<Product> MapAndCache(Google.Protobuf.Collections.RepeatedField<GetAllProductsByFilterResponseModel> models)
{
var list = new List<Product>();
foreach (var m in models)
{
var p = new Product(
Id: m.Id,
Title: m.Title ?? string.Empty,
Description: m.Description ?? string.Empty,
ImageUrl: string.IsNullOrWhiteSpace(m.ImagePath) ? string.Empty : UrlUtility.DownloadUrl + m.ImagePath,
Price: m.Price
);
_cache[p.Id] = p;
list.Add(p);
}
return list;
}
}