Update product service and detail view with gallery support
This commit is contained in:
@@ -11,18 +11,20 @@ public record Product(
|
||||
long Price,
|
||||
int Discount = 0,
|
||||
int Rate = 0,
|
||||
int RemainingCount = 0);
|
||||
int RemainingCount = 0)
|
||||
{
|
||||
public IReadOnlyList<ProductGalleryImage> Gallery { get; init; } = Array.Empty<ProductGalleryImage>();
|
||||
}
|
||||
|
||||
public record ProductGalleryImage(
|
||||
long ProductGalleryId,
|
||||
long ProductImageId,
|
||||
string Title,
|
||||
string ImageUrl,
|
||||
string ThumbnailUrl);
|
||||
|
||||
public class ProductService
|
||||
{
|
||||
private static readonly List<Product> _seed = new()
|
||||
{
|
||||
new(1, "ماسک پزشکی سهلایه", "ماسک سهلایه با فیلتراسیون بالا مناسب برای محیطهای عمومی.", "/images/store/mask.jpg", 49000),
|
||||
new(2, "دستکش لاتکس", "دستکش لاتکس مناسب معاینات پزشکی، بدون پودر.", "/images/store/gloves.jpg", 89000),
|
||||
new(3, "محلول ضدعفونی کننده", "محلول ضدعفونی بر پایه الکل ۷۰٪ مناسب دست و سطوح.", "/images/store/sanitizer.jpg", 69000),
|
||||
new(4, "تبسنج دیجیتال", "تبسنج دیجیتال با دقت بالا و نمایشگر LCD.", "/images/store/thermometer.jpg", 299000),
|
||||
new(5, "فشارسنج دیجیتال", "فشارسنج بازویی دیجیتال با حافظه داخلی.", "/images/store/bp-monitor.jpg", 1259000)
|
||||
};
|
||||
|
||||
private readonly ConcurrentDictionary<long, Product> _cache = new();
|
||||
private readonly ProductsContract.ProductsContractClient _client;
|
||||
@@ -30,7 +32,7 @@ public class ProductService
|
||||
public ProductService(ProductsContract.ProductsContractClient client)
|
||||
{
|
||||
_client = client;
|
||||
foreach (var p in _seed) _cache[p.Id] = p;
|
||||
|
||||
}
|
||||
|
||||
public async Task<List<Product>> GetProductsAsync(string? query = null)
|
||||
@@ -67,10 +69,13 @@ public class ProductService
|
||||
|
||||
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);
|
||||
var resp = await _client.GetProductsAsync(new GetProductsRequest { Id = id });
|
||||
if (resp == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return MapAndCache(resp);
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -99,4 +104,34 @@ public class ProductService
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private Product MapAndCache(GetProductsResponse model)
|
||||
{
|
||||
var gallery = model.Gallery
|
||||
.Select(item => new ProductGalleryImage(
|
||||
ProductGalleryId: item.ProductGalleryId,
|
||||
ProductImageId: item.ProductImageId,
|
||||
Title: item.Title ?? string.Empty,
|
||||
ImageUrl: BuildUrl(item.ImagePath),
|
||||
ThumbnailUrl: BuildUrl(item.ImageThumbnailPath)))
|
||||
.ToList();
|
||||
|
||||
var product = new Product(
|
||||
Id: model.Id,
|
||||
Title: model.Title ?? string.Empty,
|
||||
Description: model.Description ?? string.Empty,
|
||||
ImageUrl: BuildUrl(model.ImagePath),
|
||||
Price: model.Price,
|
||||
Discount: model.Discount,
|
||||
Rate: model.Rate,
|
||||
RemainingCount: model.RemainingCount)
|
||||
{
|
||||
Gallery = gallery
|
||||
};
|
||||
|
||||
_cache[product.Id] = product;
|
||||
return product;
|
||||
}
|
||||
|
||||
private static string BuildUrl(string? path) => string.IsNullOrWhiteSpace(path) ? string.Empty : UrlUtility.DownloadUrl + path;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user