From 32c0e83c8fba98bb50f2d2c8c6afc226adf4c602 Mon Sep 17 00:00:00 2001 From: Stephan Date: Mon, 22 Jan 2024 22:45:48 +0100 Subject: [PATCH] added tier prices --- BrickOwlSharp.Client/Inventory.cs | 3 + .../Json/TierPriceListConverter.cs | 59 +++++++++++++++++++ BrickOwlSharp.Client/TierPrice.cs | 12 ++++ 3 files changed, 74 insertions(+) create mode 100644 BrickOwlSharp.Client/Json/TierPriceListConverter.cs create mode 100644 BrickOwlSharp.Client/TierPrice.cs diff --git a/BrickOwlSharp.Client/Inventory.cs b/BrickOwlSharp.Client/Inventory.cs index 8fd511c..7bd1abd 100644 --- a/BrickOwlSharp.Client/Inventory.cs +++ b/BrickOwlSharp.Client/Inventory.cs @@ -89,5 +89,8 @@ public class Inventory [JsonPropertyName("type")] public string Type{ get; set; } + + [JsonPropertyName("tier_price"), JsonConverter(typeof(TierPriceListConverter))] + public List TierPrices { get; set; } } } diff --git a/BrickOwlSharp.Client/Json/TierPriceListConverter.cs b/BrickOwlSharp.Client/Json/TierPriceListConverter.cs new file mode 100644 index 0000000..82d8931 --- /dev/null +++ b/BrickOwlSharp.Client/Json/TierPriceListConverter.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.Json.Serialization; +using System.Text.Json; + +namespace BrickOwlSharp.Client.Json +{ + internal class TierPriceListConverter : JsonConverter> + { + public override List Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + var tierPrices = new List(); + + if (reader.TokenType != JsonTokenType.StartArray) + { + throw new JsonException(); + } + + while (reader.Read()) + { + if (reader.TokenType == JsonTokenType.EndArray) + { + break; + } + + if (reader.TokenType != JsonTokenType.StartArray) + { + throw new JsonException(); + } + + reader.Read(); + var threshold = reader.GetString(); + reader.Read(); + var price = reader.GetString(); + tierPrices.Add(new TierPrice + { + Quantity = int.Parse(threshold), + Price = decimal.Parse(price) + }); + + reader.Read(); + if (reader.TokenType != JsonTokenType.EndArray) + { + throw new JsonException(); + } + } + + return tierPrices; + } + + + public override void Write(Utf8JsonWriter writer, List value, JsonSerializerOptions options) + { + throw new NotImplementedException(); + } + } +} + diff --git a/BrickOwlSharp.Client/TierPrice.cs b/BrickOwlSharp.Client/TierPrice.cs new file mode 100644 index 0000000..796933d --- /dev/null +++ b/BrickOwlSharp.Client/TierPrice.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace BrickOwlSharp.Client +{ + public class TierPrice + { + public int Quantity { get; set; } + public decimal Price { get; set; } + } +}