Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Put(int id, InsertUpdateCustomerCommand command)
        {
            await _customerService.UpdateAsync(id, command);

            return(NoContent());
        }
Exemplo n.º 4
0
        public async Task <ActionResult <CreatedEntityDto> > Post(InsertUpdateCustomerCommand command)
        {
            var customer = await _customerService.InsertAsync(command);

            return(new CreatedEntityDto(customer.Id));
        }