Generator Changes at 9/27/2025 8:46:36 AM
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
namespace CMSMicroservice.Application.PackageCQ.Commands.CreateNewPackage;
|
||||
public record CreateNewPackageCommand : IRequest<CreateNewPackageResponseDto>
|
||||
{
|
||||
//عنوان
|
||||
public string Title { get; init; }
|
||||
//توضیحات
|
||||
public string Description { get; init; }
|
||||
//آدرس تصویر
|
||||
public string ImagePath { get; init; }
|
||||
//قیمت
|
||||
public long Price { get; init; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
namespace CMSMicroservice.Application.PackageCQ.Commands.CreateNewPackage;
|
||||
public class CreateNewPackageCommandHandler : IRequestHandler<CreateNewPackageCommand, CreateNewPackageResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public CreateNewPackageCommandHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<CreateNewPackageResponseDto> Handle(CreateNewPackageCommand request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = request.Adapt<Package>();
|
||||
await _context.Packages.AddAsync(entity, cancellationToken);
|
||||
entity.AddDomainEvent(new CreateNewPackageEvent(entity));
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return entity.Adapt<CreateNewPackageResponseDto>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
namespace CMSMicroservice.Application.PackageCQ.Commands.CreateNewPackage;
|
||||
public class CreateNewPackageCommandValidator : AbstractValidator<CreateNewPackageCommand>
|
||||
{
|
||||
public CreateNewPackageCommandValidator()
|
||||
{
|
||||
RuleFor(model => model.Title)
|
||||
.NotEmpty();
|
||||
RuleFor(model => model.Description)
|
||||
.NotEmpty();
|
||||
RuleFor(model => model.ImagePath)
|
||||
.NotEmpty();
|
||||
RuleFor(model => model.Price)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<CreateNewPackageCommand>.CreateWithOptions((CreateNewPackageCommand)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.PackageCQ.Commands.CreateNewPackage;
|
||||
public class CreateNewPackageResponseDto
|
||||
{
|
||||
//شناسه
|
||||
public long Id { get; set; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace CMSMicroservice.Application.PackageCQ.Commands.DeletePackage;
|
||||
public record DeletePackageCommand : IRequest<Unit>
|
||||
{
|
||||
//شناسه
|
||||
public long Id { get; init; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
namespace CMSMicroservice.Application.PackageCQ.Commands.DeletePackage;
|
||||
public class DeletePackageCommandHandler : IRequestHandler<DeletePackageCommand, Unit>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public DeletePackageCommandHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<Unit> Handle(DeletePackageCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Packages
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(Package), request.Id);
|
||||
entity.IsDeleted = true;
|
||||
_context.Packages.Update(entity);
|
||||
entity.AddDomainEvent(new DeletePackageEvent(entity));
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return Unit.Value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace CMSMicroservice.Application.PackageCQ.Commands.DeletePackage;
|
||||
public class DeletePackageCommandValidator : AbstractValidator<DeletePackageCommand>
|
||||
{
|
||||
public DeletePackageCommandValidator()
|
||||
{
|
||||
RuleFor(model => model.Id)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<DeletePackageCommand>.CreateWithOptions((DeletePackageCommand)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
namespace CMSMicroservice.Application.PackageCQ.Commands.UpdatePackage;
|
||||
public record UpdatePackageCommand : IRequest<Unit>
|
||||
{
|
||||
//شناسه
|
||||
public long Id { get; init; }
|
||||
//عنوان
|
||||
public string Title { get; init; }
|
||||
//توضیحات
|
||||
public string Description { get; init; }
|
||||
//آدرس تصویر
|
||||
public string ImagePath { get; init; }
|
||||
//قیمت
|
||||
public long Price { get; init; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
namespace CMSMicroservice.Application.PackageCQ.Commands.UpdatePackage;
|
||||
public class UpdatePackageCommandHandler : IRequestHandler<UpdatePackageCommand, Unit>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public UpdatePackageCommandHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<Unit> Handle(UpdatePackageCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Packages
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(Package), request.Id);
|
||||
request.Adapt(entity);
|
||||
_context.Packages.Update(entity);
|
||||
entity.AddDomainEvent(new UpdatePackageEvent(entity));
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return Unit.Value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
namespace CMSMicroservice.Application.PackageCQ.Commands.UpdatePackage;
|
||||
public class UpdatePackageCommandValidator : AbstractValidator<UpdatePackageCommand>
|
||||
{
|
||||
public UpdatePackageCommandValidator()
|
||||
{
|
||||
RuleFor(model => model.Id)
|
||||
.NotNull();
|
||||
RuleFor(model => model.Title)
|
||||
.NotEmpty();
|
||||
RuleFor(model => model.Description)
|
||||
.NotEmpty();
|
||||
RuleFor(model => model.ImagePath)
|
||||
.NotEmpty();
|
||||
RuleFor(model => model.Price)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<UpdatePackageCommand>.CreateWithOptions((UpdatePackageCommand)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace CMSMicroservice.Application.PackageCQ.EventHandlers;
|
||||
|
||||
public class CreateNewPackageEventHandler : INotificationHandler<CreateNewPackageEvent>
|
||||
{
|
||||
private readonly ILogger<CreateNewPackageEventHandler> _logger;
|
||||
|
||||
public CreateNewPackageEventHandler(ILogger<CreateNewPackageEventHandler> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Handle(CreateNewPackageEvent notification, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace CMSMicroservice.Application.PackageCQ.EventHandlers;
|
||||
|
||||
public class DeletePackageEventHandler : INotificationHandler<DeletePackageEvent>
|
||||
{
|
||||
private readonly ILogger<DeletePackageEventHandler> _logger;
|
||||
|
||||
public DeletePackageEventHandler(ILogger<DeletePackageEventHandler> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Handle(DeletePackageEvent notification, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace CMSMicroservice.Application.PackageCQ.EventHandlers;
|
||||
|
||||
public class UpdatePackageEventHandler : INotificationHandler<UpdatePackageEvent>
|
||||
{
|
||||
private readonly ILogger<UpdatePackageEventHandler> _logger;
|
||||
|
||||
public UpdatePackageEventHandler(ILogger<UpdatePackageEventHandler> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Handle(UpdatePackageEvent notification, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
namespace CMSMicroservice.Application.PackageCQ.Queries.GetAllPackageByFilter;
|
||||
public record GetAllPackageByFilterQuery : IRequest<GetAllPackageByFilterResponseDto>
|
||||
{
|
||||
//موقعیت صفحه بندی
|
||||
public PaginationState? PaginationState { get; init; }
|
||||
//مرتب سازی بر اساس
|
||||
public string? SortBy { get; init; }
|
||||
//فیلتر
|
||||
public GetAllPackageByFilterFilter? Filter { get; init; }
|
||||
|
||||
}public class GetAllPackageByFilterFilter
|
||||
{
|
||||
//شناسه
|
||||
public long? Id { get; set; }
|
||||
//عنوان
|
||||
public string? Title { get; set; }
|
||||
//توضیحات
|
||||
public string? Description { get; set; }
|
||||
//آدرس تصویر
|
||||
public string? ImagePath { get; set; }
|
||||
//قیمت
|
||||
public long? Price { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
namespace CMSMicroservice.Application.PackageCQ.Queries.GetAllPackageByFilter;
|
||||
public class GetAllPackageByFilterQueryHandler : IRequestHandler<GetAllPackageByFilterQuery, GetAllPackageByFilterResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public GetAllPackageByFilterQueryHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetAllPackageByFilterResponseDto> Handle(GetAllPackageByFilterQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _context.Packages
|
||||
.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.Title == null || x.Title.Contains(request.Filter.Title))
|
||||
.Where(x => request.Filter.Description == null || x.Description.Contains(request.Filter.Description))
|
||||
.Where(x => request.Filter.ImagePath == null || x.ImagePath.Contains(request.Filter.ImagePath))
|
||||
.Where(x => request.Filter.Price == null || x.Price == request.Filter.Price)
|
||||
;
|
||||
}
|
||||
return new GetAllPackageByFilterResponseDto
|
||||
{
|
||||
MetaData = await query.GetMetaData(request.PaginationState, cancellationToken),
|
||||
Models = await query.PaginatedListAsync(paginationState: request.PaginationState)
|
||||
.ProjectToType<GetAllPackageByFilterResponseModel>().ToListAsync(cancellationToken)
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace CMSMicroservice.Application.PackageCQ.Queries.GetAllPackageByFilter;
|
||||
public class GetAllPackageByFilterQueryValidator : AbstractValidator<GetAllPackageByFilterQuery>
|
||||
{
|
||||
public GetAllPackageByFilterQueryValidator()
|
||||
{
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<GetAllPackageByFilterQuery>.CreateWithOptions((GetAllPackageByFilterQuery)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
namespace CMSMicroservice.Application.PackageCQ.Queries.GetAllPackageByFilter;
|
||||
public class GetAllPackageByFilterResponseDto
|
||||
{
|
||||
//متادیتا
|
||||
public MetaData MetaData { get; set; }
|
||||
//مدل خروجی
|
||||
public List<GetAllPackageByFilterResponseModel>? Models { get; set; }
|
||||
|
||||
}public class GetAllPackageByFilterResponseModel
|
||||
{
|
||||
//شناسه
|
||||
public long Id { get; set; }
|
||||
//عنوان
|
||||
public string Title { get; set; }
|
||||
//توضیحات
|
||||
public string Description { get; set; }
|
||||
//آدرس تصویر
|
||||
public string ImagePath { get; set; }
|
||||
//قیمت
|
||||
public long Price { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace CMSMicroservice.Application.PackageCQ.Queries.GetPackage;
|
||||
public record GetPackageQuery : IRequest<GetPackageResponseDto>
|
||||
{
|
||||
//شناسه
|
||||
public long Id { get; init; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
namespace CMSMicroservice.Application.PackageCQ.Queries.GetPackage;
|
||||
public class GetPackageQueryHandler : IRequestHandler<GetPackageQuery, GetPackageResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public GetPackageQueryHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetPackageResponseDto> Handle(GetPackageQuery request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var response = await _context.Packages
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.ProjectToType<GetPackageResponseDto>()
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
|
||||
return response ?? throw new NotFoundException(nameof(Package), request.Id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace CMSMicroservice.Application.PackageCQ.Queries.GetPackage;
|
||||
public class GetPackageQueryValidator : AbstractValidator<GetPackageQuery>
|
||||
{
|
||||
public GetPackageQueryValidator()
|
||||
{
|
||||
RuleFor(model => model.Id)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<GetPackageQuery>.CreateWithOptions((GetPackageQuery)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
namespace CMSMicroservice.Application.PackageCQ.Queries.GetPackage;
|
||||
public class GetPackageResponseDto
|
||||
{
|
||||
//شناسه
|
||||
public long Id { get; set; }
|
||||
//عنوان
|
||||
public string Title { get; set; }
|
||||
//توضیحات
|
||||
public string Description { get; set; }
|
||||
//آدرس تصویر
|
||||
public string ImagePath { get; set; }
|
||||
//قیمت
|
||||
public long Price { get; set; }
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user