Example #1
0
        public static OrderSortingOption GetOpposite(this OrderSortingOption orderSortingOption)
        {
            switch (orderSortingOption)
            {
            case OrderSortingOption.IdDescending:
                return(OrderSortingOption.IdAscending);

            case OrderSortingOption.IdAscending:
                return(OrderSortingOption.IdDescending);

            case OrderSortingOption.DateDescending:
                return(OrderSortingOption.DateAscending);

            case OrderSortingOption.DateAscending:
                return(OrderSortingOption.DateDescending);

            case OrderSortingOption.TotalDescending:
                return(OrderSortingOption.TotalAscending);

            case OrderSortingOption.TotalAscending:
                return(OrderSortingOption.TotalDescending);

            case OrderSortingOption.StateAscending:
                return(OrderSortingOption.StateDescending);

            case OrderSortingOption.StateDescending:
                return(OrderSortingOption.StateAscending);

            default:
                return(OrderSortingOption.None);
            }
        }
        public static IOrderedEnumerable <Order> SortBy(this IEnumerable <Order> orders,
                                                        OrderSortingOption sortingOption)
        {
            switch (sortingOption)
            {
            default:
            case OrderSortingOption.None:
            case OrderSortingOption.DateAscending:
                return(orders.OrderBy(o => o.Date));

            case OrderSortingOption.DateDescending:
                return(orders.OrderByDescending(o => o.Date));

            case OrderSortingOption.IdAscending:
                return(orders.OrderBy(o => o.Id));

            case OrderSortingOption.IdDescending:
                return(orders.OrderByDescending(o => o.Id));

            case OrderSortingOption.TotalAscending:
                return(orders.OrderBy(o => o.Total));

            case OrderSortingOption.TotalDescending:
                return(orders.OrderByDescending(o => o.Total));

            case OrderSortingOption.StateAscending:
                return(orders.OrderBy(o => o.State));

            case OrderSortingOption.StateDescending:
                return(orders.OrderByDescending(o => o.State));
            }
        }
        public async Task <IActionResult> Index(OrderSortingType orderSortingType, OrderSortingOption previousSortingOption)
        {
            OrderSortingOption newSortingOption = OrdersSortingLogic.GetNewSortingOption(orderSortingType, previousSortingOption);

            OrdersListViewModel ordersListViewModel = new OrdersListViewModel();

            ordersListViewModel.PreviousSortingOption = newSortingOption;

            string userId = await GetCurrentUserIdAsync();

            ordersListViewModel.Orders = orderRepository
                                         .GetUserOrders(userId)
                                         .SortBy(newSortingOption);

            return(View(ordersListViewModel));
        }
        //TODO: refactor! maybe replace with state pattern
        public static OrderSortingOption GetNewSortingOption(OrderSortingType orderSortingType, OrderSortingOption previousSortingOption)
        {
            OrderSortingOption newSortingOption = OrderSortingOption.None;

            if (orderSortingType != OrderSortingType.None && previousSortingOption != OrderSortingOption.None)
            {
                switch (orderSortingType)
                {
                case OrderSortingType.Id:
                    if (previousSortingOption == OrderSortingOption.IdAscending || previousSortingOption == OrderSortingOption.IdDescending)
                    {
                        newSortingOption = previousSortingOption.GetOpposite();
                    }
                    break;

                default:
                case OrderSortingType.Date:
                    if (previousSortingOption == OrderSortingOption.DateAscending || previousSortingOption == OrderSortingOption.DateDescending)
                    {
                        newSortingOption = previousSortingOption.GetOpposite();
                    }
                    break;

                case OrderSortingType.Total:
                    if (previousSortingOption == OrderSortingOption.TotalAscending || previousSortingOption == OrderSortingOption.TotalDescending)
                    {
                        newSortingOption = previousSortingOption.GetOpposite();
                    }
                    break;

                case OrderSortingType.State:
                    if (previousSortingOption == OrderSortingOption.TotalAscending || previousSortingOption == OrderSortingOption.TotalDescending)
                    {
                        newSortingOption = previousSortingOption.GetOpposite();
                    }
                    break;
                }

                if (newSortingOption == OrderSortingOption.None)
                {
                    previousSortingOption = OrderSortingOption.None;
                }
            }

            if (previousSortingOption == OrderSortingOption.None && orderSortingType != OrderSortingType.None)
            {
                switch (orderSortingType)
                {
                case OrderSortingType.Id:
                    newSortingOption = OrderSortingOption.IdDescending;
                    break;

                default:
                case OrderSortingType.Date:
                    newSortingOption = OrderSortingOption.DateDescending;
                    break;

                case OrderSortingType.Total:
                    newSortingOption = OrderSortingOption.TotalDescending;
                    break;

                case OrderSortingType.State:
                    newSortingOption = OrderSortingOption.StateDescending;
                    break;
                }
            }

            return(newSortingOption);
        }