Adds query filter implementations

This commit is contained in:
Serraniel 2023-03-01 17:11:37 +01:00
parent 863f708ed1
commit 028bfeb776
Signed by: Serraniel
GPG key ID: 3690B4E7364525D3
4 changed files with 81 additions and 0 deletions

View 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('&');
}
}

View 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; }
}

View file

@ -0,0 +1,6 @@
namespace JpnCardsPokemonSdk.Utils.QueryFilter;
public interface IQueryFilter
{
string BuildQueryString();
}

View 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; }
}