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

116 lines
3.9 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-21 14:15:21 -04:00
public string Sexe { 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-21 14:15:21 -04:00
new Person{ Id = 1, FirstName = "David", LastName = "Lebee", Sexe = "Male", Age = 29},
new Person{ Id = 2, FirstName = "Michaela", LastName = "Lebee", Sexe = "Female", Age = 29},
new Person{ Id = 3, FirstName = "Zohra", LastName = "Lebee", Sexe = "Female", Age = 20},
new Person{ Id = 4, FirstName = "Eric", LastName = "Vickar", Sexe = "Male", Age = 30},
new Person{ Id = 5, FirstName = "Susan", LastName = "Vickar", Sexe = "Female", 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
criteria.Groups = new List<IGroup>()
{
2018-10-21 14:15:21 -04:00
new Group { Path = "LastName" },
new Group { Path = "Sexe" }
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>()
{
2018-10-21 14:15:21 -04:00
new Aggregate { Type = AggregateType.Count },
2018-10-19 17:44:13 -04:00
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
}
}
}