2020-06-29 04:44:35 +02:00
|
|
|
using System;
|
2017-12-03 15:35:52 +01:00
|
|
|
using System.Collections.Generic;
|
2019-04-26 01:13:17 +02:00
|
|
|
using System.Globalization;
|
2017-12-03 15:35:52 +01:00
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
namespace BTCPayServer
|
|
|
|
{
|
|
|
|
public class SearchString
|
|
|
|
{
|
2020-06-29 05:07:48 +02:00
|
|
|
readonly string _OriginalString;
|
2017-12-03 15:35:52 +01:00
|
|
|
public SearchString(string str)
|
|
|
|
{
|
|
|
|
str = str ?? string.Empty;
|
|
|
|
str = str.Trim();
|
|
|
|
_OriginalString = str.Trim();
|
|
|
|
TextSearch = _OriginalString;
|
2019-04-26 01:13:17 +02:00
|
|
|
var splitted = str.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
|
2017-12-03 15:35:52 +01:00
|
|
|
Filters
|
|
|
|
= splitted
|
2019-04-26 01:13:17 +02:00
|
|
|
.Select(t => t.Split(new char[] { ':' }, 2, StringSplitOptions.RemoveEmptyEntries))
|
2017-12-03 15:35:52 +01:00
|
|
|
.Where(kv => kv.Length == 2)
|
2019-04-26 01:13:17 +02:00
|
|
|
.Select(kv => new KeyValuePair<string, string>(kv[0].ToLowerInvariant().Trim(), kv[1]))
|
2018-04-26 04:01:59 +02:00
|
|
|
.ToMultiValueDictionary(o => o.Key, o => o.Value);
|
2017-12-03 15:35:52 +01:00
|
|
|
|
2019-04-26 01:13:17 +02:00
|
|
|
var val = splitted.FirstOrDefault(a => a?.IndexOf(':', StringComparison.OrdinalIgnoreCase) == -1);
|
|
|
|
if (val != null)
|
|
|
|
TextSearch = val.Trim();
|
|
|
|
else
|
|
|
|
TextSearch = "";
|
2017-12-03 15:35:52 +01:00
|
|
|
}
|
|
|
|
|
2019-04-26 01:13:17 +02:00
|
|
|
public string TextSearch { get; private set; }
|
|
|
|
|
2018-04-26 04:01:59 +02:00
|
|
|
public MultiValueDictionary<string, string> Filters { get; private set; }
|
2017-12-03 15:35:52 +01:00
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
return _OriginalString;
|
|
|
|
}
|
2019-04-26 01:13:17 +02:00
|
|
|
|
|
|
|
internal string[] GetFilterArray(string key)
|
|
|
|
{
|
|
|
|
return Filters.ContainsKey(key) ? Filters[key].ToArray() : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal bool? GetFilterBool(string key)
|
|
|
|
{
|
|
|
|
if (!Filters.ContainsKey(key))
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return bool.TryParse(Filters[key].First(), out var r) ?
|
|
|
|
r : (bool?)null;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal DateTimeOffset? GetFilterDate(string key, int timezoneOffset)
|
|
|
|
{
|
|
|
|
if (!Filters.ContainsKey(key))
|
|
|
|
return null;
|
|
|
|
|
|
|
|
var val = Filters[key].First();
|
2020-07-27 23:45:50 +02:00
|
|
|
|
|
|
|
// handle special string values
|
|
|
|
if (val == "-24h")
|
|
|
|
return DateTimeOffset.UtcNow.AddHours(-24).AddMinutes(timezoneOffset);
|
|
|
|
else if (val == "-3d")
|
|
|
|
return DateTimeOffset.UtcNow.AddDays(-3).AddMinutes(timezoneOffset);
|
|
|
|
else if (val == "-7d")
|
|
|
|
return DateTimeOffset.UtcNow.AddDays(-7).AddMinutes(timezoneOffset);
|
|
|
|
|
|
|
|
// default parsing logic
|
2019-04-26 01:13:17 +02:00
|
|
|
var success = DateTimeOffset.TryParse(val, null as IFormatProvider, DateTimeStyles.AssumeUniversal, out var r);
|
|
|
|
if (success)
|
|
|
|
{
|
|
|
|
r = r.AddMinutes(timezoneOffset);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2021-12-31 08:36:38 +01:00
|
|
|
|
|
|
|
internal bool ContainsFilter(string key) => Filters.ContainsKey(key);
|
2017-12-03 15:35:52 +01:00
|
|
|
}
|
|
|
|
}
|