From 028bfeb7769be1133d671841d7449b8f3bdb80d1 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Wed, 1 Mar 2023 17:11:37 +0100 Subject: [PATCH] Adds query filter implementations --- .../QueryFilter/AttributedQueryFilter.cs | 33 +++++++++++++++++++ src/Utils/QueryFilter/CardQueryFilter.cs | 24 ++++++++++++++ src/Utils/QueryFilter/IQueryFilter.cs | 6 ++++ src/Utils/QueryFilter/QueryFilterAttribute.cs | 18 ++++++++++ 4 files changed, 81 insertions(+) create mode 100644 src/Utils/QueryFilter/AttributedQueryFilter.cs create mode 100644 src/Utils/QueryFilter/CardQueryFilter.cs create mode 100644 src/Utils/QueryFilter/IQueryFilter.cs create mode 100644 src/Utils/QueryFilter/QueryFilterAttribute.cs diff --git a/src/Utils/QueryFilter/AttributedQueryFilter.cs b/src/Utils/QueryFilter/AttributedQueryFilter.cs new file mode 100644 index 0000000..1429352 --- /dev/null +++ b/src/Utils/QueryFilter/AttributedQueryFilter.cs @@ -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>(); + + var properties = GetType().GetProperties(); + foreach (var propertyInfo in properties) + { + var attribute = propertyInfo.GetCustomAttribute(); + + if (attribute == null) + continue; + + var paramName = attribute.ParamName ?? propertyInfo.Name; + var value = propertyInfo.GetValue(this).ToString(); + if (!string.IsNullOrEmpty(value)) filterBucket.Add(new KeyValuePair(paramName, value)); + } + + var filterBuilder = new StringBuilder(); + foreach (var keyValuePair in filterBucket) + filterBuilder.Append($"&{keyValuePair.Key}={WebUtility.UrlEncode(keyValuePair.Value)}"); + + return filterBuilder.ToString().TrimStart('&'); + } +} \ No newline at end of file diff --git a/src/Utils/QueryFilter/CardQueryFilter.cs b/src/Utils/QueryFilter/CardQueryFilter.cs new file mode 100644 index 0000000..bb8a5d7 --- /dev/null +++ b/src/Utils/QueryFilter/CardQueryFilter.cs @@ -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; } +} \ No newline at end of file diff --git a/src/Utils/QueryFilter/IQueryFilter.cs b/src/Utils/QueryFilter/IQueryFilter.cs new file mode 100644 index 0000000..4ae3254 --- /dev/null +++ b/src/Utils/QueryFilter/IQueryFilter.cs @@ -0,0 +1,6 @@ +namespace JpnCardsPokemonSdk.Utils.QueryFilter; + +public interface IQueryFilter +{ + string BuildQueryString(); +} \ No newline at end of file diff --git a/src/Utils/QueryFilter/QueryFilterAttribute.cs b/src/Utils/QueryFilter/QueryFilterAttribute.cs new file mode 100644 index 0000000..160f039 --- /dev/null +++ b/src/Utils/QueryFilter/QueryFilterAttribute.cs @@ -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; } +} \ No newline at end of file