Generator Changes at 10/13/2025 8:08:52 AM
This commit is contained in:
+7
@@ -0,0 +1,7 @@
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.Commands.SetAddressAsDefault;
|
||||
public record SetAddressAsDefaultCommand : IRequest<Unit>
|
||||
{
|
||||
//شناسه
|
||||
public long Id { get; init; }
|
||||
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.Commands.SetAddressAsDefault;
|
||||
public class SetAddressAsDefaultCommandHandler : IRequestHandler<SetAddressAsDefaultCommand, Unit>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public SetAddressAsDefaultCommandHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<Unit> Handle(SetAddressAsDefaultCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.UserAddresss
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(UserAddress), request.Id);
|
||||
|
||||
var entities = await _context.UserAddresss
|
||||
.Where(x => x.UserId == entity.UserId)
|
||||
.ToListAsync(cancellationToken);
|
||||
entities.ForEach(x => x.IsDefault = false);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
entity.IsDefault = true;
|
||||
_context.UserAddresss.Update(entity);
|
||||
entity.AddDomainEvent(new SetAddressAsDefaultEvent(entity));
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return Unit.Value;
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.Commands.SetAddressAsDefault;
|
||||
public class SetAddressAsDefaultCommandValidator : AbstractValidator<SetAddressAsDefaultCommand>
|
||||
{
|
||||
public SetAddressAsDefaultCommandValidator()
|
||||
{
|
||||
RuleFor(model => model.Id)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<SetAddressAsDefaultCommand>.CreateWithOptions((SetAddressAsDefaultCommand)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
using CMSMicroservice.Domain.Events;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace CMSMicroservice.Application.UserAddressCQ.EventHandlers;
|
||||
|
||||
public class SetAddressAsDefaultEventHandler : INotificationHandler<SetAddressAsDefaultEvent>
|
||||
{
|
||||
private readonly ILogger<SetAddressAsDefaultEventHandler> _logger;
|
||||
|
||||
public SetAddressAsDefaultEventHandler(ILogger<SetAddressAsDefaultEventHandler> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Handle(SetAddressAsDefaultEvent notification, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user