public async Task <CommandResult> Handle(InserirUsuarioCommand request, CancellationToken cancellationToken) { if (await _query.Any(u => u.Nome == request.Nome)) { return(CommandResultFactory.DuplicatedResult <Usuario>()); } var usuario = (Usuario)request; await _repository.InsertAsync(usuario); await _uow.CommitAsync(); var result = (UsuarioResult)usuario; return(CommandResultFactory.InsertSucessResult <Usuario>(result)); }
public async Task <CommandResult> Handle(AlterarUsuarioCommand request, CancellationToken cancellationToken) { var usuario = await _query.FindById(request.Id); if (usuario == null) { return(CommandResultFactory.NotFoundResult <Usuario>()); } if (await _query.Any(u => u.Id != request.Id && u.Nome == request.Nome)) { return(CommandResultFactory.DuplicatedResult <Usuario>()); } usuario.Alterar(request); await _repository.UpdateAsync(usuario); await _uow.CommitAsync(); var result = (UsuarioResult)usuario; return(CommandResultFactory.UpdateSucessResult <Usuario>(result)); }