Skip to content
Permalink
Browse files

Expand filtering, support POST requests due to increased query size

  • Loading branch information...
Marlamin committed Sep 8, 2019
1 parent 083b8ee commit b52fc02298123b43684daaa19b679ae88e016bdc
Showing with 151 additions and 14 deletions.
  1. +151 −14 DBCDumpHost/Controllers/DataController.cs
@@ -38,12 +38,43 @@ public string Get()
return "No DBC selected!";
}

// GET: data/name
[HttpGet("{name}")]
// GET/POST: data/name
[HttpGet("{name}"), HttpPost("{name}")]
public DataTablesResult Get(string name, string build, int draw, int start, int length)
{
var searching = false;
var searchValue = Request.Query["search[value]"];

var parameters = new Dictionary<string, string>();

if(Request.Method == "POST")
{
Console.WriteLine("POST: " + Request.Form.Count);
// POST, what site uses
foreach (var post in Request.Form)
{
parameters.Add(post.Key, post.Value);
}

if (parameters.ContainsKey("draw"))
draw = int.Parse(parameters["draw"]);

if (parameters.ContainsKey("start"))
start = int.Parse(parameters["start"]);

if (parameters.ContainsKey("length"))
length = int.Parse(parameters["length"]);
}
else
{
Console.WriteLine("GET: " + Request.QueryString);
// GET, backwards compatibility for scripts/users using this
foreach (var get in Request.Query)
{
parameters.Add(get.Key, get.Value);
}
}

var searchValue = parameters["search[value]"];

if (string.IsNullOrWhiteSpace(searchValue))
{
@@ -74,24 +105,68 @@ public DataTablesResult Get(string name, string build, int draw, int start, int
result.data = new List<List<string>>();

var filtering = false;
var filters = new Dictionary<int, string>();
var filters = new Dictionary<int, (string, bool)>();

// this is gonna break for tables with arrays
for (var i = 0; i < storage.AvailableColumns.Length; ++i)
var siteColIndex = 0;
if(storage.Values.Count > 0)
{
if (Request.Query.ContainsKey("columns[" + i + "][search][value]") && !string.IsNullOrWhiteSpace(Request.Query["columns[" + i + "][search][value]"]))
DBCDRow firstItem = storage.Values.First();

for (var i = 0; i < storage.AvailableColumns.Length; ++i)
{
searching = true;
filtering = true;
filters.Add(i, Request.Query["columns[" + i + "][search][value]"]);
var field = firstItem[storage.AvailableColumns[i]];
if (field is Array a)
{
for (var j = 0; j < a.Length; j++)
{
if (parameters.ContainsKey("columns[" + siteColIndex + "][search][value]") && !string.IsNullOrWhiteSpace(parameters["columns[" + siteColIndex + "][search][value]"]))
{
var searchVal = parameters["columns[" + siteColIndex + "][search][value]"];
searching = true;
filtering = true;
if(searchVal.Length > 6 && searchVal.Substring(0, 6) == "exact:")
{
filters.Add(siteColIndex, (searchVal.Remove(0, 6), true));
}
else
{
filters.Add(siteColIndex, (searchVal, false));
}
}

siteColIndex++;
}
}
else
{
if (parameters.ContainsKey("columns[" + siteColIndex + "][search][value]") && !string.IsNullOrWhiteSpace(parameters["columns[" + siteColIndex + "][search][value]"]))
{
var searchVal = parameters["columns[" + siteColIndex + "][search][value]"];
searching = true;
filtering = true;
if (searchVal.Length > 6 && searchVal.Substring(0, 6) == "exact:")
{
filters.Add(siteColIndex, (searchVal.Remove(0, 6), true));
}
else
{
filters.Add(siteColIndex, (searchVal, false));
}
}

siteColIndex++;
}
}
}

var resultCount = 0;
foreach (DBCDRow item in storage.Values)
{
siteColIndex = 0;

var rowList = new List<string>();
var matches = false;
var allMatch = true;

for (var i = 0; i < storage.AvailableColumns.Length; ++i)
{
@@ -104,7 +179,39 @@ public DataTablesResult Get(string name, string build, int draw, int start, int
var val = a.GetValue(j).ToString();
if (searching)
{
if (!filtering)
if (filtering)
{
if (filters.ContainsKey(siteColIndex))
{
if (filters[siteColIndex].Item2)
{
if (val == filters[siteColIndex].Item1)
{
matches = true;
}
else
{
Console.WriteLine(val + " does not match " + filters[siteColIndex].Item1 + ", filtering out");
allMatch = false;
}
}
else
{
if (val.Contains(filters[siteColIndex].Item1, StringComparison.InvariantCultureIgnoreCase))
{
Console.WriteLine(val + " matches column query " + filters[siteColIndex].Item1);

matches = true;
}
else
{
Console.WriteLine(val + " does not match " + filters[siteColIndex].Item1 + ", filtering out");
allMatch = false;
}
}
}
}
else
{
if (val.Contains(searchValue, StringComparison.InvariantCultureIgnoreCase))
matches = true;
@@ -114,6 +221,8 @@ public DataTablesResult Get(string name, string build, int draw, int start, int
val = System.Web.HttpUtility.HtmlEncode(val);

rowList.Add(val);

siteColIndex++;
}
}
else
@@ -123,8 +232,35 @@ public DataTablesResult Get(string name, string build, int draw, int start, int
{
if (filtering)
{
if(filters.ContainsKey(i) && filters[i] == val)
matches = true;
if (filters.ContainsKey(siteColIndex))
{
if (filters[siteColIndex].Item2)
{
if (val == filters[siteColIndex].Item1)
{
matches = true;
}
else
{
Console.WriteLine(val + " does not match " + filters[siteColIndex].Item1 + ", filtering out");
allMatch = false;
}
}
else
{
if (val.Contains(filters[siteColIndex].Item1, StringComparison.InvariantCultureIgnoreCase))
{
Console.WriteLine(val + " matches column query " + filters[siteColIndex].Item1);

matches = true;
}
else
{
Console.WriteLine(val + " does not match " + filters[siteColIndex].Item1 + ", filtering out");
allMatch = false;
}
}
}
}
else
{
@@ -136,12 +272,13 @@ public DataTablesResult Get(string name, string build, int draw, int start, int
val = System.Web.HttpUtility.HtmlEncode(val);

rowList.Add(val);
siteColIndex++;
}
}

if (searching)
{
if (matches)
if (matches && allMatch)
{
resultCount++;
result.data.Add(rowList);

0 comments on commit b52fc02

Please sign in to comment.
You can’t perform that action at this time.