public async Task <IResult <bool> > UpdateCustomer(Guid id, UpdateCustomerCommand updateCommand) { if (updateCommand == null) { return(await Result <bool> .FailAsync("customer empty")); } var validator = updateCommand.Validate(); if (!validator.IsValid) { return(await Result <bool> .FailValidationAsync(validator.Errors)); } var customer = await _service.GetCustomer(id); if (customer == null) { return(await Result <bool> .FailAsync("customer not found")); } var updated = await _service.UpdateCustomer(customer, updateCommand); if (!updated) { return(await Result <bool> .FailAsync("unable to update customer")); } return(await Result <bool> .SuccessAsync(updated)); }
public CommandResult Handle(UpdateCustomerCommand command) { var result = command.Validate(); var customerInDb = _customerRepository.GetById(command.Id); if (customerInDb == null) { result.Messages.Add("Customer not found with the given Id"); } if (result.IsValid) { var customer = new Customer(command.Id, command.Name, command.Email, command.Phone); _customerRepository.Update(customer); result.Value = customer; } return(result); }
public ICommandResult Handler(UpdateCustomerCommand command) { command.Validate(); if (!command.IsValid) { return(new GenericCommandResult(false, "Ops! Operação não pode ser realizada. Cheque as informações retornadas.", command.Notifications)); } if (_repository.Search(x => x.CNPJ == command.CNPJ && x.Id != command.Id).Any()) { return(new GenericCommandResult(false, "CNPJ pertence a outro cliente", null)); } var customer = _repository.GetById(command.Id); if (customer == null) { return(new GenericCommandResult(false, "Cliente não encontrado", null)); } if (customer.Active == true && command.Active == false) { customer.MarkInactive(); } if (!customer.IsValid) { return(new GenericCommandResult(false, "Ops! Operação não pode ser realizada. Cheque as informações retornadas.", customer.Notifications)); } if (customer.Active == false && command.Active == true) { customer.MarkActive(); } _repository.Update(customer); return(new GenericCommandResult(true, "Cliente atualizado com sucesso", customer)); }