static IssueFilter MakeIssueFilter(CriteriaOperator filter)
        {
            return(filter.Match(
                       binary: (propertyName, value, type) => {
                switch (propertyName)
                {
                case "Votes":
                    if (type == BinaryOperatorType.GreaterOrEqual)
                    {
                        return new IssueFilter(minVotes: (int)value);
                    }
                    if (type == BinaryOperatorType.LessOrEqual)
                    {
                        return new IssueFilter(maxVotes: (int)value);
                    }
                    throw new InvalidOperationException();

                case "Created":
                    if (type == BinaryOperatorType.GreaterOrEqual)
                    {
                        return new IssueFilter(createdFrom: (DateTime)value);
                    }
                    if (type == BinaryOperatorType.Less)
                    {
                        return new IssueFilter(createdTo: (DateTime)value);
                    }
                    throw new InvalidOperationException();

                case "Tags":
                    if (type == BinaryOperatorType.Equal)
                    {
                        return new IssueFilter(tag: (string)value);
                    }
                    throw new InvalidOperationException();

                default:
                    throw new InvalidOperationException();
                }
            },
                       and: filters => {
                return new IssueFilter(
                    createdFrom: filters.Select(x => x.CreatedFrom).SingleOrDefault(x => x != null),
                    createdTo: filters.Select(x => x.CreatedTo).SingleOrDefault(x => x != null),
                    minVotes: filters.Select(x => x.MinVotes).SingleOrDefault(x => x != null),
                    maxVotes: filters.Select(x => x.MaxVotes).SingleOrDefault(x => x != null),
                    tag: filters.Select(x => x.Tag).SingleOrDefault(x => x != null)
                    );
            },
                       @null: default(IssueFilter)
                       ));
        }
        static IssueFilter MakeIssueFilter(CriteriaOperator filter)
        {
            return(filter.Match(
                       binary: (propertyName, value, type) => {
                if (propertyName == "Votes" && type == BinaryOperatorType.GreaterOrEqual)
                {
                    return new IssueFilter(minVotes: (int)value);
                }

                if (propertyName == "Priority" && type == BinaryOperatorType.Equal)
                {
                    return new IssueFilter(priority: (Priority)value);
                }

                if (propertyName == "Created")
                {
                    if (type == BinaryOperatorType.GreaterOrEqual)
                    {
                        return new IssueFilter(createdFrom: (DateTime)value);
                    }
                    if (type == BinaryOperatorType.Less)
                    {
                        return new IssueFilter(createdTo: (DateTime)value);
                    }
                }

                throw new InvalidOperationException();
            },
                       and: filters => {
                return new IssueFilter(
                    createdFrom: filters.Select(x => x.CreatedFrom).SingleOrDefault(x => x != null),
                    createdTo: filters.Select(x => x.CreatedTo).SingleOrDefault(x => x != null),
                    minVotes: filters.Select(x => x.MinVotes).SingleOrDefault(x => x != null),
                    priority: filters.Select(x => x.Priority).SingleOrDefault(x => x != null)
                    );
            },
                       @null: default(IssueFilter)
                       ));
        }
Example #3
0
        public static IssueFilter MakeIssueFilter(CriteriaOperator filter)
        {
            return(filter.Match(
                       binary: (propertyName, value, type) => {
                if (propertyName == "Votes")
                {
                    int?_value = null;
                    if (value != null)
                    {
                        double v;
                        if (double.TryParse(value.ToString(), out v))
                        {
                            _value = (int)v;
                        }
                    }
                    switch (type)
                    {
                    case BinaryOperatorType.Greater:
                        return new IssueFilter(minVotes: _value == null ? 0 : (int)(double)_value + 1);

                    case BinaryOperatorType.GreaterOrEqual:
                        return new IssueFilter(minVotes: _value == null ? 0 : (int)(double)_value);

                    case BinaryOperatorType.Less:
                        return new IssueFilter(maxVotes: _value == null || (double)_value < 0 ? int.MaxValue : (int)(double)_value - 1);

                    case BinaryOperatorType.LessOrEqual:
                        return new IssueFilter(maxVotes: _value == null || (double)_value < 0 ? int.MaxValue : (int)(double)_value);
                    }
                }
                if (propertyName == "Created")
                {
                    switch (type)
                    {
                    case BinaryOperatorType.Greater:
                        return new IssueFilter(createdFrom: value == null ? DateTime.MinValue : ((DateTime)value).AddTicks(1));

                    case BinaryOperatorType.GreaterOrEqual:
                        return new IssueFilter(createdFrom: value == null ? DateTime.MinValue : (DateTime)value);

                    case BinaryOperatorType.Less:
                        return new IssueFilter(createdTo: value == null ? DateTime.MaxValue : ((DateTime)value).AddTicks(-1));

                    case BinaryOperatorType.LessOrEqual:
                        return new IssueFilter(createdTo: value == null ? DateTime.MaxValue : (DateTime)value);
                    }
                }
                if (propertyName == "Priority" && type == BinaryOperatorType.Equal)
                {
                    return new IssueFilter(priority: (Priority)value);
                }
                if (propertyName == "User" && type == BinaryOperatorType.Equal)
                {
                    return new IssueFilter(user: (string)value);
                }

                throw new InvalidOperationException(string.Format("Can't convert '{0}' FilterCriteria to IssueFilter (type = '{1}', value = '{2}')", propertyName, type, value));
            },
                       and: filters => {
                var result = new IssueFilter(
                    priority: filters.Select(x => x.Priority).SingleOrDefault(x => x != null),
                    createdFrom: filters.Select(x => x.CreatedFrom).SingleOrDefault(x => x != null),
                    createdTo: filters.Select(x => x.CreatedTo).SingleOrDefault(x => x != null),
                    minVotes: filters.Select(x => x.MinVotes).SingleOrDefault(x => x != null),
                    maxVotes: filters.Select(x => x.MaxVotes).SingleOrDefault(x => x != null),
                    user: filters.Select(x => x.User).SingleOrDefault(x => x != null)
                    );
                return result;
            },
                       @null: default(IssueFilter)
                       ));
        }