public JsonResult GetEmployees(EmployeeFilter filter)
        {
            Int32 pages;

            var employees = _employeeService.ByFilter(filter, out pages).ToList();

            var models = employees.Select(_mapper.Map<EmployeeModel>);

            return Json(new
            {
                pages = pages,
                employees = models
            }, JsonRequestBehavior.AllowGet);
        }
        public void GetEmployees()
        {
            var controller = this.GetController();

            var filter = new EmployeeFilter
            {
                Status = EmployeeStatusFilter.Active,
                Count = 10,
                Page = 0,
                SortDirection = "ASC",
                SortColumn = "FirstName"
            };

            var viewResult = controller.GetEmployees(filter) as JsonResult;

            Assert.IsNotNull(viewResult);
        }
Ejemplo n.º 3
0
        public IQueryable<Employee> ByFilter(EmployeeFilter filter, out Int32 pages)
        {
            IQueryable<Employee> employees;

            switch (filter.Status)
            {
                case EmployeeStatusFilter.Active:
                    employees
                        = _employeeRepository.All().Where(a => !a.IsDeleted && a.Status == EmployeeStatus.Active);
                    break;
                case EmployeeStatusFilter.Inactive:
                    employees
                        = _employeeRepository.All().Where(a => !a.IsDeleted && a.Status == EmployeeStatus.Inactive);
                    break;
                default:
                    employees
                        = _employeeRepository.All().Where(a => !a.IsDeleted);
                    break;
            }

            if (filter.SortDirection.ToUpper().Equals("ASC"))
            {
                switch (filter.SortColumn)
                {
                    case "Id":
                        employees = employees.OrderBy(x => x.Id);
                        break;
                    case "FirstName":
                        employees = employees.OrderBy(x => x.FirstName);
                        break;
                    case "Position":
                        employees = employees.OrderBy(x => x.Position);
                        break;
                    case "Status":
                        employees = employees.OrderBy(x => x.Status);
                        break;
                    case "Wage":
                        employees = employees.OrderBy(x => x.Wage);
                        break;
                    default:
                        employees = employees.OrderBy(x => x.Id);
                        break;
                }
            }
            else
            {
                switch (filter.SortColumn)
                {
                    case "Id":
                        employees = employees.OrderByDescending(x => x.Id);
                        break;
                    case "FirstName":
                        employees = employees.OrderByDescending(x => x.FirstName);
                        break;
                    case "Position":
                        employees = employees.OrderByDescending(x => x.Position);
                        break;
                    case "Status":
                        employees = employees.OrderByDescending(x => x.Status);
                        break;
                    case "Wage":
                        employees = employees.OrderByDescending(x => x.Wage);
                        break;
                    default:
                        employees = employees.OrderByDescending(x => x.Id);
                        break;
                }
            }

            pages = (Int32)Math.Ceiling((Decimal)((Double)employees.Count() / (Double)filter.Count));

            return employees.Skip(filter.Page * filter.Count).Take(filter.Count);
        }