Files
midrandbooks/MidrandBookshop/Components/Pages/ProductView.razor.cs
T
Khwezi Mngoma 7a1a7566d6
continuous-integration/drone/pr Build is passing
Stable running home page with product view
2026-05-30 19:06:14 +02:00

104 lines
4.1 KiB
C#

using LiteCharms.Features.MidrandBooks;
using LiteCharms.Features.MidrandBooks.Authors;
using LiteCharms.Features.MidrandBooks.Authors.Models;
using LiteCharms.Features.MidrandBooks.Products;
using LiteCharms.Features.MidrandBooks.Products.Models;
namespace MidrandBookshop.Components.Pages;
public partial class ProductView : ComponentBase
{
[Parameter] public long BookId { get; set; }
[Inject] private ProductService ProductService { get; set; } = default!;
[Inject] private AuthorService AuthorService { get; set; } = default!;
[Inject] private NavigationManager Navigation { get; set; } = default!;
protected bool IsLoading { get; private set; } = true;
protected Product? CurrentProduct { get; private set; }
protected decimal LivePrice { get; private set; } = 0.00m;
protected string AuthorName { get; private set; } = "Unknown Author";
protected string PrimaryCategory { get; private set; } = "General";
protected string ActiveImageUrl { get; set; } = string.Empty;
protected List<string> Thumbnails { get; private set; } = [];
protected int Quantity { get; private set; } = 1;
protected Author? CurrentAuthor { get; private set; }
protected override async Task OnParametersSetAsync()
{
try
{
IsLoading = true;
CurrentProduct = null;
CurrentAuthor = null;
Thumbnails.Clear();
// 1. Resolve product listing details
var productResult = await ProductService.GetProductAsync(BookId);
if (productResult.IsSuccess && productResult.Value != null)
{
CurrentProduct = productResult.Value;
AuthorName = CurrentProduct.Metadata?.Manufacturer ?? "Unknown Author";
// 2. Load pricing data
var priceResult = await ProductService.GetProductPriceAsync(BookId);
LivePrice = priceResult.IsSuccess ? priceResult.Value.Amount : 0m;
// 3. Extract active catalog categories
var categoryResult = await ProductService.GetProductCategoriesAsync(BookId);
if (categoryResult.IsSuccess && categoryResult.Value.Length > 0)
{
PrimaryCategory = categoryResult.Value[0].Name ?? "General";
}
// 4. Retrieve complete contextual model through the newly instantiated AuthorService lookup
var authorResult = await AuthorService.GetAuthorByProductIdAsync(BookId);
if (authorResult.IsSuccess && authorResult.Value != null)
{
CurrentAuthor = authorResult.Value;
// Format fully qualified author text cleanly depending on their publisher model details
if (CurrentAuthor.PublisherType == PublisherTypes.Company && !string.IsNullOrWhiteSpace(CurrentAuthor.Company))
AuthorName = CurrentAuthor.Company;
else
AuthorName = $"{CurrentAuthor.Name} {CurrentAuthor.LastName}".Trim();
}
// 5. Build presentation image viewer variables
if (!string.IsNullOrWhiteSpace(CurrentProduct.ImageUrl))
Thumbnails.Add(CurrentProduct.ImageUrl);
if (CurrentProduct.ThumbnailUrls != null && CurrentProduct.ThumbnailUrls?.Length != 0)
foreach (var url in CurrentProduct.ThumbnailUrls!)
if (!string.IsNullOrWhiteSpace(url) && !Thumbnails.Contains(url)) Thumbnails.Add(url);
ActiveImageUrl = Thumbnails.FirstOrDefault() ?? string.Empty;
}
}
catch (Exception)
{
CurrentProduct = null;
}
finally
{
IsLoading = false;
}
}
protected void IncreaseQty() => Quantity++;
protected void DecreaseQty() { if (Quantity > 1) Quantity--; }
protected void HandleAddToCart()
{
if (CurrentProduct == null) return;
}
protected void ViewAllAuthorBooks()
{
if (CurrentAuthor is not null)
Navigation.NavigateTo($"/?authorId={CurrentAuthor.Id}");
}
}