Remove almost everything :(

This commit is contained in:
lumijiez
2025-05-26 11:42:52 +03:00
parent ce452e0df4
commit e52cef083b
35 changed files with 1 additions and 1935 deletions

View File

@@ -1,12 +0,0 @@
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; }
}

View File

@@ -1,95 +0,0 @@
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;
}
public 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}";
}
}

View File

@@ -1,11 +0,0 @@
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; }
}

View File

@@ -1,6 +0,0 @@
namespace Printbase.Application.Products.Dtos;
public class AllProductsDto
{
public ICollection<ProductDto> Products {get;set;}
}

View File

@@ -1,15 +0,0 @@
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; } = new List<ProductVariantDto>();
public DateTime CreatedAt { get; set; }
public DateTime? UpdatedAt { get; set; }
public bool IsActive { get; set; }
}

View File

@@ -1,14 +0,0 @@
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; }
}

View File

@@ -1,9 +0,0 @@
using MediatR;
using Printbase.Application.Products.Dtos;
namespace Printbase.Application.Products.Queries.GetAllProducts;
public class GetAllProductsQuery(bool includeVariants = true) : IRequest<AllProductsDto?>
{
public bool IncludeVariants { get; } = includeVariants;
}

View File

@@ -1,48 +0,0 @@
using MediatR;
using Printbase.Application.Products.Dtos;
using Printbase.Domain.Repositories;
namespace Printbase.Application.Products.Queries.GetAllProducts;
public class GetAllProductsQueryHandler(IProductRepository productRepository)
: IRequestHandler<GetAllProductsQuery, AllProductsDto?>
{
private readonly IProductRepository _productRepository = productRepository
?? throw new ArgumentNullException(nameof(productRepository));
public async Task<AllProductsDto?> Handle(GetAllProductsQuery request, CancellationToken cancellationToken)
{
var products = await _productRepository.GetAllAsync(true, cancellationToken);
var allProducts = new AllProductsDto
{
Products = products.Select(p => new ProductDto
{
Id = p.Id,
Name = p.Name,
Description = p.Description,
TypeId = p.TypeId,
TypeName = p.Type.Name,
GroupName = p.Type.Group.Name,
CreatedAt = p.CreatedAt,
UpdatedAt = p.UpdatedAt,
IsActive = p.IsActive,
Variants = request.IncludeVariants
? p.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()
: []
}).ToList()
};
return allProducts;
}
}

View File

@@ -1,10 +0,0 @@
using MediatR;
using Printbase.Application.Products.Dtos;
namespace Printbase.Application.Products.Queries.GetProductById;
public class GetProductByIdQuery(Guid id, bool includeVariants = true) : IRequest<ProductDto?>
{
public Guid Id { get; } = id;
public bool IncludeVariants { get; } = includeVariants;
}

View File

@@ -1,56 +0,0 @@
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;
}
}