Add two basic CQRS commands/queries
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
using MediatR;
|
||||
using Printbase.Application.Products.Dtos;
|
||||
|
||||
namespace Printbase.Application.Products.Commands.CreateProduct;
|
||||
|
||||
public class CreateProductCommand : IRequest<ProductDto>
|
||||
{
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string? Description { get; set; }
|
||||
public Guid TypeId { get; set; }
|
||||
public List<CreateProductVariantDto>? Variants { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
using MediatR;
|
||||
using Printbase.Application.Products.Dtos;
|
||||
using Printbase.Domain.Entities.Products;
|
||||
using Printbase.Domain.Repositories;
|
||||
|
||||
namespace Printbase.Application.Products.Commands.CreateProduct;
|
||||
|
||||
public class CreateProductCommandHandler(
|
||||
IProductRepository productRepository,
|
||||
IProductVariantRepository variantRepository,
|
||||
IProductTypeRepository typeRepository)
|
||||
: IRequestHandler<CreateProductCommand, ProductDto>
|
||||
{
|
||||
private readonly IProductRepository _productRepository = productRepository ?? throw new ArgumentNullException(nameof(productRepository));
|
||||
private readonly IProductVariantRepository _variantRepository = variantRepository ?? throw new ArgumentNullException(nameof(variantRepository));
|
||||
private readonly IProductTypeRepository _typeRepository = typeRepository ?? throw new ArgumentNullException(nameof(typeRepository));
|
||||
|
||||
public async Task<ProductDto> Handle(CreateProductCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var productType = await _typeRepository.GetByIdAsync(request.TypeId, includeRelations: true, cancellationToken);
|
||||
if (productType == null)
|
||||
{
|
||||
throw new ArgumentException($"Product type with ID {request.TypeId} not found");
|
||||
}
|
||||
|
||||
var product = new Product
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Name = request.Name,
|
||||
Description = request.Description,
|
||||
TypeId = request.TypeId,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
IsActive = true
|
||||
};
|
||||
|
||||
var createdProduct = await _productRepository.AddAsync(product, cancellationToken);
|
||||
|
||||
var productVariants = new List<ProductVariant>();
|
||||
if (request.Variants != null && request.Variants.Count != 0)
|
||||
{
|
||||
foreach (var variant in request.Variants.Select(variantDto => new ProductVariant
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
ProductId = createdProduct.Id,
|
||||
Color = variantDto.Color,
|
||||
Size = variantDto.Size,
|
||||
Price = variantDto.Price,
|
||||
Discount = variantDto.Discount,
|
||||
Stock = variantDto.Stock,
|
||||
SKU = variantDto.SKU ?? GenerateSku(createdProduct.Name, variantDto.Color, variantDto.Size),
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
IsActive = true
|
||||
}))
|
||||
{
|
||||
var createdVariant = await _variantRepository.AddAsync(variant, cancellationToken);
|
||||
productVariants.Add(createdVariant);
|
||||
}
|
||||
}
|
||||
|
||||
var productDto = new ProductDto
|
||||
{
|
||||
Id = createdProduct.Id,
|
||||
Name = createdProduct.Name,
|
||||
Description = createdProduct.Description,
|
||||
TypeId = createdProduct.TypeId,
|
||||
TypeName = productType.Name,
|
||||
GroupName = productType.Group.Name,
|
||||
CreatedAt = createdProduct.CreatedAt,
|
||||
IsActive = createdProduct.IsActive,
|
||||
Variants = productVariants.Select(v => new ProductVariantDto
|
||||
{
|
||||
Id = v.Id,
|
||||
Color = v.Color,
|
||||
Size = v.Size,
|
||||
Price = v.Price,
|
||||
Discount = v.Discount,
|
||||
Stock = v.Stock,
|
||||
SKU = v.SKU,
|
||||
IsActive = v.IsActive
|
||||
}).ToList()
|
||||
};
|
||||
|
||||
return productDto;
|
||||
}
|
||||
|
||||
private static string GenerateSku(string productName, string? color, string? size)
|
||||
{
|
||||
var prefix = productName.Length >= 3 ? productName[..3].ToUpper() : productName.ToUpper();
|
||||
var colorPart = !string.IsNullOrEmpty(color) ? color[..Math.Min(3, color.Length)].ToUpper() : "XXX";
|
||||
var sizePart = !string.IsNullOrEmpty(size) ? size.ToUpper() : "OS";
|
||||
var randomPart = new Random().Next(100, 999).ToString();
|
||||
|
||||
return $"{prefix}-{colorPart}-{sizePart}-{randomPart}";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace Printbase.Application.Products.Commands.CreateProduct;
|
||||
|
||||
public class CreateProductVariantDto
|
||||
{
|
||||
public string? Color { get; set; }
|
||||
public string? Size { get; set; }
|
||||
public decimal Price { get; set; }
|
||||
public decimal? Discount { get; set; }
|
||||
public int Stock { get; set; }
|
||||
public string? SKU { get; set; }
|
||||
}
|
||||
15
src/Printbase.Application/Products/Dtos/ProductDto.cs
Normal file
15
src/Printbase.Application/Products/Dtos/ProductDto.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace Printbase.Application.Products.Dtos;
|
||||
|
||||
public class ProductDto
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string? Description { get; set; }
|
||||
public Guid TypeId { get; set; }
|
||||
public string TypeName { get; set; } = string.Empty;
|
||||
public string GroupName { get; set; } = string.Empty;
|
||||
public ICollection<ProductVariantDto>? Variants { get; set; }
|
||||
public DateTime CreatedAt { get; set; }
|
||||
public DateTime? UpdatedAt { get; set; }
|
||||
public bool IsActive { get; set; }
|
||||
}
|
||||
14
src/Printbase.Application/Products/Dtos/ProductVariantDto.cs
Normal file
14
src/Printbase.Application/Products/Dtos/ProductVariantDto.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace Printbase.Application.Products.Dtos;
|
||||
|
||||
public class ProductVariantDto
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string? Color { get; set; }
|
||||
public string? Size { get; set; }
|
||||
public decimal Price { get; set; }
|
||||
public decimal? Discount { get; set; }
|
||||
public decimal DiscountedPrice => Discount is > 0 ? Price - Price * Discount.Value / 100m : Price;
|
||||
public int Stock { get; set; }
|
||||
public string? SKU { get; set; }
|
||||
public bool IsActive { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using MediatR;
|
||||
using Printbase.Application.Products.Dtos;
|
||||
|
||||
namespace Printbase.Application.Products.Queries;
|
||||
|
||||
public class GetProductByIdQuery(Guid id, bool includeVariants = true) : IRequest<ProductDto?>
|
||||
{
|
||||
public Guid Id { get; } = id;
|
||||
public bool IncludeVariants { get; } = includeVariants;
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
using AutoMapper;
|
||||
using MediatR;
|
||||
using Printbase.Application.Products.Dtos;
|
||||
using Printbase.Domain.Repositories;
|
||||
|
||||
namespace Printbase.Application.Products.Queries.GetProductById;
|
||||
|
||||
public class GetProductByIdQueryHandler(IProductRepository productRepository, IMapper mapper)
|
||||
: IRequestHandler<GetProductByIdQuery, ProductDto?>
|
||||
{
|
||||
private readonly IProductRepository _productRepository = productRepository
|
||||
?? throw new ArgumentNullException(nameof(productRepository));
|
||||
private readonly IMapper _mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
|
||||
|
||||
public async Task<ProductDto?> Handle(GetProductByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var product = await _productRepository.GetByIdAsync(request.Id, includeRelations: true, cancellationToken);
|
||||
|
||||
if (product == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var productDto = new ProductDto
|
||||
{
|
||||
Id = product.Id,
|
||||
Name = product.Name,
|
||||
Description = product.Description,
|
||||
TypeId = product.TypeId,
|
||||
TypeName = product.Type.Name,
|
||||
GroupName = product.Type.Group.Name,
|
||||
CreatedAt = product.CreatedAt,
|
||||
UpdatedAt = product.UpdatedAt,
|
||||
IsActive = product.IsActive
|
||||
};
|
||||
|
||||
if (request.IncludeVariants)
|
||||
{
|
||||
productDto.Variants = product.Variants
|
||||
.Select(v => new ProductVariantDto
|
||||
{
|
||||
Id = v.Id,
|
||||
Color = v.Color,
|
||||
Size = v.Size,
|
||||
Price = v.Price,
|
||||
Discount = v.Discount,
|
||||
Stock = v.Stock,
|
||||
SKU = v.SKU,
|
||||
IsActive = v.IsActive
|
||||
})
|
||||
.ToList();
|
||||
}
|
||||
|
||||
return productDto;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user