Exemple #1
0
        public async Task <long> AddCompany(AddCompanyCommand command)
        {
            var company = new Company()
            {
                Name = command.Name,
                EstablishmentYear = command.EstablishmentYear,
                Employees         = command.Employees.Select(e =>
                {
                    var employee = new Employee()
                    {
                        DateOfBirth = e.DateOfBirth,
                        FirstName   = e.FirstName,
                        LastName    = e.LastName,
                        JobTitle    = JobTitleHelper.GetEnumFromString(e.JobTitle)
                    };
                    return(employee);
                }).ToList()
            };

            return(await _companiesRepository.AddCompany(company));
        }
        public async Task <IEnumerable <Company> > Search(SearchCompanyQuery query)
        {
            if (query.EmployeeJobTitles == null) //to avoid null in JobTitlesList
            {
                query.EmployeeJobTitles = new List <string>();
            }

            var companies = await _context.Companies.Include(c => c.Employees).ToListAsync();

            if (!string.IsNullOrWhiteSpace(query.Keyword) && query.EmployeeDateOfBirthFrom != null && query.EmployeeDateOfBirthTo != null)
            {
                var result = companies.Where(c => c.Name.Contains(query.Keyword) ||
                                             c.Employees.Any(e => e.FirstName.Contains(query.Keyword) ||
                                                             e.LastName.Contains(query.Keyword)) ||
                                             c.Employees.Any(k => (k.DateOfBirth > query.EmployeeDateOfBirthFrom && k.DateOfBirth < query.EmployeeDateOfBirthTo)) ||
                                             c.Employees.Any(e => query.EmployeeJobTitles.Any(s => JobTitleHelper.GetEnumFromString(s) == e.JobTitle))).ToList();
                return(result);
            }
            else if (string.IsNullOrWhiteSpace(query.Keyword) && (query.EmployeeDateOfBirthFrom != null && query.EmployeeDateOfBirthTo != null))
            {
                var result = companies.Where(c =>
                                             c.Employees.Any(k => (k.DateOfBirth > query.EmployeeDateOfBirthFrom && k.DateOfBirth < query.EmployeeDateOfBirthTo)) ||
                                             c.Employees.Any(e => query.EmployeeJobTitles.Any(s => JobTitleHelper.GetEnumFromString(s) == e.JobTitle))).ToList();
                return(result);
            }
            else if (!string.IsNullOrWhiteSpace(query.Keyword) && (query.EmployeeDateOfBirthFrom == null || query.EmployeeDateOfBirthTo == null))
            {
                var result = companies.Where(c => c.Name.Contains(query.Keyword) ||
                                             c.Employees.Any(e => e.FirstName.Contains(query.Keyword) ||
                                                             e.LastName.Contains(query.Keyword)) ||
                                             c.Employees.Any(e => query.EmployeeJobTitles.Any(s => JobTitleHelper.GetEnumFromString(s) == e.JobTitle))).ToList();
                return(result);
            }
            else if (string.IsNullOrWhiteSpace(query.Keyword) && (query.EmployeeDateOfBirthFrom == null || query.EmployeeDateOfBirthTo == null))
            {
                var result = companies.Where(c => c.Employees.Any(e => query.EmployeeJobTitles.Any(s => JobTitleHelper.GetEnumFromString(s) == e.JobTitle))).ToList();
                return(result);
            }

            return(new List <Company>());
        }