public async Task <Customer> UpdateAsync(int id, InsertUpdateCustomerCommand command) { var cnpj = new Cnpj(command.Cnpj); var customer = await _customerRepository.GetAsync(id); if (customer == null) { throw new EntityNotFoundException("Customer"); } if (customer.Cnpj != cnpj) { var existingCustomer = await _customerRepository.GetByCnpjAsync(cnpj); if (existingCustomer != null) { throw new BusinessException($"Customer already exists with CNPJ {cnpj}"); } } customer.Cnpj = cnpj; customer.CommercialName = command.CommercialName; customer.LegalName = command.LegalName; await _customerRepository.SaveChangesAsync(); return(customer); }
public async Task <Customer> InsertAsync(InsertUpdateCustomerCommand command) { var cnpj = new Cnpj(command.Cnpj); var existingCustomer = await _customerRepository.GetByCnpjAsync(cnpj); if (existingCustomer != null) { throw new BusinessException($"Customer already exists with CNPJ {cnpj}"); } var customer = new Customer { Cnpj = cnpj, CommercialName = command.CommercialName, LegalName = command.LegalName }; _customerRepository.Add(customer); await _customerRepository.SaveChangesAsync(); return(customer); }
public async Task <IActionResult> Put(int id, InsertUpdateCustomerCommand command) { await _customerService.UpdateAsync(id, command); return(NoContent()); }
public async Task <ActionResult <CreatedEntityDto> > Post(InsertUpdateCustomerCommand command) { var customer = await _customerService.InsertAsync(command); return(new CreatedEntityDto(customer.Id)); }