Example #1
0
        public FilterDefinition <TEntity> GetDbTermFilterDefinition()
        {
            var rowTerm = rowTermData.GetRawTerm();

            Alias = AliasStore.GetAlias <TEntity>(rowTerm.field);
            var field = Alias?.DataBasePropertyName;


            Require.Strings.NotNullOrWhiteSpace <BadRequestException>(field,
                                                                      () => $"Specified field '{rowTerm.field}' for filtering data is not exist");
            Require.Booleans.IsTrue <BadRequestException>(
                StrictFilterContractConstants.AvailableRules.Any(x => x == rowTerm.command),
                () => $"Specified operator '{rowTerm.command}' for filtering data is not exist");

            CommandName = rowTerm.command;
            FieldName   = field;
            Content     = rowTerm.content;

            return(StrictFilterContractConstants.GetFilterDefinition(this));
        }
        private string GetOrderTermOrThrowException <T>(
            string rowValue,
            string firstSeparator)
        {
            string[] dataArray = rowValue?.Split(firstSeparator);

            Func <string> message = () => $"Specified order value: '{rowValue}' is not valid";

            Require.Collections.NotEmpty <BadRequestException, string>(dataArray, message);
            Require.Booleans.IsTrue <BadRequestException>(dataArray.Count() == 2, message);
            Require.Strings.NotNullOrWhiteSpace <BadRequestException>(dataArray[0], message);
            Require.Strings.NotNullOrWhiteSpace <BadRequestException>(dataArray[1], message);

            Alias alias = aliasStore.GetAlias <T>(dataArray[0]);

            Require.Objects.NotNull <BadRequestException>(alias, message);

            Require.Booleans.IsTrue <BadRequestException>(
                dataArray[1].Equals(Ascending, StringComparison.OrdinalIgnoreCase) ||
                dataArray[1].Equals(Descending, StringComparison.OrdinalIgnoreCase), message);

            return(rowValue.Replace(dataArray[0], alias.DataBasePropertyName));
        }
Example #3
0
        private void DoConvert(string originalMessage, ref List <string> result, string parameters = "")
        {
            if (string.IsNullOrEmpty(originalMessage))
            {
                result.Add(string.Empty);
                return;
            }

            originalMessage = originalMessage.Trim();

            var words     = originalMessage.Split(' ');
            var firstWord = words.First();

            var isKnownAlias = _aliasStore.ContainsAlias(firstWord);

            if (!isKnownAlias)
            {
                if (!string.IsNullOrEmpty(parameters))
                {
                    object[] stringParams1 = parameters.Split(' ').ToArray();
                    originalMessage = string.Format(originalMessage, stringParams1);
                }
                result.Add(originalMessage);
                return;
            }

            var myAlias = _aliasStore.GetAlias(firstWord);

            var containsMultipleCommands = myAlias.Contains("|");

            if (containsMultipleCommands)
            {
                var commands = myAlias.Split('|').Select(x => x.Trim());
                var param    = string.Join(" ", words.Skip(1).ToArray());

                foreach (var command in commands)
                {
                    DoConvert(command, ref result, param);
                }

                return;
            }

            var parameterCountInAlias = myAlias.ToCharArray().Where(x => x == '{').Count();

            object[] stringParams = words.Skip(1).ToList().ToArray();
            if (stringParams.Length < parameterCountInAlias)
            {
                var allParams         = new List <string>((IEnumerable <string>)stringParams);
                var emptyParamsNeeded = parameterCountInAlias - stringParams.Length;

                for (var i = 0; i < emptyParamsNeeded; i++)
                {
                    allParams.Add("");
                }

                stringParams = allParams.ToArray();
            }

            var outPut = string.Format(myAlias, stringParams);

            var shouldIncludeAllWordsFromTheOriginalMessage = outPut.ToCharArray().Last() == '-';

            if (shouldIncludeAllWordsFromTheOriginalMessage)
            {
                var skipCount     = 1 + parameterCountInAlias;
                var restOfMessage = string.Join(" ", words.Skip(skipCount).ToArray());

                outPut = outPut.Remove(outPut.Length - 1) + " " + restOfMessage;
            }

            if (!string.Equals(outPut, originalMessage))
            {
                DoConvert(outPut, ref result);
            }
            else
            {
                result.Add(outPut);
            }

            return;
        }