public async Task <IActionResult> Index(DateTime?dateOrder, string fullNameCust, int?employee,
                                                bool?guarantee, int page = 1, Order.SortState sortOrder = Order.SortState.DateOrderDesc)
        {
            int pageSize = 20;

            IQueryable <Order> source = _context.Orders
                                        .Include(o => o.RepairedModel)
                                        .Include(o => o.TypeOfFault)
                                        .Include(o => o.ServicedStore)
                                        .Include(o => o.Employee);

            if (dateOrder != null)
            {
                source = source.Where(o => o.DateOrder.Year == dateOrder.Value.Year &&
                                      o.DateOrder.Month == dateOrder.Value.Month &&
                                      o.DateOrder.Day == dateOrder.Value.Day);
            }

            if (!String.IsNullOrEmpty(fullNameCust))
            {
                source = source.Where(o => o.FullNameCustumer.Contains(fullNameCust));
            }

            if (employee != null && employee != 0)
            {
                source = source.Where(o => o.EmployeeId == employee);
            }

            if (guarantee != null)
            {
                source = source.Where(o => o.GuaranteeMark == guarantee.Value);
            }

            source = OrdersSort(source, sortOrder);

            int count = await source.CountAsync();

            var items = await source.Skip((page - 1) *pageSize)
                        .Take(pageSize)
                        .ToListAsync();

            var employees = await _context.Employees.ToListAsync();

            OrdersViewModel ordersViewModels = new OrdersViewModel()
            {
                OrdersSort    = new OrdersSort(sortOrder),
                OrdersFilter  = new OrdersFilter(dateOrder, fullNameCust, employees, employee, guarantee),
                Orders        = source,
                PageViewModel = new PageViewModel(count, page, pageSize)
            };

            return(View(ordersViewModels));
        }
        public OrdersSort(Order.SortState sortOrder)
        {
            DateOrderSort = sortOrder == Order.SortState.DateOrderAsc ? Order.SortState.DateOrderDesc
                                                                      : Order.SortState.DateOrderAsc;

            ReturnDateSort = sortOrder == Order.SortState.ReturnDateAsc ? Order.SortState.ReturnDateDesc
                                                                      : Order.SortState.ReturnDateAsc;

            FullNameCustSort = sortOrder == Order.SortState.FullNameCustAsc ? Order.SortState.FullNameCustDesc
                                                                      : Order.SortState.FullNameCustAsc;

            RepModelSort = sortOrder == Order.SortState.RepModelAsc ? Order.SortState.RepModelDesc
                                                                    : Order.SortState.RepModelAsc;

            TypeOfFaultSort = sortOrder == Order.SortState.TypeOfFaultAsc ? Order.SortState.TypeOfFaultDesc
                                                                          : Order.SortState.TypeOfFaultAsc;

            ServicedStoreSort = sortOrder == Order.SortState.ServiceStoreAsc ? Order.SortState.ServiceStroeDesc
                                                                             : Order.SortState.ServiceStoreAsc;

            GuaranteeMarkSort = sortOrder
                                == Order.SortState.GuaranteeMarkAsc ? Order.SortState.GuaranteeMarkDesc
                                                      : Order.SortState.GuaranteeMarkAsc;

            GuaranteePeriodSort = sortOrder
                                  == Order.SortState.GuaranteePeriodAsc ? Order.SortState.GuaranteePeriodDesc
                                                      : Order.SortState.GuaranteePeriodAsc;

            PriceSort = sortOrder == Order.SortState.PriceAsc ? Order.SortState.PriceDesc
                                                              : Order.SortState.PriceAsc;

            EmployeeSort = sortOrder == Order.SortState.EmployeeAsc ? Order.SortState.EmployeeDesc
                                                                    : Order.SortState.EmployeeAsc;

            Current = sortOrder;
        }
Beispiel #3
0
        private IQueryable <Order> OrdersSort(IQueryable <Order> orders, Order.SortState sortOrder)
        {
            switch (sortOrder)
            {
            case Order.SortState.DateOrderAsc:
                return(orders.OrderBy(o => o.DateOrder));

            case Order.SortState.DateOrderDesc:
                return(orders.OrderByDescending(o => o.DateOrder));

            case Order.SortState.ReturnDateAsc:
                return(orders.OrderBy(o => o.ReturnDate));

            case Order.SortState.ReturnDateDesc:
                return(orders.OrderByDescending(o => o.ReturnDate));

            case Order.SortState.FullNameCustAsc:
                return(orders.OrderBy(o => o.FullNameCustumer));

            case Order.SortState.FullNameCustDesc:
                return(orders.OrderByDescending(o => o.FullNameCustumer));

            case Order.SortState.RepModelAsc:
                return(orders.OrderBy(o => o.RepairedModel.Name));

            case Order.SortState.RepModelDesc:
                return(orders.OrderByDescending(o => o.RepairedModel.Name));

            case Order.SortState.TypeOfFaultAsc:
                return(orders.OrderBy(o => o.TypeOfFault.Name));

            case Order.SortState.TypeOfFaultDesc:
                return(orders.OrderByDescending(o => o.TypeOfFault.Name));

            case Order.SortState.GuaranteeMarkAsc:
                return(orders.OrderBy(o => o.GuaranteeMark));

            case Order.SortState.GuaranteeMarkDesc:
                return(orders.OrderByDescending(o => o.GuaranteeMark));

            case Order.SortState.GuaranteePeriodAsc:
                return(orders.OrderBy(o => o.GuaranteePeriod));

            case Order.SortState.GuaranteePeriodDesc:
                return(orders.OrderByDescending(o => o.GuaranteePeriod));

            case Order.SortState.PriceAsc:
                return(orders.OrderBy(o => o.Price));

            case Order.SortState.PriceDesc:
                return(orders.OrderByDescending(o => o.Price));

            case Order.SortState.EmployeeAsc:
                return(orders.OrderBy(o => o.Employee.FullName));

            case Order.SortState.EmployeeDesc:
                return(orders.OrderByDescending(o => o.Employee.FullName));

            default:
                return(orders.OrderBy(o => o.DateOrder));
            }
        }