Billing: Add BillingData and JSON schema validation
This commit is contained in:
44
Elwig/Helpers/Billing/BillingData.cs
Normal file
44
Elwig/Helpers/Billing/BillingData.cs
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user