23 lines
855 B
C#
23 lines
855 B
C#
using CMSMicroservice.Domain.Events;
|
|
namespace CMSMicroservice.Application.UserCQ.Commands.UpdateUser;
|
|
public class UpdateUserCommandHandler : IRequestHandler<UpdateUserCommand, Unit>
|
|
{
|
|
private readonly IApplicationDbContext _context;
|
|
|
|
public UpdateUserCommandHandler(IApplicationDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public async Task<Unit> Handle(UpdateUserCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var entity = await _context.Users
|
|
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(User), request.Id);
|
|
request.Adapt(entity);
|
|
_context.Users.Update(entity);
|
|
entity.AddDomainEvent(new UpdateUserEvent(entity));
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
return Unit.Value;
|
|
}
|
|
}
|