public async Task Handle(EmployeeCreateOrUpdateCommand model) { var isValid = await model.IsValid(); if (!isValid) { return; } if (!string.IsNullOrWhiteSpace(model.Email)) { if (await _employeeRepository.GetByEmail(model.Email, model.Id)) { model.Failures.Add("Já existe um Email cadastrado"); return; } } var email = new Email(model.Email); if (model.Id == null) { #region Insert var employee = new Employee(Guid.NewGuid(), model.Name, email, model.Genre, model.Birth, model.Role); foreach (var item in model.Dependent) { employee.AddDependent(new Dependent(item.Name, employee.Id)); } await _employeeRepository.Add(employee); #endregion } else { #region Update var employee = new Employee((Guid)model.Id, model.Name, email, model.Genre, model.Birth, model.Role); _employeeRepository.Update(employee); var dependents = await _dependentRepository.GetAllByEmployee((Guid)model.Id); if (dependents.Any()) { _dependentRepository.RemoveAll(dependents); } if (model.Dependent.Any()) { foreach (var item in model.Dependent) { await _dependentRepository.Add(new Dependent(item.Name, employee.Id)); } } #endregion } await _unitOfWork.Commit(); }