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.ProductGalleriesCQ.Commands.CreateNewProductGalleries;
public record CreateNewProductGalleriesCommand : IRequest<CreateNewProductGallerysResponseDto>
{
//
public long ProductImageId { get; init; }
//
public long ProductId { get; init; }
}
@@ -0,0 +1,21 @@
using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.ProductGalleriesCQ.Commands.CreateNewProductGalleries;
public class CreateNewProductGallerysCommandHandler : IRequestHandler<CreateNewProductGalleriesCommand, CreateNewProductGallerysResponseDto>
{
private readonly IApplicationDbContext _context;
public CreateNewProductGallerysCommandHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<CreateNewProductGallerysResponseDto> Handle(CreateNewProductGalleriesCommand request,
CancellationToken cancellationToken)
{
var entity = request.Adapt<ProductGallery>();
await _context.ProductGalleries.AddAsync(entity, cancellationToken);
entity.AddDomainEvent(new CreateNewProductGallerysEvent(entity));
await _context.SaveChangesAsync(cancellationToken);
return entity.Adapt<CreateNewProductGallerysResponseDto>();
}
}
@@ -0,0 +1,18 @@
namespace CMSMicroservice.Application.ProductGalleriesCQ.Commands.CreateNewProductGalleries;
public class CreateNewProductGallerysCommandValidator : AbstractValidator<CreateNewProductGalleriesCommand>
{
public CreateNewProductGallerysCommandValidator()
{
RuleFor(model => model.ProductImageId)
.NotNull();
RuleFor(model => model.ProductId)
.NotNull();
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(ValidationContext<CreateNewProductGalleriesCommand>.CreateWithOptions((CreateNewProductGalleriesCommand)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.ProductGalleriesCQ.Commands.CreateNewProductGalleries;
public class CreateNewProductGallerysResponseDto
{
//
public long Id { get; set; }
}
@@ -0,0 +1,7 @@
namespace CMSMicroservice.Application.ProductGalleriesCQ.Commands.DeleteProductGalleries;
public record DeleteProductGalleriesCommand : IRequest<Unit>
{
//
public long Id { get; init; }
}
@@ -0,0 +1,22 @@
using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.ProductGalleriesCQ.Commands.DeleteProductGalleries;
public class DeleteProductGallerysCommandHandler : IRequestHandler<DeleteProductGalleriesCommand, Unit>
{
private readonly IApplicationDbContext _context;
public DeleteProductGallerysCommandHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<Unit> Handle(DeleteProductGalleriesCommand request, CancellationToken cancellationToken)
{
var entity = await _context.ProductGalleries
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(ProductGallery), request.Id);
entity.IsDeleted = true;
_context.ProductGalleries.Update(entity);
entity.AddDomainEvent(new DeleteProductGallerysEvent(entity));
await _context.SaveChangesAsync(cancellationToken);
return Unit.Value;
}
}
@@ -0,0 +1,16 @@
namespace CMSMicroservice.Application.ProductGalleriesCQ.Commands.DeleteProductGalleries;
public class DeleteProductGallerysCommandValidator : AbstractValidator<DeleteProductGalleriesCommand>
{
public DeleteProductGallerysCommandValidator()
{
RuleFor(model => model.Id)
.NotNull();
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(ValidationContext<DeleteProductGalleriesCommand>.CreateWithOptions((DeleteProductGalleriesCommand)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.ProductGalleriesCQ.Commands.UpdateProductGalleries;
public record UpdateProductGalleriesCommand : IRequest<Unit>
{
//
public long Id { get; init; }
//
public long ProductImageId { get; init; }
//
public long ProductId { get; init; }
}
@@ -0,0 +1,22 @@
using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.ProductGalleriesCQ.Commands.UpdateProductGalleries;
public class UpdateProductGallerysCommandHandler : IRequestHandler<UpdateProductGalleriesCommand, Unit>
{
private readonly IApplicationDbContext _context;
public UpdateProductGallerysCommandHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<Unit> Handle(UpdateProductGalleriesCommand request, CancellationToken cancellationToken)
{
var entity = await _context.ProductGalleries
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(ProductGallery), request.Id);
request.Adapt(entity);
_context.ProductGalleries.Update(entity);
entity.AddDomainEvent(new UpdateProductGallerysEvent(entity));
await _context.SaveChangesAsync(cancellationToken);
return Unit.Value;
}
}
@@ -0,0 +1,20 @@
namespace CMSMicroservice.Application.ProductGalleriesCQ.Commands.UpdateProductGalleries;
public class UpdateProductGallerysCommandValidator : AbstractValidator<UpdateProductGalleriesCommand>
{
public UpdateProductGallerysCommandValidator()
{
RuleFor(model => model.Id)
.NotNull();
RuleFor(model => model.ProductImageId)
.NotNull();
RuleFor(model => model.ProductId)
.NotNull();
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(ValidationContext<UpdateProductGalleriesCommand>.CreateWithOptions((UpdateProductGalleriesCommand)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.ProductGalleriesCQ.EventHandlers;
public class CreateNewProductGallerysEventHandler : INotificationHandler<CreateNewProductGallerysEvent>
{
private readonly ILogger<
CreateNewProductGallerysEventHandler> _logger;
public CreateNewProductGallerysEventHandler(ILogger<CreateNewProductGallerysEventHandler> logger)
{
_logger = logger;
}
public Task Handle(CreateNewProductGallerysEvent 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.ProductGalleriesCQ.EventHandlers;
public class DeleteProductGallerysEventHandler : INotificationHandler<DeleteProductGallerysEvent>
{
private readonly ILogger<
DeleteProductGallerysEventHandler> _logger;
public DeleteProductGallerysEventHandler(ILogger<DeleteProductGallerysEventHandler> logger)
{
_logger = logger;
}
public Task Handle(DeleteProductGallerysEvent 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.ProductGalleriesCQ.EventHandlers;
public class UpdateProductGallerysEventHandler : INotificationHandler<UpdateProductGallerysEvent>
{
private readonly ILogger<
UpdateProductGallerysEventHandler> _logger;
public UpdateProductGallerysEventHandler(ILogger<UpdateProductGallerysEventHandler> logger)
{
_logger = logger;
}
public Task Handle(UpdateProductGallerysEvent notification, CancellationToken cancellationToken)
{
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
return Task.CompletedTask;
}
}
@@ -0,0 +1,19 @@
namespace CMSMicroservice.Application.ProductGalleriesCQ.Queries.GetAllProductGalleriesByFilter;
public record GetAllProductGalleriesByFilterQuery : IRequest<GetAllProductGallerysByFilterResponseDto>
{
//موقعیت صفحه بندی
public PaginationState? PaginationState { get; init; }
//مرتب سازی بر اساس
public string? SortBy { get; init; }
//فیلتر
public GetAllProductGallerysByFilterFilter? Filter { get; init; }
}public class GetAllProductGallerysByFilterFilter
{
//
public long? Id { get; set; }
//
public long? ProductImageId { get; set; }
//
public long? ProductId { get; set; }
}
@@ -0,0 +1,32 @@
namespace CMSMicroservice.Application.ProductGalleriesCQ.Queries.GetAllProductGalleriesByFilter;
public class GetAllProductGallerysByFilterQueryHandler : IRequestHandler<GetAllProductGalleriesByFilterQuery, GetAllProductGallerysByFilterResponseDto>
{
private readonly IApplicationDbContext _context;
public GetAllProductGallerysByFilterQueryHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<GetAllProductGallerysByFilterResponseDto> Handle(GetAllProductGalleriesByFilterQuery request, CancellationToken cancellationToken)
{
var query = _context.ProductGalleries
.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.ProductImageId == null || x.ProductImageId == request.Filter.ProductImageId)
.Where(x => request.Filter.ProductId == null || x.ProductId == request.Filter.ProductId)
;
}
return new GetAllProductGallerysByFilterResponseDto
{
MetaData = await query.GetMetaData(request.PaginationState, cancellationToken),
Models = await query.PaginatedListAsync(paginationState: request.PaginationState)
.ProjectToType<GetAllProductGallerysByFilterResponseModel>().ToListAsync(cancellationToken)
};
}
}
@@ -0,0 +1,14 @@
namespace CMSMicroservice.Application.ProductGalleriesCQ.Queries.GetAllProductGalleriesByFilter;
public class GetAllProductGallerysByFilterQueryValidator : AbstractValidator<GetAllProductGalleriesByFilterQuery>
{
public GetAllProductGallerysByFilterQueryValidator()
{
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(ValidationContext<GetAllProductGalleriesByFilterQuery>.CreateWithOptions((GetAllProductGalleriesByFilterQuery)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.ProductGalleriesCQ.Queries.GetAllProductGalleriesByFilter;
public class GetAllProductGallerysByFilterResponseDto
{
//متادیتا
public MetaData MetaData { get; set; }
//مدل خروجی
public List<GetAllProductGallerysByFilterResponseModel>? Models { get; set; }
}public class GetAllProductGallerysByFilterResponseModel
{
//
public long Id { get; set; }
//
public long ProductImageId { get; set; }
//
public long ProductId { get; set; }
}
@@ -0,0 +1,7 @@
namespace CMSMicroservice.Application.ProductGalleriesCQ.Queries.GetProductGalleries;
public record GetProductGalleriesQuery : IRequest<GetProductGallerysResponseDto>
{
//
public long Id { get; init; }
}
@@ -0,0 +1,22 @@
namespace CMSMicroservice.Application.ProductGalleriesCQ.Queries.GetProductGalleries;
public class GetProductGallerysQueryHandler : IRequestHandler<GetProductGalleriesQuery, GetProductGallerysResponseDto>
{
private readonly IApplicationDbContext _context;
public GetProductGallerysQueryHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<GetProductGallerysResponseDto> Handle(GetProductGalleriesQuery request,
CancellationToken cancellationToken)
{
var response = await _context.ProductGalleries
.AsNoTracking()
.Where(x => x.Id == request.Id)
.ProjectToType<GetProductGallerysResponseDto>()
.FirstOrDefaultAsync(cancellationToken);
return response ?? throw new NotFoundException(nameof(ProductGallery), request.Id);
}
}
@@ -0,0 +1,16 @@
namespace CMSMicroservice.Application.ProductGalleriesCQ.Queries.GetProductGalleries;
public class GetProductGallerysQueryValidator : AbstractValidator<GetProductGalleriesQuery>
{
public GetProductGallerysQueryValidator()
{
RuleFor(model => model.Id)
.NotNull();
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(ValidationContext<GetProductGalleriesQuery>.CreateWithOptions((GetProductGalleriesQuery)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.ProductGalleriesCQ.Queries.GetProductGalleries;
public class GetProductGallerysResponseDto
{
//
public long Id { get; set; }
//
public long ProductImageId { get; set; }
//
public long ProductId { get; set; }
}