Exemplo n.º 1
0
        public static IResult <OrderByCollection, NonEmptyString> TryParse(string orderBy, ImmutableList <NonEmptyString> allowedColumns)
        {
            var result = new List <OrderBy>();

            var orderByTrimmed = orderBy.Trim();

            if (orderByTrimmed != string.Empty)
            {
                var allowedColumnsLowerCase = allowedColumns.Select(s => (NonEmptyLowerCaseString)s.Value).ToImmutableList();

                var nonEmptyLowerCaseStringResults = orderByTrimmed
                                                     .Split(',')
                                                     .Select(orderByItem => orderByItem.Trim())
                                                     .Select(orderByItemTrimmed => NonEmptyLowerCaseString.TryCreate(orderByItemTrimmed, (NonEmptyString)nameof(orderByItemTrimmed)));

                var orderByResult = GetOrderByResult(orderBy, nonEmptyLowerCaseStringResults, allowedColumnsLowerCase);

                if (orderByResult.IsFailure)
                {
                    return(orderByResult.Error.GetFailResult <OrderByCollection>());
                }

                result.AddRange(orderByResult.Value);
            }

            var collectionResult = OrderByCollection.TryCreate(result.ToImmutableList());

            return(collectionResult.OnSuccess(() => collectionResult.Value.GetOkMessage()));
        }
Exemplo n.º 2
0
        public void DuplicatedElementsWithDiffrentOrder_ShouldFail()
        {
            var r = OrderByCollection.TryCreate(new List <OrderBy> {
                OrderBy.Create((NonEmptyString)"c", true), OrderBy.Create((NonEmptyString)"c", false)
            }.ToImmutableList());

            r.IsFailure.ShouldBeTrue();
        }
Exemplo n.º 3
0
        public void ValidElements_ShouldSucceed()
        {
            var r = OrderByCollection.TryCreate(new List <OrderBy> {
                OrderBy.Create((NonEmptyString)"c1", true), OrderBy.Create((NonEmptyString)"c2", true)
            }.ToImmutableList());

            r.IsSuccess.ShouldBeTrue();
        }
Exemplo n.º 4
0
        public void TwoOrderByCollectionsWithDiffrentValueShouldNotBeEqual()
        {
            var r1 = OrderByCollection.TryCreate(new List <OrderBy> {
                OrderBy.Create((NonEmptyString)"c", true)
            }.ToImmutableList());
            var r2 = OrderByCollection.TryCreate(new List <OrderBy> {
                OrderBy.Create((NonEmptyString)"cx", true)
            }.ToImmutableList());

            Helper.ShouldNotBeEqual(r1, r2);
        }
Exemplo n.º 5
0
        public void GetTranslatedSort_SortEmpty_ShouldReturnDefaultSort()
        {
            const string defaultColumn = "defaultColumn";

            var sort = Extensions.GetValue(() => OrderByCollection.TryCreate(new List <OrderBy>().ToImmutableList()));

            var mappings = ImmutableDictionary <NonEmptyString, NonEmptyString> .Empty;

            var defaultSort = GetDefaultSort(defaultColumn);

            var result = CommandHelper.GetTranslatedSort(sort, defaultSort, mappings);

            result.Value.ShouldBe($"{defaultColumn.ToUpper()} ASC");
        }
Exemplo n.º 6
0
        public void GetTranslatedSort_WrongMapping_ShouldReturnDefaultSort()
        {
            const string defaultColumn = "defaultColumn";

            const string sourceColumn = "sourceColumn";

            const string destinationColumn = "destinationColumn";

            var sort = Extensions.GetValue(() => OrderByCollection.TryCreate(new List <OrderBy> {
                OrderBy.Create((NonEmptyString)sourceColumn, true)
            }.ToImmutableList()));

            var mappings = new Dictionary <NonEmptyString, NonEmptyString> {
                { (NonEmptyString)(sourceColumn + "X"), (NonEmptyString)destinationColumn }
            }.ToImmutableDictionary();

            var defaultSort = GetDefaultSort(defaultColumn);

            var result = CommandHelper.GetTranslatedSort(sort, defaultSort, mappings);

            result.Value.ShouldBe($"{defaultColumn.ToUpper()} ASC");
        }
Exemplo n.º 7
0
 private static OrderByCollection GetOrderByCollection(ImmutableList <OrderBy> orderByList)
 {
     return(Extensions.GetValue(() => OrderByCollection.TryCreate(orderByList)));
 }