Add validators and services for Product Galleries and Product Tags

- Implemented Create, Delete, Get, and Update validators for Product Galleries.
- Added Create, Delete, Get, and Update validators for Product Tags.
- Created service classes for handling Discount Categories, Discount Orders, Discount Products, Discount Shopping Cart, Product Categories, Product Galleries, and Product Tags.
- Each service class integrates with CQRS for command and query handling.
- Established mapping profiles for Product Galleries.
This commit is contained in:
masoodafar-web
2025-12-04 02:40:49 +03:30
parent 40d54d08fc
commit f0f48118e7
436 changed files with 33159 additions and 2005 deletions
@@ -0,0 +1,9 @@
namespace CMSMicroservice.Application.ProductCategoryCQ.Commands.CreateNewProductCategory;
public record CreateNewProductCategoryCommand : IRequest<CreateNewProductCategoryResponseDto>
{
//شناسه محصول
public long ProductId { get; init; }
//شناسه دسته بندی
public long CategoryId { get; init; }
}
@@ -0,0 +1,21 @@
using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.ProductCategoryCQ.Commands.CreateNewProductCategory;
public class CreateNewProductCategoryCommandHandler : IRequestHandler<CreateNewProductCategoryCommand, CreateNewProductCategoryResponseDto>
{
private readonly IApplicationDbContext _context;
public CreateNewProductCategoryCommandHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<CreateNewProductCategoryResponseDto> Handle(CreateNewProductCategoryCommand request,
CancellationToken cancellationToken)
{
var entity = request.Adapt<ProductCategory>();
await _context.ProductCategories.AddAsync(entity, cancellationToken);
entity.AddDomainEvent(new CreateNewProductCategoryEvent(entity));
await _context.SaveChangesAsync(cancellationToken);
return entity.Adapt<CreateNewProductCategoryResponseDto>();
}
}
@@ -0,0 +1,18 @@
namespace CMSMicroservice.Application.ProductCategoryCQ.Commands.CreateNewProductCategory;
public class CreateNewProductCategoryCommandValidator : AbstractValidator<CreateNewProductCategoryCommand>
{
public CreateNewProductCategoryCommandValidator()
{
RuleFor(model => model.ProductId)
.NotNull();
RuleFor(model => model.CategoryId)
.NotNull();
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(ValidationContext<CreateNewProductCategoryCommand>.CreateWithOptions((CreateNewProductCategoryCommand)model, x => x.IncludeProperties(propertyName)));
if (result.IsValid)
return Array.Empty<string>();
return result.Errors.Select(e => e.ErrorMessage);
};
}
@@ -0,0 +1,7 @@
namespace CMSMicroservice.Application.ProductCategoryCQ.Commands.CreateNewProductCategory;
public class CreateNewProductCategoryResponseDto
{
//شناسه
public long Id { get; set; }
}
@@ -0,0 +1,7 @@
namespace CMSMicroservice.Application.ProductCategoryCQ.Commands.DeleteProductCategory;
public record DeleteProductCategoryCommand : IRequest<Unit>
{
//شناسه
public long Id { get; init; }
}
@@ -0,0 +1,22 @@
using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.ProductCategoryCQ.Commands.DeleteProductCategory;
public class DeleteProductCategoryCommandHandler : IRequestHandler<DeleteProductCategoryCommand, Unit>
{
private readonly IApplicationDbContext _context;
public DeleteProductCategoryCommandHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<Unit> Handle(DeleteProductCategoryCommand request, CancellationToken cancellationToken)
{
var entity = await _context.ProductCategories
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(ProductCategory), request.Id);
entity.IsDeleted = true;
_context.ProductCategories.Update(entity);
entity.AddDomainEvent(new DeleteProductCategoryEvent(entity));
await _context.SaveChangesAsync(cancellationToken);
return Unit.Value;
}
}
@@ -0,0 +1,16 @@
namespace CMSMicroservice.Application.ProductCategoryCQ.Commands.DeleteProductCategory;
public class DeleteProductCategoryCommandValidator : AbstractValidator<DeleteProductCategoryCommand>
{
public DeleteProductCategoryCommandValidator()
{
RuleFor(model => model.Id)
.NotNull();
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(ValidationContext<DeleteProductCategoryCommand>.CreateWithOptions((DeleteProductCategoryCommand)model, x => x.IncludeProperties(propertyName)));
if (result.IsValid)
return Array.Empty<string>();
return result.Errors.Select(e => e.ErrorMessage);
};
}
@@ -0,0 +1,11 @@
namespace CMSMicroservice.Application.ProductCategoryCQ.Commands.UpdateProductCategory;
public record UpdateProductCategoryCommand : IRequest<Unit>
{
//شناسه
public long Id { get; init; }
//شناسه محصول
public long ProductId { get; init; }
//شناسه دسته بندی
public long CategoryId { get; init; }
}
@@ -0,0 +1,22 @@
using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.ProductCategoryCQ.Commands.UpdateProductCategory;
public class UpdateProductCategoryCommandHandler : IRequestHandler<UpdateProductCategoryCommand, Unit>
{
private readonly IApplicationDbContext _context;
public UpdateProductCategoryCommandHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<Unit> Handle(UpdateProductCategoryCommand request, CancellationToken cancellationToken)
{
var entity = await _context.ProductCategories
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(ProductCategory), request.Id);
request.Adapt(entity);
_context.ProductCategories.Update(entity);
entity.AddDomainEvent(new UpdateProductCategoryEvent(entity));
await _context.SaveChangesAsync(cancellationToken);
return Unit.Value;
}
}
@@ -0,0 +1,20 @@
namespace CMSMicroservice.Application.ProductCategoryCQ.Commands.UpdateProductCategory;
public class UpdateProductCategoryCommandValidator : AbstractValidator<UpdateProductCategoryCommand>
{
public UpdateProductCategoryCommandValidator()
{
RuleFor(model => model.Id)
.NotNull();
RuleFor(model => model.ProductId)
.NotNull();
RuleFor(model => model.CategoryId)
.NotNull();
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(ValidationContext<UpdateProductCategoryCommand>.CreateWithOptions((UpdateProductCategoryCommand)model, x => x.IncludeProperties(propertyName)));
if (result.IsValid)
return Array.Empty<string>();
return result.Errors.Select(e => e.ErrorMessage);
};
}
@@ -0,0 +1,22 @@
using CMSMicroservice.Domain.Events;
using Microsoft.Extensions.Logging;
namespace CMSMicroservice.Application.ProductCategoryCQ.EventHandlers;
public class CreateNewProductCategoryEventHandler : INotificationHandler<CreateNewProductCategoryEvent>
{
private readonly ILogger<
CreateNewProductCategoryEventHandler> _logger;
public CreateNewProductCategoryEventHandler(ILogger<CreateNewProductCategoryEventHandler> logger)
{
_logger = logger;
}
public Task Handle(CreateNewProductCategoryEvent notification, CancellationToken cancellationToken)
{
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
return Task.CompletedTask;
}
}
@@ -0,0 +1,22 @@
using CMSMicroservice.Domain.Events;
using Microsoft.Extensions.Logging;
namespace CMSMicroservice.Application.ProductCategoryCQ.EventHandlers;
public class DeleteProductCategoryEventHandler : INotificationHandler<DeleteProductCategoryEvent>
{
private readonly ILogger<
DeleteProductCategoryEventHandler> _logger;
public DeleteProductCategoryEventHandler(ILogger<DeleteProductCategoryEventHandler> logger)
{
_logger = logger;
}
public Task Handle(DeleteProductCategoryEvent notification, CancellationToken cancellationToken)
{
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
return Task.CompletedTask;
}
}
@@ -0,0 +1,22 @@
using CMSMicroservice.Domain.Events;
using Microsoft.Extensions.Logging;
namespace CMSMicroservice.Application.ProductCategoryCQ.EventHandlers;
public class UpdateProductCategoryEventHandler : INotificationHandler<UpdateProductCategoryEvent>
{
private readonly ILogger<
UpdateProductCategoryEventHandler> _logger;
public UpdateProductCategoryEventHandler(ILogger<UpdateProductCategoryEventHandler> logger)
{
_logger = logger;
}
public Task Handle(UpdateProductCategoryEvent notification, CancellationToken cancellationToken)
{
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
return Task.CompletedTask;
}
}
@@ -0,0 +1,19 @@
namespace CMSMicroservice.Application.ProductCategoryCQ.Queries.GetAllProductCategoryByFilter;
public record GetAllProductCategoryByFilterQuery : IRequest<GetAllProductCategoryByFilterResponseDto>
{
//موقعیت صفحه بندی
public PaginationState? PaginationState { get; init; }
//مرتب سازی بر اساس
public string? SortBy { get; init; }
//فیلتر
public GetAllProductCategoryByFilterFilter? Filter { get; init; }
}public class GetAllProductCategoryByFilterFilter
{
//شناسه
public long? Id { get; set; }
//شناسه محصول
public long? ProductId { get; set; }
//شناسه دسته بندی
public long? CategoryId { get; set; }
}
@@ -0,0 +1,32 @@
namespace CMSMicroservice.Application.ProductCategoryCQ.Queries.GetAllProductCategoryByFilter;
public class GetAllProductCategoryByFilterQueryHandler : IRequestHandler<GetAllProductCategoryByFilterQuery, GetAllProductCategoryByFilterResponseDto>
{
private readonly IApplicationDbContext _context;
public GetAllProductCategoryByFilterQueryHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<GetAllProductCategoryByFilterResponseDto> Handle(GetAllProductCategoryByFilterQuery request, CancellationToken cancellationToken)
{
var query = _context.ProductCategories
.ApplyOrder(sortBy: request.SortBy)
.AsNoTracking()
.AsQueryable();
if (request.Filter is not null)
{
query = query
.Where(x => request.Filter.Id == null || x.Id == request.Filter.Id)
.Where(x => request.Filter.ProductId == null || x.ProductId==request.Filter.ProductId)
.Where(x => request.Filter.CategoryId == null || x.CategoryId==request.Filter.CategoryId)
;
}
return new GetAllProductCategoryByFilterResponseDto
{
MetaData = await query.GetMetaData(request.PaginationState, cancellationToken),
Models = await query.PaginatedListAsync(paginationState: request.PaginationState)
.ProjectToType<GetAllProductCategoryByFilterResponseModel>().ToListAsync(cancellationToken)
};
}
}
@@ -0,0 +1,14 @@
namespace CMSMicroservice.Application.ProductCategoryCQ.Queries.GetAllProductCategoryByFilter;
public class GetAllProductCategoryByFilterQueryValidator : AbstractValidator<GetAllProductCategoryByFilterQuery>
{
public GetAllProductCategoryByFilterQueryValidator()
{
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(ValidationContext<GetAllProductCategoryByFilterQuery>.CreateWithOptions((GetAllProductCategoryByFilterQuery)model, x => x.IncludeProperties(propertyName)));
if (result.IsValid)
return Array.Empty<string>();
return result.Errors.Select(e => e.ErrorMessage);
};
}
@@ -0,0 +1,17 @@
namespace CMSMicroservice.Application.ProductCategoryCQ.Queries.GetAllProductCategoryByFilter;
public class GetAllProductCategoryByFilterResponseDto
{
//متادیتا
public MetaData MetaData { get; set; }
//مدل خروجی
public List<GetAllProductCategoryByFilterResponseModel>? Models { get; set; }
}public class GetAllProductCategoryByFilterResponseModel
{
//شناسه
public long Id { get; set; }
//شناسه محصول
public long ProductId { get; set; }
//شناسه دسته بندی
public long CategoryId { get; set; }
}
@@ -0,0 +1,7 @@
namespace CMSMicroservice.Application.ProductCategoryCQ.Queries.GetProductCategory;
public record GetProductCategoryQuery : IRequest<GetProductCategoryResponseDto>
{
//شناسه
public long Id { get; init; }
}
@@ -0,0 +1,22 @@
namespace CMSMicroservice.Application.ProductCategoryCQ.Queries.GetProductCategory;
public class GetProductCategoryQueryHandler : IRequestHandler<GetProductCategoryQuery, GetProductCategoryResponseDto>
{
private readonly IApplicationDbContext _context;
public GetProductCategoryQueryHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<GetProductCategoryResponseDto> Handle(GetProductCategoryQuery request,
CancellationToken cancellationToken)
{
var response = await _context.ProductCategories
.AsNoTracking()
.Where(x => x.Id == request.Id)
.ProjectToType<GetProductCategoryResponseDto>()
.FirstOrDefaultAsync(cancellationToken);
return response ?? throw new NotFoundException(nameof(ProductCategory), request.Id);
}
}
@@ -0,0 +1,16 @@
namespace CMSMicroservice.Application.ProductCategoryCQ.Queries.GetProductCategory;
public class GetProductCategoryQueryValidator : AbstractValidator<GetProductCategoryQuery>
{
public GetProductCategoryQueryValidator()
{
RuleFor(model => model.Id)
.NotNull();
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(ValidationContext<GetProductCategoryQuery>.CreateWithOptions((GetProductCategoryQuery)model, x => x.IncludeProperties(propertyName)));
if (result.IsValid)
return Array.Empty<string>();
return result.Errors.Select(e => e.ErrorMessage);
};
}
@@ -0,0 +1,11 @@
namespace CMSMicroservice.Application.ProductCategoryCQ.Queries.GetProductCategory;
public class GetProductCategoryResponseDto
{
//شناسه
public long Id { get; set; }
//شناسه محصول
public long ProductId { get; set; }
//شناسه دسته بندی
public long CategoryId { get; set; }
}