feat: enhance product detail loading and initialization logic
Build and Deploy to Kubernetes / build-and-deploy (push) Successful in 6m50s
Build and Deploy to Kubernetes / build-and-deploy (push) Successful in 6m50s
- Introduced an `_initialized` flag to manage product loading state more effectively. - Updated `OnParametersSetAsync` to conditionally load product details based on the initialization state and valid product ID. - Refactored product retrieval logic in `ProductService` to handle invalid IDs and improve fallback mechanisms for fetching product details. These changes improve the reliability and performance of the product detail component, ensuring that product information is loaded correctly and efficiently.
This commit is contained in:
@@ -137,30 +137,66 @@ public class ProductService
|
||||
|
||||
public async Task<Product?> GetByIdAsync(long id)
|
||||
{
|
||||
if (TryGetCachedProduct(id, out var cached) && HasDetailedData(cached))
|
||||
{
|
||||
return cached;
|
||||
}
|
||||
if (id <= 0)
|
||||
return null;
|
||||
|
||||
TryGetCachedProduct(id, out var cached);
|
||||
|
||||
if (cached is not null && HasDetailedData(cached))
|
||||
return cached;
|
||||
|
||||
// جزئیات کامل (گالری + دستهبندی)
|
||||
var detailed = await TryFetchDetailAsync(id);
|
||||
if (detailed is not null)
|
||||
return detailed;
|
||||
|
||||
// fallback: همان API لیست محصولات — ناموجودها را هم برمیگرداند
|
||||
var fromFilter = await TryFetchByFilterIdAsync(id);
|
||||
if (fromFilter is not null)
|
||||
return fromFilter;
|
||||
|
||||
return cached;
|
||||
}
|
||||
|
||||
private async Task<Product?> TryFetchDetailAsync(long id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var resp = await _client.GetProductsAsync(new GetProductsRequest { Id = id });
|
||||
if (resp == null)
|
||||
{
|
||||
if (resp is null || resp.Id <= 0)
|
||||
return null;
|
||||
}
|
||||
|
||||
return MapAndCache(resp);
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (cached is not null)
|
||||
{
|
||||
return cached;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
TryGetCachedProduct(id, out var result);
|
||||
return result;
|
||||
private async Task<Product?> TryFetchByFilterIdAsync(long id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var resp = await _client.GetAllProductsByFilterAsync(new GetAllProductsByFilterRequest
|
||||
{
|
||||
PaginationState = new CMSMicroservice.Protobuf.Protos.PaginationState
|
||||
{
|
||||
PageNumber = 1,
|
||||
PageSize = 1
|
||||
},
|
||||
Filter = new GetAllProductsByFilterFilter { Id = id }
|
||||
});
|
||||
|
||||
var model = resp.Models.FirstOrDefault(m => m.Id == id);
|
||||
if (model is null)
|
||||
return null;
|
||||
|
||||
return MapAndCache(resp.Models).FirstOrDefault(p => p.Id == id);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user