Files
WatchIt/WatchIt.Common/WatchIt.Common.Query/QueryParameters.cs

144 lines
3.3 KiB
C#
Raw Normal View History

2024-04-27 22:36:16 +02:00
using System.Reflection;
2024-07-03 22:18:32 +02:00
using System.Text;
2024-04-27 22:36:16 +02:00
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Mvc;
2024-07-03 22:18:32 +02:00
namespace WatchIt.Common.Query;
2024-04-27 22:36:16 +02:00
2024-07-03 22:18:32 +02:00
public abstract class QueryParameters
2024-04-27 22:36:16 +02:00
{
#region PROPERTIES
[FromQuery(Name = "order_by")]
public string? OrderBy { get; set; }
[FromQuery(Name = "order")]
public string? Order { get; set; }
[FromQuery(Name = "first")]
public int? First { get; set; }
[FromQuery(Name = "after")]
public int? After { get; set; }
#endregion
2024-07-03 22:18:32 +02:00
#region PUBLIC METHODS
public override string ToString()
2024-04-27 22:36:16 +02:00
{
2024-07-03 22:18:32 +02:00
List<string> queries = new List<string>();
PropertyInfo[] properties = this.GetType().GetProperties();
foreach (PropertyInfo property in properties)
2024-04-27 22:36:16 +02:00
{
2024-07-03 22:18:32 +02:00
object? value = property.GetValue(this);
FromQueryAttribute? attribute = property.GetCustomAttributes<FromQueryAttribute>(true).FirstOrDefault();
if (value is not null && attribute is not null)
2024-04-27 22:36:16 +02:00
{
2024-07-03 22:18:32 +02:00
string query = $"{attribute.Name}={value}";
queries.Add(query);
2024-04-27 22:36:16 +02:00
}
}
2024-07-03 22:18:32 +02:00
return $"?{string.Join('&', queries)}";
2024-04-27 22:36:16 +02:00
}
#endregion
2024-07-03 22:18:32 +02:00
2024-04-27 22:36:16 +02:00
#region PRIVATE METHODS
2024-09-29 23:00:32 +02:00
protected static bool Test<T>(T? property, T? query) =>
2024-07-03 22:18:32 +02:00
(
query is null
||
2024-09-29 23:00:32 +02:00
(
property is not null
&&
property.Equals(query)
)
2024-07-03 22:18:32 +02:00
);
2024-09-29 23:00:32 +02:00
protected static bool TestStringWithRegex(string? property, string? regexQuery) =>
2024-04-27 22:36:16 +02:00
(
string.IsNullOrEmpty(regexQuery)
||
(
!string.IsNullOrEmpty(property)
&&
2024-09-29 15:47:11 +02:00
Regex.IsMatch(property, regexQuery, RegexOptions.IgnoreCase)
2024-04-27 22:36:16 +02:00
)
);
2024-07-03 22:18:32 +02:00
protected static bool TestComparable(IComparable? property, IComparable? exact, IComparable? from, IComparable? to) =>
2024-04-27 22:36:16 +02:00
(
(
exact is null
||
(
property is not null
&&
property.CompareTo(exact) == 0
)
)
&&
(
from is null
||
(
property is not null
&&
property.CompareTo(from) > 0
)
)
&&
(
to is null
||
(
property is not null
&&
property.CompareTo(to) < 0
)
)
);
2024-07-03 22:18:32 +02:00
#endregion
}
2024-09-29 23:00:32 +02:00
public abstract class QueryParameters<T> : QueryParameters where T : IQueryOrderable<T>
2024-07-03 22:18:32 +02:00
{
#region PUBLIC METHODS
public abstract bool IsMeetingConditions(T item);
public IEnumerable<T> PrepareData(IEnumerable<T> data)
{
data = data.Where(IsMeetingConditions);
if (OrderBy is not null)
{
2024-09-29 23:00:32 +02:00
if (T.OrderableProperties.TryGetValue(OrderBy, out Func<T, IComparable>? orderFunc))
2024-07-03 22:18:32 +02:00
{
2024-09-29 23:00:32 +02:00
data = Order == "asc" ? data.OrderBy(orderFunc) : data.OrderByDescending(orderFunc);
2024-07-03 22:18:32 +02:00
}
}
if (After is not null)
{
data = data.Skip(After.Value);
}
if (First is not null)
{
data = data.Take(First.Value);
}
return data;
}
2024-04-27 22:36:16 +02:00
#endregion
}