83 lines
2.5 KiB
C#
83 lines
2.5 KiB
C#
using InnovEnergy.Lib.Utils;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Serialization;
|
|
|
|
namespace InnovEnergy.API.DataModel;
|
|
|
|
public partial record Data
|
|
{
|
|
private static readonly JsonSerializerSettings PersistenceJsonSettings = new JsonSerializerSettings
|
|
{
|
|
Formatting = Formatting.Indented,
|
|
NullValueHandling = NullValueHandling.Include,
|
|
ContractResolver = new CamelCasePropertyNamesContractResolver(),
|
|
TypeNameHandling = TypeNameHandling.Auto
|
|
};
|
|
|
|
public static readonly JsonSerializerSettings WireFormatJsonSettings = new JsonSerializerSettings
|
|
{
|
|
Formatting = Formatting.Indented,
|
|
NullValueHandling = NullValueHandling.Ignore,
|
|
ContractResolver = new CamelCasePropertyNamesContractResolver(),
|
|
TypeNameHandling = TypeNameHandling.None
|
|
};
|
|
|
|
public static Data Load() => GetLastVersion().Apply(Load);
|
|
public static String LoadJson() => GetLastVersion().Apply(LoadJson);
|
|
|
|
public static Data Load(Int32 sequenceNb)
|
|
{
|
|
var json = LoadJson(sequenceNb);
|
|
return JsonConvert.DeserializeObject<Data>(json, PersistenceJsonSettings)!;
|
|
}
|
|
|
|
public static String LoadJson(Int32 sequenceNb)
|
|
{
|
|
var jsonFile = System.IO.Path.Combine(Program.RootDir, sequenceNb + ".json");
|
|
|
|
Console.WriteLine($"loaded {jsonFile}");
|
|
|
|
return jsonFile.Apply(File.ReadAllText);
|
|
}
|
|
|
|
public void Save(String editAction, String user)
|
|
{
|
|
var version = Edit.Version + 1;
|
|
|
|
Edit = new Edit(editAction, user, version);
|
|
|
|
var path = System.IO.Path.Combine(Program.RootDir, version + ".json");
|
|
var json = SerializeJson();
|
|
|
|
File.WriteAllText(path, json);
|
|
}
|
|
|
|
public String SerializeJson()
|
|
{
|
|
return JsonConvert.SerializeObject(this, PersistenceJsonSettings);
|
|
}
|
|
|
|
private static String Base64Hash(DataElement element)
|
|
{
|
|
return element
|
|
.GetHashCode()
|
|
.Apply(BitConverter.GetBytes)
|
|
.Apply(Convert.ToBase64String)
|
|
.Apply(TrimEq);
|
|
}
|
|
|
|
private static String TrimEq(String h) => h.TrimEnd('=');
|
|
|
|
private static Int32 GetLastVersion()
|
|
{
|
|
return Directory
|
|
.EnumerateFiles(Program.RootDir, "*.json")
|
|
.Select(System.IO.Path.GetFileNameWithoutExtension)
|
|
.Where(f => f!.All(Char.IsDigit))
|
|
.OrderBy(f => f)
|
|
.Last()
|
|
.Apply(Int32.Parse!);
|
|
}
|
|
|
|
//public override String ToString() => JsonConvert.SerializeObject(this, WireFormatJsonSettings);
|
|
} |