littleshop/LittleShop/Services/VariantCollectionService.cs
sysadmin eb87148c63 Add variant collections system and enhance ProductVariant with weight/stock tracking
This commit introduces a comprehensive variant management system and enhances
the existing ProductVariant model with per-variant weight overrides and stock
tracking, integrated across Admin Panel and TeleBot.

Features Added:
- Variant Collections: Reusable variant templates (e.g., "Standard Sizes")
- Admin UI for managing variant collections (CRUD operations)
- Dynamic variant editor with JavaScript-based UI
- Per-variant weight and weight unit overrides
- Per-variant stock level tracking
- SalesLedger model for financial tracking

ProductVariant Enhancements:
- Added Weight (decimal, nullable) field for variant-specific weights
- Added WeightUnit (enum, nullable) field for variant-specific units
- Maintains backward compatibility with product-level weights

TeleBot Integration:
- Enhanced variant selection UI to display stock levels
- Shows weight information with proper unit conversion (µg, g, oz, lb, ml, L)
- Compact button format: "Medium (15 in stock, 350g)"
- Real-time stock availability display

Database Migrations:
- 20250928014850_AddVariantCollectionsAndSalesLedger
- 20250928155814_AddWeightToProductVariants

Technical Changes:
- Updated Product model to support VariantCollectionId and VariantsJson
- Extended ProductService with variant collection operations
- Enhanced OrderService to handle variant-specific pricing and weights
- Updated LittleShop.Client DTOs to match server models
- Added JavaScript dynamic variant form builder

Files Modified: 15
Files Added: 17
Lines Changed: ~2000

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-28 17:03:09 +01:00

112 lines
3.4 KiB
C#

using Microsoft.EntityFrameworkCore;
using LittleShop.Data;
using LittleShop.Models;
using LittleShop.DTOs;
namespace LittleShop.Services;
public class VariantCollectionService : IVariantCollectionService
{
private readonly LittleShopContext _context;
public VariantCollectionService(LittleShopContext context)
{
_context = context;
}
public async Task<IEnumerable<VariantCollectionDto>> GetAllVariantCollectionsAsync()
{
return await _context.VariantCollections
.OrderByDescending(vc => vc.CreatedAt)
.Select(vc => new VariantCollectionDto
{
Id = vc.Id,
Name = vc.Name,
PropertiesJson = vc.PropertiesJson,
IsActive = vc.IsActive,
CreatedAt = vc.CreatedAt,
UpdatedAt = vc.UpdatedAt
})
.ToListAsync();
}
public async Task<VariantCollectionDto?> GetVariantCollectionByIdAsync(Guid id)
{
var collection = await _context.VariantCollections
.FirstOrDefaultAsync(vc => vc.Id == id);
if (collection == null) return null;
return new VariantCollectionDto
{
Id = collection.Id,
Name = collection.Name,
PropertiesJson = collection.PropertiesJson,
IsActive = collection.IsActive,
CreatedAt = collection.CreatedAt,
UpdatedAt = collection.UpdatedAt
};
}
public async Task<VariantCollectionDto> CreateVariantCollectionAsync(CreateVariantCollectionDto createDto)
{
var collection = new VariantCollection
{
Id = Guid.NewGuid(),
Name = createDto.Name,
PropertiesJson = string.IsNullOrWhiteSpace(createDto.PropertiesJson) ? "[]" : createDto.PropertiesJson,
IsActive = true,
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow
};
_context.VariantCollections.Add(collection);
await _context.SaveChangesAsync();
return new VariantCollectionDto
{
Id = collection.Id,
Name = collection.Name,
PropertiesJson = collection.PropertiesJson,
IsActive = collection.IsActive,
CreatedAt = collection.CreatedAt,
UpdatedAt = collection.UpdatedAt
};
}
public async Task<bool> UpdateVariantCollectionAsync(Guid id, UpdateVariantCollectionDto updateDto)
{
var collection = await _context.VariantCollections.FindAsync(id);
if (collection == null) return false;
if (!string.IsNullOrEmpty(updateDto.Name))
{
collection.Name = updateDto.Name;
}
if (updateDto.PropertiesJson != null)
{
collection.PropertiesJson = string.IsNullOrWhiteSpace(updateDto.PropertiesJson) ? "[]" : updateDto.PropertiesJson;
}
if (updateDto.IsActive.HasValue)
{
collection.IsActive = updateDto.IsActive.Value;
}
collection.UpdatedAt = DateTime.UtcNow;
await _context.SaveChangesAsync();
return true;
}
public async Task<bool> DeleteVariantCollectionAsync(Guid id)
{
var collection = await _context.VariantCollections.FindAsync(id);
if (collection == null) return false;
collection.IsActive = false;
collection.UpdatedAt = DateTime.UtcNow;
await _context.SaveChangesAsync();
return true;
}
}