Adds query filter implementations
This commit is contained in:
parent
863f708ed1
commit
028bfeb776
33
src/Utils/QueryFilter/AttributedQueryFilter.cs
Normal file
33
src/Utils/QueryFilter/AttributedQueryFilter.cs
Normal file
|
@ -0,0 +1,33 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
|
||||
namespace JpnCardsPokemonSdk.Utils.QueryFilter;
|
||||
|
||||
public abstract class AttributedQueryFilter : IQueryFilter
|
||||
{
|
||||
public string BuildQueryString()
|
||||
{
|
||||
var filterBucket = new List<KeyValuePair<string, string>>();
|
||||
|
||||
var properties = GetType().GetProperties();
|
||||
foreach (var propertyInfo in properties)
|
||||
{
|
||||
var attribute = propertyInfo.GetCustomAttribute<QueryFilterAttribute>();
|
||||
|
||||
if (attribute == null)
|
||||
continue;
|
||||
|
||||
var paramName = attribute.ParamName ?? propertyInfo.Name;
|
||||
var value = propertyInfo.GetValue(this).ToString();
|
||||
if (!string.IsNullOrEmpty(value)) filterBucket.Add(new KeyValuePair<string, string>(paramName, value));
|
||||
}
|
||||
|
||||
var filterBuilder = new StringBuilder();
|
||||
foreach (var keyValuePair in filterBucket)
|
||||
filterBuilder.Append($"&{keyValuePair.Key}={WebUtility.UrlEncode(keyValuePair.Value)}");
|
||||
|
||||
return filterBuilder.ToString().TrimStart('&');
|
||||
}
|
||||
}
|
24
src/Utils/QueryFilter/CardQueryFilter.cs
Normal file
24
src/Utils/QueryFilter/CardQueryFilter.cs
Normal file
|
@ -0,0 +1,24 @@
|
|||
namespace JpnCardsPokemonSdk.Utils.QueryFilter;
|
||||
|
||||
public class CardQueryFilter : AttributedQueryFilter
|
||||
{
|
||||
[QueryFilter("id")] public int? Id { get; set; }
|
||||
|
||||
[QueryFilter("name")] public string? Name { get; set; }
|
||||
|
||||
[QueryFilter("set_id")] public int? SetId { get; set; }
|
||||
|
||||
[QueryFilter("illustrator")] public string? Artist { get; set; }
|
||||
|
||||
[QueryFilter("p_no")] public string? PrintedNumber { get; set; }
|
||||
|
||||
[QueryFilter("uuid")] public int? Uuid { get; set; }
|
||||
|
||||
[QueryFilter("rarity")] public string? Rarity { get; set; }
|
||||
|
||||
[QueryFilter("subtype")] public string? Subtype { get; set; }
|
||||
|
||||
[QueryFilter("type")] public string? Type { get; set; }
|
||||
|
||||
[QueryFilter("set_code")] public string? SetCode { get; set; }
|
||||
}
|
6
src/Utils/QueryFilter/IQueryFilter.cs
Normal file
6
src/Utils/QueryFilter/IQueryFilter.cs
Normal file
|
@ -0,0 +1,6 @@
|
|||
namespace JpnCardsPokemonSdk.Utils.QueryFilter;
|
||||
|
||||
public interface IQueryFilter
|
||||
{
|
||||
string BuildQueryString();
|
||||
}
|
18
src/Utils/QueryFilter/QueryFilterAttribute.cs
Normal file
18
src/Utils/QueryFilter/QueryFilterAttribute.cs
Normal file
|
@ -0,0 +1,18 @@
|
|||
using System;
|
||||
|
||||
namespace JpnCardsPokemonSdk.Utils.QueryFilter;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Property)]
|
||||
public class QueryFilterAttribute : Attribute
|
||||
{
|
||||
public QueryFilterAttribute(string? paramName)
|
||||
{
|
||||
ParamName = paramName;
|
||||
}
|
||||
|
||||
public QueryFilterAttribute() : this(null)
|
||||
{
|
||||
}
|
||||
|
||||
public string? ParamName { get; set; }
|
||||
}
|
Loading…
Reference in a new issue