Generator Changes at 9/27/2025 8:46:36 AM
This commit is contained in:
+9
@@ -0,0 +1,9 @@
|
||||
namespace CMSMicroservice.Application.UserRoleCQ.Commands.CreateNewUserRole;
|
||||
public record CreateNewUserRoleCommand : IRequest<CreateNewUserRoleResponseDto>
|
||||
{
|
||||
//شناسه نقش
|
||||
public long RoleId { get; init; }
|
||||
//شناسه کاربر
|
||||
public long UserId { get; init; }
|
||||
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
namespace CMSMicroservice.Application.UserRoleCQ.Commands.CreateNewUserRole;
|
||||
public class CreateNewUserRoleCommandHandler : IRequestHandler<CreateNewUserRoleCommand, CreateNewUserRoleResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public CreateNewUserRoleCommandHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<CreateNewUserRoleResponseDto> Handle(CreateNewUserRoleCommand request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = request.Adapt<UserRole>();
|
||||
await _context.UserRoles.AddAsync(entity, cancellationToken);
|
||||
entity.AddDomainEvent(new CreateNewUserRoleEvent(entity));
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return entity.Adapt<CreateNewUserRoleResponseDto>();
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
namespace CMSMicroservice.Application.UserRoleCQ.Commands.CreateNewUserRole;
|
||||
public class CreateNewUserRoleCommandValidator : AbstractValidator<CreateNewUserRoleCommand>
|
||||
{
|
||||
public CreateNewUserRoleCommandValidator()
|
||||
{
|
||||
RuleFor(model => model.RoleId)
|
||||
.NotNull();
|
||||
RuleFor(model => model.UserId)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<CreateNewUserRoleCommand>.CreateWithOptions((CreateNewUserRoleCommand)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.UserRoleCQ.Commands.CreateNewUserRole;
|
||||
public class CreateNewUserRoleResponseDto
|
||||
{
|
||||
//شناسه
|
||||
public long Id { get; set; }
|
||||
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
namespace CMSMicroservice.Application.UserRoleCQ.Commands.DeleteUserRole;
|
||||
public record DeleteUserRoleCommand : IRequest<Unit>
|
||||
{
|
||||
//شناسه
|
||||
public long Id { get; init; }
|
||||
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
namespace CMSMicroservice.Application.UserRoleCQ.Commands.DeleteUserRole;
|
||||
public class DeleteUserRoleCommandHandler : IRequestHandler<DeleteUserRoleCommand, Unit>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public DeleteUserRoleCommandHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<Unit> Handle(DeleteUserRoleCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.UserRoles
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(UserRole), request.Id);
|
||||
entity.IsDeleted = true;
|
||||
_context.UserRoles.Update(entity);
|
||||
entity.AddDomainEvent(new DeleteUserRoleEvent(entity));
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return Unit.Value;
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
namespace CMSMicroservice.Application.UserRoleCQ.Commands.DeleteUserRole;
|
||||
public class DeleteUserRoleCommandValidator : AbstractValidator<DeleteUserRoleCommand>
|
||||
{
|
||||
public DeleteUserRoleCommandValidator()
|
||||
{
|
||||
RuleFor(model => model.Id)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<DeleteUserRoleCommand>.CreateWithOptions((DeleteUserRoleCommand)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.UserRoleCQ.Commands.UpdateUserRole;
|
||||
public record UpdateUserRoleCommand : IRequest<Unit>
|
||||
{
|
||||
//شناسه
|
||||
public long Id { get; init; }
|
||||
//شناسه نقش
|
||||
public long RoleId { get; init; }
|
||||
//شناسه کاربر
|
||||
public long UserId { get; init; }
|
||||
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
namespace CMSMicroservice.Application.UserRoleCQ.Commands.UpdateUserRole;
|
||||
public class UpdateUserRoleCommandHandler : IRequestHandler<UpdateUserRoleCommand, Unit>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public UpdateUserRoleCommandHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<Unit> Handle(UpdateUserRoleCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.UserRoles
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(UserRole), request.Id);
|
||||
request.Adapt(entity);
|
||||
_context.UserRoles.Update(entity);
|
||||
entity.AddDomainEvent(new UpdateUserRoleEvent(entity));
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return Unit.Value;
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
namespace CMSMicroservice.Application.UserRoleCQ.Commands.UpdateUserRole;
|
||||
public class UpdateUserRoleCommandValidator : AbstractValidator<UpdateUserRoleCommand>
|
||||
{
|
||||
public UpdateUserRoleCommandValidator()
|
||||
{
|
||||
RuleFor(model => model.Id)
|
||||
.NotNull();
|
||||
RuleFor(model => model.RoleId)
|
||||
.NotNull();
|
||||
RuleFor(model => model.UserId)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<UpdateUserRoleCommand>.CreateWithOptions((UpdateUserRoleCommand)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