public async Task UpdateAsync(int id, CreateUpdateCustomerCommand createUpdateCustomerCommand)
        {
            if (!createUpdateCustomerCommand.IsValid())
            {
                _notificationService.AddValidation("Campos obrigatórios devem ser preenchidos.");
                return;
            }

            var customer = await _context.Set <Customer>().FirstOrDefaultAsync(p => p.Id == id && !p.IsRemoved);

            if (customer == null)
            {
                _notificationService.AddValidation("Cliente não encontrado.");
                return;
            }

            var categoryExists = await _context.Set <Category>().AnyAsync(p => p.Id == createUpdateCustomerCommand.CategoryId);

            if (!categoryExists)
            {
                _notificationService.AddValidation("Categoria não encontrada.");
                return;
            }

            customer.DocumentNumber = createUpdateCustomerCommand.DocumentNumber;
            customer.Name           = createUpdateCustomerCommand.Name;
            customer.CategoryId     = createUpdateCustomerCommand.CategoryId;

            await _context.SaveChangesAsync();
        }
        public async Task CreateAsync(CreateUpdateCustomerCommand createUpdateCustomerCommand)
        {
            if (!createUpdateCustomerCommand.IsValid())
            {
                _notificationService.AddValidation("Campos obrigatórios devem ser preenchidos.");
                return;
            }

            var categoryExists = await _context.Set <Category>().AnyAsync(p => p.Id == createUpdateCustomerCommand.CategoryId);

            if (!categoryExists)
            {
                _notificationService.AddValidation("Categoria não encontrada.");
                return;
            }

            var customer = new Customer
            {
                Name           = createUpdateCustomerCommand.Name,
                DocumentNumber = createUpdateCustomerCommand.DocumentNumber,
                CategoryId     = createUpdateCustomerCommand.CategoryId,
                IsRemoved      = false,
                Status         = CustomerStatusType.WaitingForApproval,
                CreationDate   = DateTime.Now
            };

            _context.Add(customer);
            await _context.SaveChangesAsync();
        }
        public async Task <IActionResult> PutAsync(int customerId, CreateUpdateCustomerCommand createUpdateCustomerCommand)
        {
            await _service.UpdateAsync(customerId, createUpdateCustomerCommand);

            return(Ok());
        }
        public async Task <IActionResult> PostAsync(CreateUpdateCustomerCommand createUpdateCustomerCommand)
        {
            await _service.CreateAsync(createUpdateCustomerCommand);

            return(Ok());
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Save([FromBody] CreateUpdateCustomerCommand command)
        {
            var result = await _mediator.Send(command);

            return(Ok(result));
        }