This commit is contained in:
Zel
2025-02-23 18:47:21 +08:00
parent eaaffeeccb
commit e46a7ca31c
104 changed files with 2630 additions and 2516 deletions

View File

@@ -8,18 +8,19 @@ namespace ZelWiki.Library
public static class QueryStringConverter
{
/// <summary>
/// Takes the current page query string and upserts the given order-by field,
/// if the string already sorts on the given field then the order is inverted (asc/desc).
/// 排序
/// </summary>
/// <param name="context"></param>
/// <param name="value"></param>
/// <returns></returns>
public static string OrderHelper(ISessionState context, string value)
{
string orderByKey = "OrderBy";
string orderByDirectionKey = "OrderByDirection";
string? currentDirection = "asc";
var orderByKey = "OrderBy";
var orderByDirectionKey = "OrderByDirection";
var currentDirection = "asc";
var collection = ToDictionary(context.QueryString);
//Check to see if we are sorting on the value that we are already sorted on, this would mean we need to invert the sort.
if (collection.TryGetValue(orderByKey, out var currentValue))
{
bool invertDirection = string.Equals(currentValue, value, StringComparison.InvariantCultureIgnoreCase);
@@ -28,14 +29,7 @@ namespace ZelWiki.Library
{
if (collection.TryGetValue(orderByDirectionKey, out currentDirection))
{
if (currentDirection == "asc")
{
currentDirection = "desc";
}
else
{
currentDirection = "asc";
}
currentDirection = currentDirection == "asc" ? "desc" : "asc";
}
}
else
@@ -53,13 +47,13 @@ namespace ZelWiki.Library
return FromCollection(collection);
}
/// <summary>
/// Takes the current page query string and upserts a query key/value, replacing any conflicting query string entry.
/// </summary>
/// <param name="queryString"></param>
/// <param name="name"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <summary>
///
/// </summary>
/// <param name="queryString"></param>
/// <param name="name"></param>
/// <param name="value"></param>
/// <returns></returns>
public static string Upsert(IQueryCollection? queryString, string name, string value)
{
var collection = ToDictionary(queryString);
@@ -79,8 +73,6 @@ namespace ZelWiki.Library
foreach (var item in queryString)
{
//Technically, keys can be duplicated in a IQueryCollection but we do not
//support this. Use .Single() to throw exception if duplicates are found.
dictionary.Add(item.Key, item.Value.Single() ?? string.Empty);
}
@@ -98,13 +90,11 @@ namespace ZelWiki.Library
return dictionary;
}
// If the query string starts with '?', remove it
if (queryString.StartsWith('?'))
{
queryString = queryString.Substring(1);
}
// Split the query string into key-value pairs
var keyValuePairs = queryString.Split('&');
foreach (var kvp in keyValuePairs)
@@ -158,6 +148,7 @@ namespace ZelWiki.Library
{
queryString.Append('&');
}
queryString.Append($"{Uri.EscapeDataString(kvp.Key)}={Uri.EscapeDataString(kvp.Value)}");
}
@@ -176,7 +167,8 @@ namespace ZelWiki.Library
{
clone.Add(kvp.Key, kvp.Value);
}
return clone;
}
}
}
}