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(); }