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:
+9
@@ -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; }
|
||||
|
||||
}
|
||||
+21
@@ -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>();
|
||||
}
|
||||
}
|
||||
+18
@@ -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);
|
||||
};
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
namespace CMSMicroservice.Application.ProductGalleriesCQ.Commands.CreateNewProductGalleries;
|
||||
public class CreateNewProductGallerysResponseDto
|
||||
{
|
||||
//
|
||||
public long Id { get; set; }
|
||||
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
namespace CMSMicroservice.Application.ProductGalleriesCQ.Commands.DeleteProductGalleries;
|
||||
public record DeleteProductGalleriesCommand : IRequest<Unit>
|
||||
{
|
||||
//
|
||||
public long Id { get; init; }
|
||||
|
||||
}
|
||||
+22
@@ -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;
|
||||
}
|
||||
}
|
||||
+16
@@ -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);
|
||||
};
|
||||
}
|
||||
+11
@@ -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; }
|
||||
|
||||
}
|
||||
+22
@@ -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;
|
||||
}
|
||||
}
|
||||
+20
@@ -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);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user