Пример #1
0
        //public async Task<IEnumerable<Employee>> GetEmployeesAsync(Guid companyId,string genderDisplay,string q)
        //{
        //    if (companyId == Guid.Empty)
        //    {
        //        throw new ArgumentNullException(nameof(companyId));
        //    }

        //    if(string.IsNullOrWhiteSpace(genderDisplay) && string.IsNullOrWhiteSpace(q))
        //    {
        //        return await _myContext.Employees
        //        .Where(x => x.CompanyId == companyId)
        //        .OrderBy(x => x.EmployeeNo)
        //        .ToListAsync();
        //    }

        //    var items = _myContext.Employees.Where(x => x.CompanyId == companyId);

        //    if (!string.IsNullOrWhiteSpace(genderDisplay))
        //    {
        //        genderDisplay = genderDisplay.Trim();
        //        var gender = Enum.Parse<Gender>(genderDisplay);

        //        items = items.Where(x=>x.Gender == gender);
        //    }

        //    if (!string.IsNullOrWhiteSpace(q))
        //    {
        //        q = q.Trim();

        //        items = items.Where(x => x.EmployeeNo.Contains(q) ||
        //          x.FirstName.Contains(q) ||
        //          x.LastName.Contains(q));
        //    }

        //    return await items.OrderBy(x => x.EmployeeNo)
        //        .ToListAsync();
        //}

        public async Task <IEnumerable <Employee> > GetEmployeesAsync(Guid companyId, EmployeeResourcePatameter patameters)
        {
            if (companyId == Guid.Empty)
            {
                throw new ArgumentNullException(nameof(companyId));
            }

            //if (string.IsNullOrWhiteSpace(patameters.Gender) && string.IsNullOrWhiteSpace(patameters.Q))
            //{
            //    return await _myContext.Employees
            //    .Where(x => x.CompanyId == companyId)
            //    .OrderBy(x => x.EmployeeNo)
            //    .ToListAsync();
            //}

            var items = _myContext.Employees.Where(x => x.CompanyId == companyId);

            if (!string.IsNullOrWhiteSpace(patameters.Gender))
            {
                patameters.Gender = patameters.Gender.Trim();
                var gender = Enum.Parse <Gender>(patameters.Gender);

                items = items.Where(x => x.Gender == gender);
            }

            if (!string.IsNullOrWhiteSpace(patameters.Q))
            {
                patameters.Q = patameters.Q.Trim();

                items = items.Where(x => x.EmployeeNo.Contains(patameters.Q) ||
                                    x.FirstName.Contains(patameters.Q) ||
                                    x.LastName.Contains(patameters.Q));
            }

            //if (!string.IsNullOrWhiteSpace(patameters.OrderBy))
            //{
            //    if (patameters.OrderBy.ToLowerInvariant()=="name")
            //    {
            //        items = items.OrderBy(x => x.FirstName).ThenBy(x => x.LastName);
            //    }
            //}

            var mappingDictionary = propertyMappingService.GetPropertyMapping <EmployeeResource, Employee>();

            items.ApplySort(patameters.OrderBy, mappingDictionary);

            return(await items.ToListAsync());
        }
Пример #2
0
        public async Task <ActionResult <IEnumerable <EmployeeResource> > > GetEmployeesForCompany(Guid companyId, [FromQuery]  EmployeeResourcePatameter patameters)
        {
            if (!await companyRepository.CompanyExistsAsync(companyId))
            {
                return(NotFound());
            }

            var employees = await employeeRepository.GetEmployeesAsync(companyId, patameters);

            var employeeResources = mapper.Map <IEnumerable <EmployeeResource> >(employees);

            return(Ok(employeeResources));
        }