This commit is contained in:
@@ -62,7 +62,7 @@ NOTES ABOUT WHY I DID THE FILTEROPTIONS LIKE I DID:
|
|||||||
|
|
||||||
SORTING
|
SORTING
|
||||||
=-=-=-=-
|
=-=-=-=-
|
||||||
|
|
||||||
Sorting is done same as filtering but property on datafilter is called Sort and it's also a json Array
|
Sorting is done same as filtering but property on datafilter is called Sort and it's also a json Array
|
||||||
Fields are: "fld" which is the same column name as in a datafilter and "dir" for direction which will contain a single character either a "-" or a "+" for descending and ascending respectively
|
Fields are: "fld" which is the same column name as in a datafilter and "dir" for direction which will contain a single character either a "-" or a "+" for descending and ascending respectively
|
||||||
|
If no Sort is defined the default will be by ID number descending meaning the most recent records in that table will be first
|
||||||
|
|
||||||
|
|||||||
@@ -317,8 +317,6 @@ namespace AyaNova.Biz
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//VALIDATE SORT
|
//VALIDATE SORT
|
||||||
//Filter json must parse
|
//Filter json must parse
|
||||||
if (!string.IsNullOrWhiteSpace(inObj.Sort))
|
if (!string.IsNullOrWhiteSpace(inObj.Sort))
|
||||||
@@ -330,12 +328,12 @@ namespace AyaNova.Biz
|
|||||||
{
|
{
|
||||||
var sortItem = v[i];
|
var sortItem = v[i];
|
||||||
if (sortItem["fld"] == null)
|
if (sortItem["fld"] == null)
|
||||||
AddError(ValidationErrorType.RequiredPropertyEmpty, "Filter", $"Filter array item {i}, object is missing required \"fld\" property ");
|
AddError(ValidationErrorType.RequiredPropertyEmpty, "Sort", $"Sort array item {i}, object is missing required \"fld\" property ");
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var fld = sortItem["fld"].Value<string>();
|
var fld = sortItem["fld"].Value<string>();
|
||||||
if (string.IsNullOrWhiteSpace(fld))
|
if (string.IsNullOrWhiteSpace(fld))
|
||||||
AddError(ValidationErrorType.RequiredPropertyEmpty, "Filter", $"Filter array item {i}, \"fld\" property is empty and required");
|
AddError(ValidationErrorType.RequiredPropertyEmpty, "Sort", $"Sort array item {i}, \"fld\" property is empty and required");
|
||||||
|
|
||||||
//validate the field name if we can
|
//validate the field name if we can
|
||||||
if (ListValidFilterOptions != null)
|
if (ListValidFilterOptions != null)
|
||||||
@@ -343,32 +341,19 @@ namespace AyaNova.Biz
|
|||||||
|
|
||||||
if (!ListValidFilterOptions.Flds.Exists(x => x.Fld == fld))
|
if (!ListValidFilterOptions.Flds.Exists(x => x.Fld == fld))
|
||||||
{
|
{
|
||||||
AddError(ValidationErrorType.InvalidValue, "Filter", $"Filter array item {i}, fld property value \"{fld}\" is not a valid value for ListKey specified");
|
AddError(ValidationErrorType.InvalidValue, "Sort", $"Sort array item {i}, fld property value \"{fld}\" is not a valid value for ListKey specified");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (sortItem["op"] == null)
|
if (sortItem["dir"] == null)
|
||||||
AddError(ValidationErrorType.RequiredPropertyEmpty, "Filter", $"Filter array item {i}, object is missing required \"op\" property ");
|
AddError(ValidationErrorType.RequiredPropertyEmpty, "Sort", $"Sort array item {i}, object is missing required \"dir\" sort direction property ");
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var opType = sortItem["op"].Value<string>();
|
var sortDir = sortItem["dir"].Value<string>();
|
||||||
if (!FilterComparisonOperator.Operators.Contains(opType))
|
if (sortDir != "+" && sortDir != "-")
|
||||||
AddError(ValidationErrorType.InvalidValue, "Filter", $"Filter array item {i}, \"op\" property value of \"{opType}\" is not a valid FilterComparisonOperator type");
|
AddError(ValidationErrorType.InvalidValue, "Sort", $"Sort array item {i}, \"dir\" property value of \"{sortDir}\" is not a valid sort direction value, must be \"+\" or \"-\" only");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sortItem["value"] == null)
|
|
||||||
AddError(ValidationErrorType.RequiredPropertyEmpty, "Filter", $"Filter array item {i}, object is missing or is empty the required \"value\" property ");
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (sortItem["value"].Type == JTokenType.String && string.IsNullOrWhiteSpace(sortItem["value"].Value<string>()))
|
|
||||||
AddError(ValidationErrorType.RequiredPropertyEmpty, "Filter", $"Filter array item {i}, object is missing or is empty the required \"value\" property ");
|
|
||||||
|
|
||||||
if (sortItem["value"].Type == JTokenType.Array && sortItem["value"].Count() == 0)
|
|
||||||
AddError(ValidationErrorType.RequiredPropertyEmpty, "Filter", $"Filter array item {i}, object is missing or is empty the required \"value\" property ARRAY ");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//NOTE: value of nothing, null or empty is a valid value so no checking for it here
|
//NOTE: value of nothing, null or empty is a valid value so no checking for it here
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
61
server/AyaNova/biz/FilterSqlOrderByBuilder.cs
Normal file
61
server/AyaNova/biz/FilterSqlOrderByBuilder.cs
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System;
|
||||||
|
using System.Globalization;
|
||||||
|
using System.Text;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
using System.Linq;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace AyaNova.Biz
|
||||||
|
{
|
||||||
|
public static class FilterSqlOrderByBuilder
|
||||||
|
{
|
||||||
|
|
||||||
|
public static string DefaultOrderBy(){
|
||||||
|
return " ORDER BY ID DESC";
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string DataFilterToSQLOrderBy(AyaNova.Models.DataFilter dataFilter)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(dataFilter.Sort))
|
||||||
|
{
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
|
//iterate the datafilter and concatenate a sql query from it
|
||||||
|
var SortArray = JArray.Parse(dataFilter.Filter);
|
||||||
|
for (int i = 0; i < SortArray.Count; i++)
|
||||||
|
{
|
||||||
|
|
||||||
|
var SortItem = SortArray[i];
|
||||||
|
var fld = SortItem["fld"].Value<string>();
|
||||||
|
var opType = SortItem["op"].Value<string>();
|
||||||
|
List<string> tagList = new List<string>();
|
||||||
|
string val = string.Empty;
|
||||||
|
if (SortItem["value"].Type != JTokenType.Array)
|
||||||
|
val = SortItem["value"].Value<string>();
|
||||||
|
else
|
||||||
|
{
|
||||||
|
tagList = SortItem["value"].ToObject<List<String>>();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
sb.Append("(");
|
||||||
|
|
||||||
|
if (i < SortArray.Count - 1)
|
||||||
|
{
|
||||||
|
sb.Append(") AND ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return " ORDER BY " + sb.ToString() + ")";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}//eoc
|
||||||
|
}//ens
|
||||||
@@ -180,7 +180,7 @@ namespace AyaNova.Biz
|
|||||||
//base query
|
//base query
|
||||||
var q = "SELECT *, xmin FROM AWIDGET ";
|
var q = "SELECT *, xmin FROM AWIDGET ";
|
||||||
|
|
||||||
//GET THE FILTER
|
//GET THE FILTER / SORT
|
||||||
if (pagingOptions.DataFilterId > 0)
|
if (pagingOptions.DataFilterId > 0)
|
||||||
{
|
{
|
||||||
var TheFilter = await ct.DataFilter.FirstOrDefaultAsync(x => x.Id == pagingOptions.DataFilterId);
|
var TheFilter = await ct.DataFilter.FirstOrDefaultAsync(x => x.Id == pagingOptions.DataFilterId);
|
||||||
@@ -188,6 +188,9 @@ namespace AyaNova.Biz
|
|||||||
//BUILD WHERE AND APPEND IT
|
//BUILD WHERE AND APPEND IT
|
||||||
q = q + FilterSqlCriteriaBuilder.DataFilterToSQLCriteria(TheFilter, WidgetBiz.FilterOptions(), UserId);
|
q = q + FilterSqlCriteriaBuilder.DataFilterToSQLCriteria(TheFilter, WidgetBiz.FilterOptions(), UserId);
|
||||||
|
|
||||||
|
//BUILD ORDER BY AND APPEND IT
|
||||||
|
q = q + FilterSqlCriteriaBuilder.DataFilterToSQLCriteria(TheFilter, WidgetBiz.FilterOptions(), UserId);
|
||||||
|
|
||||||
//TESTING:
|
//TESTING:
|
||||||
// //TODO: remove this from production build
|
// //TODO: remove this from production build
|
||||||
// //LOG THE CRIT AND QUERY
|
// //LOG THE CRIT AND QUERY
|
||||||
@@ -196,10 +199,11 @@ namespace AyaNova.Biz
|
|||||||
// log.LogInformation(TheFilter.Filter);
|
// log.LogInformation(TheFilter.Filter);
|
||||||
// log.LogInformation("Generated SQL:");
|
// log.LogInformation("Generated SQL:");
|
||||||
// log.LogInformation(q);
|
// log.LogInformation(q);
|
||||||
|
}else{
|
||||||
|
//GET DEFAULT ORDER BY
|
||||||
}
|
}
|
||||||
|
|
||||||
//BUILD ORDER BY AND APPEND IT
|
|
||||||
//TODO: Code a separate order by builder block
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user