Example #1
0
        public List <EmployeeViewModel> SearchBySkill(List <long> skills)
        {
            var skillList = _skillRepository.GetAllByCondition(x => skills.Contains(x.Id)).ToList();

            return(_employeeSkillRepository.GetAllByCondition(x => skillList.Contains(x.Skill))
                   .Select(x => x.Employee).ProjectTo <EmployeeViewModel>().ToList());
            //return _repository.GetAllByCondition(x => x.Skills.)
        }
        public EmployeeViewModel Update(EmployeeViewModel employeeViewModel)
        {
            var model = AutoMapper.Mapper.Map <EmployeeViewModel, Employee>(employeeViewModel);

            var employee = _repository
                           .FindById(model.Id);

            if (employee == null)
            {
                return(null);
            }


            if (model.Nationality != null)
            {
                model.Nationality = _countryRepository.FindById(model.Nationality.Id);
            }
            if (model.EthnicGroup != null)
            {
                model.EthnicGroup = _ethnicGroupRepository.FindById(model.EthnicGroup.Id);
            }
            if (model.Religion != null)
            {
                model.Religion = _religionRepository.FindById(model.Religion.Id);
            }
            if (model.Education != null)
            {
                model.Education = _educationRepository.FindById(model.Education.Id);
            }

            // Temporal
            model.UserId = 1;

            var skills = new List <Skill>();

            if (employeeViewModel.SelectedSkills != null && employeeViewModel.SelectedSkills.Any())
            {
                var skillIds = employeeViewModel.SelectedSkills != null?
                               employeeViewModel.SelectedSkills.ToList() :
                                   new List <long>();

                // new skills
                skills =
                    _skillRepository
                    .GetAllByCondition(x => skillIds.Contains(x.Id)).ToList();

                // delete old skills
                var oldSkills = _employeeSkillRepository.GetAllByCondition(x => x.Employee.Id == model.Id).ToList();
                foreach (var oldSkill in oldSkills)
                {
                    _employeeSkillRepository.Delete(oldSkill);
                }
            }

            using (var scope = new TransactionScope())
            {
                try
                {
                    _repository.Update(model);
                    Save();
                    foreach (var skill in skills)
                    {
                        _employeeSkillRepository.Add(new EmployeeSkill()
                        {
                            Skill    = skill,
                            Employee = employee
                        });
                    }

                    Save();

                    scope.Complete();
                }
                catch (TransactionException ex)
                {
                    scope.Dispose();
                }
            }

            return(employeeViewModel);
        }