diff --git a/Elwig/App.xaml.cs b/Elwig/App.xaml.cs index f67678c..eeb3805 100644 --- a/Elwig/App.xaml.cs +++ b/Elwig/App.xaml.cs @@ -16,6 +16,7 @@ using Elwig.Helpers.Printing; using Elwig.Windows; using Elwig.Dialogs; using System.Threading.Tasks; +using Elwig.Helpers.Billing; namespace Elwig { public partial class App : Application { @@ -104,6 +105,7 @@ namespace Elwig { Utils.RunBackground("HTML Initialization", () => Html.Init(PrintingReadyChanged)); Utils.RunBackground("PDF Initialization", () => Pdf.Init(PrintingReadyChanged)); + Utils.RunBackground("JSON Schema Initialization", BillingData.Init); var list = new List(); foreach (var s in Config.Scales) { diff --git a/Elwig/Elwig.csproj b/Elwig/Elwig.csproj index 32a5d30..d574c3a 100644 --- a/Elwig/Elwig.csproj +++ b/Elwig/Elwig.csproj @@ -14,6 +14,7 @@ + @@ -28,6 +29,7 @@ + diff --git a/Elwig/Helpers/Billing/BillingData.cs b/Elwig/Helpers/Billing/BillingData.cs new file mode 100644 index 0000000..986e59d --- /dev/null +++ b/Elwig/Helpers/Billing/BillingData.cs @@ -0,0 +1,44 @@ +using Newtonsoft.Json; +using NJsonSchema; +using System; +using System.Reflection; +using System.Text.Json.Nodes; +using System.Threading.Tasks; + +namespace Elwig.Helpers.Billing { + public class BillingData { + + public static JsonSchema? Schema { get; private set; } + + public static async Task Init() { + var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Elwig.Schemas.payment_variant.json"); + Schema = await JsonSchema.FromJsonAsync(stream ?? throw new ArgumentException("JSON schema not found")); + } + + private JsonObject Data; + + public BillingData(JsonObject data) { + Data = data; + + } + + public static JsonObject ParseJson(string json) { + if (Schema == null) throw new InvalidOperationException("Schema has to be initialized first"); + try { + var errors = Schema.Validate(json); + if (errors.Count != 0) throw new ArgumentException("Invalid JSON data"); + return JsonNode.Parse(json)?.AsObject() ?? throw new ArgumentException("Invalid JSON data"); + } catch (JsonReaderException) { + throw new ArgumentException("Invalid JSON data"); + } + } + + public static BillingData FromJson(string json) { + return new(ParseJson(json)); + } + + public decimal CalculatePrice(string sortId, string? attrid, string qualId, bool gebunden, double oe, double kmw, bool minQuw) { + return 0m; + } + } +} diff --git a/Elwig/Schemas/payment_variant.json b/Elwig/Schemas/payment_variant.json new file mode 100644 index 0000000..af7e86b --- /dev/null +++ b/Elwig/Schemas/payment_variant.json @@ -0,0 +1,135 @@ +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "type": "object", + "required": ["mode"], + "anyOf": [{ + "required": ["version", "payment", "curves"], + "additionalProperties": false, + "properties": { + "mode": {"enum": ["elwig"]}, + "version": {"enum": [1]}, + "payment": { + "type": ["number", "string", "object"], + "pattern": "^curve:[0-9]+$", + "additionalProperties": false, + "properties": { + "default": { + "type": ["number", "string"], + "pattern": "^curve:[0-9]+$" + } + }, + "patternProperties": { + "^[A-Z]+$": { + "type": ["number", "string"], + "pattern": "^curve:[0-9]+$" + } + } + }, + "quality": { + "type": "object", + "additionalProperties": false, + "patternProperties": { + "^[A-Z]{3}$": { + "type": ["number", "string", "object"], + "pattern": "^curve:[0-9]+$", + "additionalProperties": false, + "properties": { + "default": { + "type": ["number", "string"], + "pattern": "^curve:[0-9]+$" + } + }, + "patternProperties": { + "^[A-Z]+$": { + "type": ["number", "string"], + "pattern": "^curve:[0-9]+$" + } + } + } + } + }, + "curves": { + "type": "array", + "items": {"$ref": "#/definitions/curve"} + } + } + }, { + "properties": { + "mode": {"enum": ["wgmaster"]}, + "AuszahlungSorten": { + "type": "object", + "additionalProperties": false, + "required": ["Kurven"], + "properties": { + "Kurven": { + "type": "array", + "items": { + "type": "object", + "additionalProperties": false, + "minProperties": 1, + "patternProperties": { + "^[0-9]+(\\.[0-9]+)?oe$": {"type": "number"} + } + } + } + }, + "patternProperties": { + "^[A-Z]{2}$": { + "type": "object", + "additionalProperties": false, + "patternProperties": { + "^[A-Z]*$": { + "type": "object", + "additionalProperties": false, + "properties": { + "Gebunden": { + "type": "integer", + "minimum": 0 + }, + "NichtGebunden": { + "type": "integer", + "minimum": 0 + } + } + } + } + } + } + } + } + }], + "definitions": { + "curve": { + "type": "object", + "required": ["id", "mode", "data"], + "additionalProperties": false, + "properties": { + "id": { + "type": "integer", + "minimum": 0 + }, + "mode": {"enum": ["oe", "kmw"]}, + "data": { + "anyOf": [ + {"type": "number" }, + {"$ref": "#/definitions/curve_data"} + ] + }, + "geb": { + "anyOf": [ + {"type": "number"}, + {"$ref": "#/definitions/curve_data"} + ] + } + } + }, + "curve_data": { + "type": "object", + "additionalProperties": false, + "minProperties": 1, + "patternProperties": { + "^([0-9]+(\\.[0-9]+)?)(oe|kmw)$": {"type": "number"} + } + } + } +} diff --git a/Elwig/Windows/ChartWindow.xaml.cs b/Elwig/Windows/ChartWindow.xaml.cs index 1f2e2ff..1466b99 100644 --- a/Elwig/Windows/ChartWindow.xaml.cs +++ b/Elwig/Windows/ChartWindow.xaml.cs @@ -63,8 +63,8 @@ namespace Elwig.Windows { private static JsonObject? ParseData(PaymentVar variant) { try { - return JsonNode.Parse(variant.Data)?.AsObject(); - } catch (JsonException) { + return BillingData.ParseJson(variant.Data); + } catch (ArgumentException) { return null; } }