|
@@ -20,7 +20,9 @@ namespace InABox.Core
|
|
|
|
|
|
Expression Expression { get; set; }
|
|
|
|
|
|
- List<ISortOrder> Thens { get; }
|
|
|
+ IEnumerable<ISortOrder> Thens { get; }
|
|
|
+
|
|
|
+ ISortOrder ThenBy(ISortOrder sortOrder);
|
|
|
|
|
|
IEnumerable<String> ColumnNames();
|
|
|
|
|
@@ -49,7 +51,7 @@ namespace InABox.Core
|
|
|
|
|
|
public List<SortOrder<T>> Thens { get; private set; }
|
|
|
|
|
|
- List<ISortOrder> ISortOrder.Thens => Thens.OfType<ISortOrder>().ToList();
|
|
|
+ IEnumerable<ISortOrder> ISortOrder.Thens => Thens;
|
|
|
|
|
|
//public SortOrder<T> Ascending()
|
|
|
//{
|
|
@@ -62,6 +64,14 @@ namespace InABox.Core
|
|
|
// Direction = SortOrder.Descending;
|
|
|
// return this;
|
|
|
//}
|
|
|
+ ISortOrder ISortOrder.ThenBy(ISortOrder sortOrder)
|
|
|
+ {
|
|
|
+ if(sortOrder is SortOrder<T> order)
|
|
|
+ {
|
|
|
+ Thens.Add(order);
|
|
|
+ }
|
|
|
+ return this;
|
|
|
+ }
|
|
|
|
|
|
public SortOrder<T> ThenBy(Expression<Func<T, object?>> expression, SortDirection direction = SortDirection.Ascending)
|
|
|
{
|
|
@@ -254,7 +264,6 @@ namespace InABox.Core
|
|
|
|
|
|
public class SortOrderJsonConverter : CustomJsonConverter<ISortOrder>
|
|
|
{
|
|
|
-
|
|
|
public override bool CanConvert(Type objectType)
|
|
|
{
|
|
|
if (objectType.IsConstructedGenericType)
|
|
@@ -273,68 +282,70 @@ namespace InABox.Core
|
|
|
if (reader.TokenType == JsonTokenType.Null)
|
|
|
return null;
|
|
|
|
|
|
- ISortOrder? result = null;
|
|
|
- var sType = "";
|
|
|
+ var type = typeToConvert;
|
|
|
var sExpr = "";
|
|
|
- var sDir = "";
|
|
|
+ SortDirection dir = default;
|
|
|
+ List<ISortOrder> thens = new List<ISortOrder>();
|
|
|
|
|
|
- while (reader.TokenType != JsonTokenType.EndObject && reader.Read())
|
|
|
+ while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
|
|
|
{
|
|
|
var tag = reader.GetString();
|
|
|
reader.Read();
|
|
|
if (string.Equals(tag, "$type"))
|
|
|
{
|
|
|
- sType = reader.GetString();
|
|
|
+ var sType = reader.GetString();
|
|
|
if (!string.IsNullOrWhiteSpace(sType))
|
|
|
{
|
|
|
- var tType = Type.GetType(sType);
|
|
|
- if (tType != null)
|
|
|
- {
|
|
|
- var gType = typeof(SortOrder<>).MakeGenericType(tType);
|
|
|
- result = Activator.CreateInstance(gType) as ISortOrder;
|
|
|
- }
|
|
|
+ type = Type.GetType(sType) ?? typeToConvert;
|
|
|
}
|
|
|
}
|
|
|
- else if (string.Equals(tag, "Expression") && result != null)
|
|
|
+ else if (string.Equals(tag, "Expression"))
|
|
|
sExpr = reader.GetString();
|
|
|
- else if (string.Equals(tag, "Direction") && result != null)
|
|
|
- sDir = reader.GetString();
|
|
|
- else if (string.Equals(tag, "Thens") && result != null)
|
|
|
+ else if (string.Equals(tag, "Direction"))
|
|
|
+ dir = Enum.Parse<SortDirection>(reader.GetString());
|
|
|
+ else if (string.Equals(tag, "Thens"))
|
|
|
{
|
|
|
- reader.Read();
|
|
|
- while (reader.TokenType != JsonTokenType.EndObject)
|
|
|
+ while (reader.Read() && reader.TokenType != JsonTokenType.EndArray)
|
|
|
{
|
|
|
var then = Read(ref reader, typeof(ISortOrder), options);
|
|
|
if (then != null)
|
|
|
- result.Thens.Add(then);
|
|
|
+ thens.Add(then);
|
|
|
}
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
-
|
|
|
+ var prop = (CoreUtils.StringToExpression(sExpr) as MemberExpression)!;
|
|
|
+ var result = (Activator.CreateInstance(type, CoreUtils.GetFullPropertyName(prop, "."), dir) as ISortOrder)!;
|
|
|
+ foreach(var then in thens)
|
|
|
+ {
|
|
|
+ result.ThenBy(then);
|
|
|
}
|
|
|
+
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
public override void Write(Utf8JsonWriter writer, ISortOrder value, JsonSerializerOptions options)
|
|
|
{
|
|
|
var type = value.GetType().GenericTypeArguments[0];
|
|
|
-
|
|
|
- var property = CoreUtils.GetPropertyValue(value, "Expression") as MemberExpression;
|
|
|
+
|
|
|
+ var property = value.Expression;
|
|
|
var prop = CoreUtils.ExpressionToString(type, property, true);
|
|
|
- var dir = CoreUtils.GetPropertyValue(value, "Direction")?.ToString() ?? nameof(SortDirection.Ascending);
|
|
|
+ var dir = value.Direction;
|
|
|
|
|
|
writer.WriteStartObject();
|
|
|
|
|
|
- writer.WriteString("$type",value.GetType().FullName);
|
|
|
- writer.WriteString("Expression",prop);
|
|
|
- writer.WriteString("Direction",dir);
|
|
|
+ writer.WriteString("$type", value.GetType().FullName);
|
|
|
+ writer.WriteString("Expression", prop);
|
|
|
+ writer.WriteString("Direction", dir.ToString());
|
|
|
|
|
|
- if (CoreUtils.GetPropertyValue(value, "Thens") is IEnumerable<ISortOrder> thens)
|
|
|
+ if(value.Thens.Any())
|
|
|
{
|
|
|
writer.WritePropertyName("Thens");
|
|
|
writer.WriteStartArray();
|
|
|
- foreach (var then in thens)
|
|
|
- Write(writer,then,options);
|
|
|
+ foreach(var then in value.Thens)
|
|
|
+ {
|
|
|
+ Write(writer, then, options);
|
|
|
+ }
|
|
|
writer.WriteEndArray();
|
|
|
}
|
|
|
|