dotnet-dynamic-query/PoweredSoft.DynamicQuery.Cli/Program.cs

130 lines
4.4 KiB
C#
Raw Normal View History

2018-10-17 19:30:55 -04:00
using PoweredSoft.DynamicQuery.Core;
using System;
using System.Collections.Generic;
using System.Linq;
2018-10-18 21:52:05 -04:00
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
2018-10-17 19:30:55 -04:00
namespace PoweredSoft.DynamicQuery.Cli
{
2018-10-17 22:14:21 -04:00
public class PersonQueryInterceptor : IQueryInterceptor
//, IBeforeQueryAlteredInterceptor<Person>
2018-10-17 22:39:12 -04:00
//, IFilterInterceptor
2018-10-17 19:30:55 -04:00
{
2018-10-17 22:14:21 -04:00
public IQueryable<Person> InterceptQueryBeforeAltered(IQueryCriteria criteria, IQueryable<Person> queryable)
=> queryable.Where(t => t.FirstName.StartsWith("Da"));
public IFilter InterceptFilter(IFilter filter)
{
if (filter is SimpleFilter)
{
var simpleFilter = filter as ISimpleFilter;
if (simpleFilter.Path == "FirstName" && simpleFilter.Value is string && ((string)simpleFilter.Value).Contains(","))
{
var firstNames = ((string) simpleFilter.Value).Split(',');
var filters = firstNames.Select(firstName => new SimpleFilter
{
Path = "FirstName",
Type = FilterType.Equal,
Value = firstName
}).Cast<IFilter>().ToList();
return new CompositeFilter
{
Type = FilterType.Composite,
Filters = filters,
And = true
};
}
}
return filter;
}
2018-10-17 19:30:55 -04:00
}
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
2018-10-19 17:44:13 -04:00
public int Age { get; set; }
2018-10-17 19:30:55 -04:00
}
2018-10-17 22:42:54 -04:00
public class OtherClass
{
}
2018-10-17 19:30:55 -04:00
class Program
{
static void Main(string[] args)
{
Play1();
}
private static void Play1()
{
var list = new List<Person>()
{
2018-10-19 17:44:13 -04:00
new Person{ Id = 1, FirstName = "David", LastName = "Lebee", Age = 29},
new Person{ Id = 2, FirstName = "Michaela", LastName = "Lebee", Age = 29},
new Person{ Id = 3, FirstName = "Zohra", LastName = "Lebee", Age = 20},
new Person{ Id = 4, FirstName = "Eric", LastName = "Vickar", Age = 30},
new Person{ Id = 5, FirstName = "Susan", LastName = "Vickar", Age = 30},
2018-10-17 19:30:55 -04:00
};
var queryable = list.AsQueryable();
2018-10-17 22:14:21 -04:00
var criteria = new QueryCriteria();
2018-10-18 21:52:05 -04:00
criteria.Page = 1;
criteria.PageSize = 10;
2018-10-17 22:14:21 -04:00
2018-10-19 17:44:13 -04:00
/*
2018-10-18 21:52:05 -04:00
criteria.Filters = new List<IFilter>
2018-10-17 22:14:21 -04:00
{
2018-10-18 21:52:05 -04:00
new SimpleFilter() {Path = nameof(Person.LastName), Value = "Lebee", Type = FilterType.Equal},
new CompositeFilter()
{
Type = FilterType.Composite,
And = true,
Filters = new List<IFilter>
{
new SimpleFilter() {Path = nameof(Person.FirstName), Value = "David", Type = FilterType.Equal},
new SimpleFilter() {Path = nameof(Person.FirstName), Value = "Zohra", Type = FilterType.Equal},
}
}
2018-10-19 17:44:13 -04:00
};*/
criteria.Groups = new List<IGroup>()
{
new Group { Path = "LastName" }
2018-10-18 21:52:05 -04:00
};
2018-10-17 22:14:21 -04:00
2018-10-19 17:44:13 -04:00
criteria.Aggregates = new List<IAggregate>()
{
new Aggregate { Path = "Age", Type = AggregateType.Count },
new Aggregate { Path = "Age", Type = AggregateType.Avg }
};;
2018-10-17 22:14:21 -04:00
var handler = new QueryHandler();
handler.AddInterceptor(new PersonQueryInterceptor());
2018-10-18 21:52:05 -04:00
var result = handler.Execute(queryable, criteria);
var jsonSettings = new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
};
jsonSettings.Converters.Add(new StringEnumConverter { AllowIntegerValues = false });
Console.WriteLine("Request:\n");
Console.WriteLine(JsonConvert.SerializeObject(criteria, Formatting.Indented, jsonSettings));
Console.WriteLine("");
Console.WriteLine("Response:\n");
Console.WriteLine(JsonConvert.SerializeObject(result, Formatting.Indented, jsonSettings));
Console.ReadKey();
2018-10-17 19:30:55 -04:00
}
}
}