Fix-variant-display-in-API

This commit is contained in:
sysadmin 2025-10-03 20:20:25 +01:00
parent e5f19f8b83
commit 8075560877
2 changed files with 59 additions and 4 deletions

View File

@ -22,6 +22,7 @@ public class ProductService : IProductService
.Include(p => p.Category) .Include(p => p.Category)
.Include(p => p.Photos) .Include(p => p.Photos)
.Include(p => p.MultiBuys.Where(v => v.IsActive)) .Include(p => p.MultiBuys.Where(v => v.IsActive))
.Include(p => p.Variants.Where(v => v.IsActive))
.Where(p => p.IsActive) .Where(p => p.IsActive)
.Select(p => new ProductDto .Select(p => new ProductDto
{ {
@ -58,6 +59,19 @@ public class ProductService : IProductService
IsActive = mb.IsActive, IsActive = mb.IsActive,
CreatedAt = mb.CreatedAt, CreatedAt = mb.CreatedAt,
UpdatedAt = mb.UpdatedAt UpdatedAt = mb.UpdatedAt
}).ToList(),
Variants = p.Variants.Select(v => new ProductVariantDto
{
Id = v.Id,
ProductId = v.ProductId,
VariantType = v.VariantType,
Name = v.Name,
Price = v.Price,
StockLevel = v.StockLevel,
SortOrder = v.SortOrder,
IsActive = v.IsActive,
CreatedAt = v.CreatedAt,
UpdatedAt = v.UpdatedAt
}).ToList() }).ToList()
}) })
.ToListAsync(); .ToListAsync();
@ -69,6 +83,7 @@ public class ProductService : IProductService
.Include(p => p.Category) .Include(p => p.Category)
.Include(p => p.Photos) .Include(p => p.Photos)
.Include(p => p.MultiBuys.Where(v => v.IsActive)) .Include(p => p.MultiBuys.Where(v => v.IsActive))
.Include(p => p.Variants.Where(v => v.IsActive))
.Where(p => p.IsActive && p.CategoryId == categoryId) .Where(p => p.IsActive && p.CategoryId == categoryId)
.Select(p => new ProductDto .Select(p => new ProductDto
{ {
@ -105,6 +120,19 @@ public class ProductService : IProductService
IsActive = mb.IsActive, IsActive = mb.IsActive,
CreatedAt = mb.CreatedAt, CreatedAt = mb.CreatedAt,
UpdatedAt = mb.UpdatedAt UpdatedAt = mb.UpdatedAt
}).ToList(),
Variants = p.Variants.Select(v => new ProductVariantDto
{
Id = v.Id,
ProductId = v.ProductId,
VariantType = v.VariantType,
Name = v.Name,
Price = v.Price,
StockLevel = v.StockLevel,
SortOrder = v.SortOrder,
IsActive = v.IsActive,
CreatedAt = v.CreatedAt,
UpdatedAt = v.UpdatedAt
}).ToList() }).ToList()
}) })
.ToListAsync(); .ToListAsync();
@ -116,6 +144,7 @@ public class ProductService : IProductService
.Include(p => p.Category) .Include(p => p.Category)
.Include(p => p.Photos) .Include(p => p.Photos)
.Include(p => p.MultiBuys.Where(v => v.IsActive)) .Include(p => p.MultiBuys.Where(v => v.IsActive))
.Include(p => p.Variants.Where(v => v.IsActive))
.FirstOrDefaultAsync(p => p.Id == id); .FirstOrDefaultAsync(p => p.Id == id);
if (product == null) return null; if (product == null) return null;
@ -157,6 +186,19 @@ public class ProductService : IProductService
IsActive = v.IsActive, IsActive = v.IsActive,
CreatedAt = v.CreatedAt, CreatedAt = v.CreatedAt,
UpdatedAt = v.UpdatedAt UpdatedAt = v.UpdatedAt
}).ToList(),
Variants = product.Variants.OrderBy(v => v.SortOrder).Select(v => new ProductVariantDto
{
Id = v.Id,
ProductId = v.ProductId,
VariantType = v.VariantType,
Name = v.Name,
Price = v.Price,
StockLevel = v.StockLevel,
SortOrder = v.SortOrder,
IsActive = v.IsActive,
CreatedAt = v.CreatedAt,
UpdatedAt = v.UpdatedAt
}).ToList() }).ToList()
}; };
} }

View File

@ -385,13 +385,19 @@ namespace TeleBot.UI
{ {
string variantInfo = ""; string variantInfo = "";
// Show price if different from base product price
if (variant.Price.HasValue && variant.Price.Value != product.Price)
{
variantInfo = $" - £{variant.Price.Value:F2}";
}
if (variant.StockLevel > 0) if (variant.StockLevel > 0)
{ {
variantInfo = $" ({variant.StockLevel} in stock"; variantInfo += variantInfo == "" ? $" ({variant.StockLevel} in stock" : $" ({variant.StockLevel} in stock";
} }
else if (variant.StockLevel == 0) else if (variant.StockLevel == 0)
{ {
variantInfo = " (Out of stock"; variantInfo += variantInfo == "" ? " (Out of stock" : " (Out of stock";
} }
if (variant.Weight.HasValue) if (variant.Weight.HasValue)
@ -406,10 +412,17 @@ namespace TeleBot.UI
6 => "L", 6 => "L",
_ => "unit" _ => "unit"
}; };
variantInfo += variantInfo == "" ? $" ({variant.Weight}{unitName}" : $", {variant.Weight}{unitName}"; if (variantInfo.Contains("("))
{
variantInfo += $", {variant.Weight}{unitName}";
}
else
{
variantInfo += variantInfo == "" ? $" ({variant.Weight}{unitName}" : $" ({variant.Weight}{unitName}";
}
} }
if (variantInfo != "") if (variantInfo.Contains("(") && !variantInfo.EndsWith(")"))
{ {
variantInfo += ")"; variantInfo += ")";
} }