示例#1
0
        public async Task <ActionResult <Dependent> > CreateDependent(Dependent dependent)
        {
            if (await _dependentRepository.DependentExists(dependent))
            {
                return(BadRequest("A Dependent with the name '" + dependent.FirstName + ' ' + dependent.LastName + "' already exists for this employee"));
            }

            dependent.Birthdate = ParseDates.ParseDate(dependent.Birthdate);
            _dependentRepository.Add(dependent);

            if (await _dependentRepository.SaveAllAsync())
            {
                return(NoContent());
            }

            return(BadRequest("Failed to add dependent"));
        }
        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();
        }