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

144 lines
4.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
2018-10-21 17:37:59 -04:00
, IAggregateInterceptor
, IQueryConvertInterceptor<Person>
2018-10-17 22:14:21 -04:00
//, 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-21 17:37:59 -04:00
public IAggregate InterceptAggregate(IAggregate aggregate)
{
if (aggregate.Path == nameof(PersonModel.AgeStr))
2018-10-21 17:37:59 -04:00
return new Aggregate {Type = aggregate.Type, Path = nameof(Person.Age)};
return aggregate;
}
public object InterceptResultTo(Person entity)
{
var personModel = new PersonModel();
personModel.Id = entity.Id;
personModel.FirstName = entity.FirstName;
personModel.LastName = entity.LastName;
personModel.Age = entity.Age;
personModel.Sex = entity.Sex;
return personModel;
}
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; }
public string Sex { get; set; }
2018-10-17 19:30:55 -04:00
}
public class PersonModel
2018-10-17 22:42:54 -04:00
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string Sex { get; set; }
public string AgeStr => $"{Age} years old";
public string FullName => $"{FirstName} {LastName}";
2018-10-17 22:42:54 -04:00
}
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>()
{
new Person{ Id = 1, FirstName = "David", LastName = "Lebee", Sex = "Male", Age = 29 },
new Person{ Id = 2, FirstName = "Michaela", LastName = "Lebee", Sex = "Female", Age = 29},
new Person{ Id = 3, FirstName = "Zohra", LastName = "Lebee", Sex = "Female", Age = 20},
new Person{ Id = 4, FirstName = "Eric", LastName = "Vickar", Sex = "Male", Age = 30},
new Person{ Id = 5, FirstName = "Susan", LastName = "Vickar", Sex = "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 17:06:45 -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-22 20:44:27 -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-21 17:37:59 -04:00
new Aggregate { Path = "AgeStr", Type = AggregateType.Avg }
2018-10-19 17:44:13 -04:00
};;
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
}
}
}