Generator Changes at 9/27/2025 8:46:36 AM
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
namespace CMSMicroservice.Application.RoleCQ.Commands.CreateNewRole;
|
||||
public record CreateNewRoleCommand : IRequest<CreateNewRoleResponseDto>
|
||||
{
|
||||
//نام لاتین
|
||||
public string Name { get; init; }
|
||||
//عنوان
|
||||
public string Title { get; init; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
namespace CMSMicroservice.Application.RoleCQ.Commands.CreateNewRole;
|
||||
public class CreateNewRoleCommandHandler : IRequestHandler<CreateNewRoleCommand, CreateNewRoleResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public CreateNewRoleCommandHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<CreateNewRoleResponseDto> Handle(CreateNewRoleCommand request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = request.Adapt<Role>();
|
||||
await _context.Roles.AddAsync(entity, cancellationToken);
|
||||
entity.AddDomainEvent(new CreateNewRoleEvent(entity));
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return entity.Adapt<CreateNewRoleResponseDto>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
namespace CMSMicroservice.Application.RoleCQ.Commands.CreateNewRole;
|
||||
public class CreateNewRoleCommandValidator : AbstractValidator<CreateNewRoleCommand>
|
||||
{
|
||||
public CreateNewRoleCommandValidator()
|
||||
{
|
||||
RuleFor(model => model.Name)
|
||||
.NotEmpty();
|
||||
RuleFor(model => model.Title)
|
||||
.NotEmpty();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<CreateNewRoleCommand>.CreateWithOptions((CreateNewRoleCommand)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.RoleCQ.Commands.CreateNewRole;
|
||||
public class CreateNewRoleResponseDto
|
||||
{
|
||||
//شناسه
|
||||
public long Id { get; set; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace CMSMicroservice.Application.RoleCQ.Commands.DeleteRole;
|
||||
public record DeleteRoleCommand : IRequest<Unit>
|
||||
{
|
||||
//شناسه
|
||||
public long Id { get; init; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
namespace CMSMicroservice.Application.RoleCQ.Commands.DeleteRole;
|
||||
public class DeleteRoleCommandHandler : IRequestHandler<DeleteRoleCommand, Unit>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public DeleteRoleCommandHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<Unit> Handle(DeleteRoleCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Roles
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(Role), request.Id);
|
||||
entity.IsDeleted = true;
|
||||
_context.Roles.Update(entity);
|
||||
entity.AddDomainEvent(new DeleteRoleEvent(entity));
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return Unit.Value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace CMSMicroservice.Application.RoleCQ.Commands.DeleteRole;
|
||||
public class DeleteRoleCommandValidator : AbstractValidator<DeleteRoleCommand>
|
||||
{
|
||||
public DeleteRoleCommandValidator()
|
||||
{
|
||||
RuleFor(model => model.Id)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<DeleteRoleCommand>.CreateWithOptions((DeleteRoleCommand)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.RoleCQ.Commands.UpdateRole;
|
||||
public record UpdateRoleCommand : IRequest<Unit>
|
||||
{
|
||||
//شناسه
|
||||
public long Id { get; init; }
|
||||
//نام لاتین
|
||||
public string Name { get; init; }
|
||||
//عنوان
|
||||
public string Title { get; init; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
namespace CMSMicroservice.Application.RoleCQ.Commands.UpdateRole;
|
||||
public class UpdateRoleCommandHandler : IRequestHandler<UpdateRoleCommand, Unit>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public UpdateRoleCommandHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<Unit> Handle(UpdateRoleCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Roles
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(Role), request.Id);
|
||||
request.Adapt(entity);
|
||||
_context.Roles.Update(entity);
|
||||
entity.AddDomainEvent(new UpdateRoleEvent(entity));
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return Unit.Value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
namespace CMSMicroservice.Application.RoleCQ.Commands.UpdateRole;
|
||||
public class UpdateRoleCommandValidator : AbstractValidator<UpdateRoleCommand>
|
||||
{
|
||||
public UpdateRoleCommandValidator()
|
||||
{
|
||||
RuleFor(model => model.Id)
|
||||
.NotNull();
|
||||
RuleFor(model => model.Name)
|
||||
.NotEmpty();
|
||||
RuleFor(model => model.Title)
|
||||
.NotEmpty();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<UpdateRoleCommand>.CreateWithOptions((UpdateRoleCommand)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.RoleCQ.EventHandlers;
|
||||
|
||||
public class CreateNewRoleEventHandler : INotificationHandler<CreateNewRoleEvent>
|
||||
{
|
||||
private readonly ILogger<CreateNewRoleEventHandler> _logger;
|
||||
|
||||
public CreateNewRoleEventHandler(ILogger<CreateNewRoleEventHandler> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Handle(CreateNewRoleEvent 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.RoleCQ.EventHandlers;
|
||||
|
||||
public class DeleteRoleEventHandler : INotificationHandler<DeleteRoleEvent>
|
||||
{
|
||||
private readonly ILogger<DeleteRoleEventHandler> _logger;
|
||||
|
||||
public DeleteRoleEventHandler(ILogger<DeleteRoleEventHandler> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Handle(DeleteRoleEvent 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.RoleCQ.EventHandlers;
|
||||
|
||||
public class UpdateRoleEventHandler : INotificationHandler<UpdateRoleEvent>
|
||||
{
|
||||
private readonly ILogger<UpdateRoleEventHandler> _logger;
|
||||
|
||||
public UpdateRoleEventHandler(ILogger<UpdateRoleEventHandler> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Handle(UpdateRoleEvent notification, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
namespace CMSMicroservice.Application.RoleCQ.Queries.GetAllRoleByFilter;
|
||||
public record GetAllRoleByFilterQuery : IRequest<GetAllRoleByFilterResponseDto>
|
||||
{
|
||||
//موقعیت صفحه بندی
|
||||
public PaginationState? PaginationState { get; init; }
|
||||
//مرتب سازی بر اساس
|
||||
public string? SortBy { get; init; }
|
||||
//فیلتر
|
||||
public GetAllRoleByFilterFilter? Filter { get; init; }
|
||||
|
||||
}public class GetAllRoleByFilterFilter
|
||||
{
|
||||
//شناسه
|
||||
public long? Id { get; set; }
|
||||
//نام لاتین
|
||||
public string? Name { get; set; }
|
||||
//عنوان
|
||||
public string? Title { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
namespace CMSMicroservice.Application.RoleCQ.Queries.GetAllRoleByFilter;
|
||||
public class GetAllRoleByFilterQueryHandler : IRequestHandler<GetAllRoleByFilterQuery, GetAllRoleByFilterResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public GetAllRoleByFilterQueryHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetAllRoleByFilterResponseDto> Handle(GetAllRoleByFilterQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _context.Roles
|
||||
.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.Name == null || x.Name.Contains(request.Filter.Name))
|
||||
.Where(x => request.Filter.Title == null || x.Title.Contains(request.Filter.Title))
|
||||
;
|
||||
}
|
||||
return new GetAllRoleByFilterResponseDto
|
||||
{
|
||||
MetaData = await query.GetMetaData(request.PaginationState, cancellationToken),
|
||||
Models = await query.PaginatedListAsync(paginationState: request.PaginationState)
|
||||
.ProjectToType<GetAllRoleByFilterResponseModel>().ToListAsync(cancellationToken)
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace CMSMicroservice.Application.RoleCQ.Queries.GetAllRoleByFilter;
|
||||
public class GetAllRoleByFilterQueryValidator : AbstractValidator<GetAllRoleByFilterQuery>
|
||||
{
|
||||
public GetAllRoleByFilterQueryValidator()
|
||||
{
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<GetAllRoleByFilterQuery>.CreateWithOptions((GetAllRoleByFilterQuery)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.RoleCQ.Queries.GetAllRoleByFilter;
|
||||
public class GetAllRoleByFilterResponseDto
|
||||
{
|
||||
//متادیتا
|
||||
public MetaData MetaData { get; set; }
|
||||
//مدل خروجی
|
||||
public List<GetAllRoleByFilterResponseModel>? Models { get; set; }
|
||||
|
||||
}public class GetAllRoleByFilterResponseModel
|
||||
{
|
||||
//شناسه
|
||||
public long Id { get; set; }
|
||||
//نام لاتین
|
||||
public string Name { get; set; }
|
||||
//عنوان
|
||||
public string Title { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace CMSMicroservice.Application.RoleCQ.Queries.GetRole;
|
||||
public record GetRoleQuery : IRequest<GetRoleResponseDto>
|
||||
{
|
||||
//شناسه
|
||||
public long Id { get; init; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
namespace CMSMicroservice.Application.RoleCQ.Queries.GetRole;
|
||||
public class GetRoleQueryHandler : IRequestHandler<GetRoleQuery, GetRoleResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public GetRoleQueryHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetRoleResponseDto> Handle(GetRoleQuery request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var response = await _context.Roles
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.ProjectToType<GetRoleResponseDto>()
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
|
||||
return response ?? throw new NotFoundException(nameof(Role), request.Id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace CMSMicroservice.Application.RoleCQ.Queries.GetRole;
|
||||
public class GetRoleQueryValidator : AbstractValidator<GetRoleQuery>
|
||||
{
|
||||
public GetRoleQueryValidator()
|
||||
{
|
||||
RuleFor(model => model.Id)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<GetRoleQuery>.CreateWithOptions((GetRoleQuery)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.RoleCQ.Queries.GetRole;
|
||||
public class GetRoleResponseDto
|
||||
{
|
||||
//شناسه
|
||||
public long Id { get; set; }
|
||||
//نام لاتین
|
||||
public string Name { get; set; }
|
||||
//عنوان
|
||||
public string Title { get; set; }
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user