public void IgnoresMoveBeyondLowerBounds()
        {
            var set = CreateTestSet();

            var changeSpec = new SortOrderChangeSpecification { From = 1, To = 0 };
            set.ChangeSortOrder(changeSpec);

            AssertSort(set, 1, "A");
            AssertSort(set, 2, "B");
            AssertSort(set, 3, "C");
            AssertSort(set, 4, "D");
            AssertSort(set, 5, "E");
        }
        public void IgnoresInvalidFrom()
        {
            var set = CreateTestSet();

            var changeSpec = new SortOrderChangeSpecification { From = 6, To = 1 };
            set.ChangeSortOrder(changeSpec);

            AssertSort(set, 1, "A");
            AssertSort(set, 2, "B");
            AssertSort(set, 3, "C");
            AssertSort(set, 4, "D");
            AssertSort(set, 5, "E");
        }
        /// <summary>
        /// Changes the sort order of the entities in the list according to the specified change.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="specification">The specification.</param>
        public static void ChangeSortOrder(this IQueryable<ISortOrderedEntity> source, SortOrderChangeSpecification specification)
        {
            if (specification.To == specification.From)
                return;

            if (specification.To < source.Min(s => s.SortOrder) || specification.To > source.Max(s => s.SortOrder) )
                return;

            var itemToMove = source.FirstOrDefault(s => s.SortOrder == specification.From);
            if (itemToMove != null)
            {
                if (specification.From > specification.To)
                    source.Where(s => s.SortOrder >= specification.To && s.SortOrder < specification.From)
                        .ToList().ForEach(s => s.SortOrder++);
                else
                    source.Where(s => s.SortOrder <= specification.To && s.SortOrder > specification.From)
                        .ToList().ForEach(s => s.SortOrder--);

                itemToMove.SortOrder = specification.To;
            }
        }
        public void MovesUp()
        {
            var set = CreateTestSet();

            var changeSpec = new SortOrderChangeSpecification { From = 4, To = 2 };
            set.ChangeSortOrder(changeSpec);

            AssertSort(set, 1, "A");
            AssertSort(set, 2, "D");
            AssertSort(set, 3, "B");
            AssertSort(set, 4, "C");
            AssertSort(set, 5, "E");
        }