Skip to content

Commit

Permalink
added tier prices
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanstapel committed Jan 22, 2024
1 parent 9f9f8c3 commit 32c0e83
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
3 changes: 3 additions & 0 deletions BrickOwlSharp.Client/Inventory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,8 @@ public class Inventory

[JsonPropertyName("type")]
public string Type{ get; set; }

[JsonPropertyName("tier_price"), JsonConverter(typeof(TierPriceListConverter))]
public List<TierPrice> TierPrices { get; set; }
}
}
59 changes: 59 additions & 0 deletions BrickOwlSharp.Client/Json/TierPriceListConverter.cs
Original file line number Diff line number Diff line change
@@ -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<List<TierPrice>>
{
public override List<TierPrice> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var tierPrices = new List<TierPrice>();

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<TierPrice> value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}
}

12 changes: 12 additions & 0 deletions BrickOwlSharp.Client/TierPrice.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}

0 comments on commit 32c0e83

Please sign in to comment.