Implemented the JSON export
This commit is contained in:
parent
c2c8a36ad2
commit
cdffce9bbd
@ -1,4 +1,6 @@
|
|||||||
using Microsoft.EntityFrameworkCore;
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace DataModel.Database.Common;
|
namespace DataModel.Database.Common;
|
||||||
|
|
||||||
@ -63,4 +65,97 @@ public sealed class DataContext : DbContext, IDataContext
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#region Export and import data structures
|
||||||
|
|
||||||
|
private readonly record struct JsonData(
|
||||||
|
IEnumerable<JsonSetting> Settings,
|
||||||
|
IEnumerable<JsonSection> Sections,
|
||||||
|
IEnumerable<JsonTextElement> TextElements,
|
||||||
|
IEnumerable<JsonTranslation> Translations
|
||||||
|
);
|
||||||
|
|
||||||
|
internal readonly record struct JsonUniqueId(string Code, Guid UniqueId, string Prefix = "")
|
||||||
|
{
|
||||||
|
public override string ToString() => string.IsNullOrWhiteSpace(this.Prefix) ? $"{this.Code}::{this.UniqueId}" : $"{this.Prefix}::{this.Code}::{this.UniqueId}";
|
||||||
|
|
||||||
|
public static implicit operator string(JsonUniqueId id) => id.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private sealed class JsonUniqueIdConverter : JsonConverter<JsonUniqueId>
|
||||||
|
{
|
||||||
|
public override JsonUniqueId Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
var json = reader.GetString();
|
||||||
|
var parts = json?.Split("::");
|
||||||
|
return parts?.Length switch
|
||||||
|
{
|
||||||
|
2 => new JsonUniqueId(parts[0], Guid.Parse(parts[1])),
|
||||||
|
3 => new JsonUniqueId(parts[1], Guid.Parse(parts[2]), parts[0]),
|
||||||
|
|
||||||
|
_ => throw new JsonException($"Invalid format of JsonUniqueId: {json}")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Write(Utf8JsonWriter writer, JsonUniqueId value, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
writer.WriteStringValue(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal readonly record struct JsonSetting(
|
||||||
|
JsonUniqueId UniqueId,
|
||||||
|
string Code,
|
||||||
|
string TextValue,
|
||||||
|
int IntegerValue,
|
||||||
|
bool BoolValue,
|
||||||
|
Guid GuidValue
|
||||||
|
);
|
||||||
|
|
||||||
|
internal readonly record struct JsonSection(
|
||||||
|
JsonUniqueId UniqueId,
|
||||||
|
string Name,
|
||||||
|
string DataKey,
|
||||||
|
int Depth,
|
||||||
|
JsonUniqueId ParentUniqueId,
|
||||||
|
List<JsonUniqueId> TextElements
|
||||||
|
);
|
||||||
|
|
||||||
|
internal readonly record struct JsonTextElement(
|
||||||
|
JsonUniqueId UniqueId,
|
||||||
|
string Code,
|
||||||
|
string Name,
|
||||||
|
bool IsMultiLine,
|
||||||
|
JsonUniqueId SectionUniqueId,
|
||||||
|
List<JsonUniqueId> Translations
|
||||||
|
);
|
||||||
|
|
||||||
|
internal readonly record struct JsonTranslation(
|
||||||
|
JsonUniqueId UniqueId,
|
||||||
|
string Culture,
|
||||||
|
string Text,
|
||||||
|
bool TranslateManual,
|
||||||
|
JsonUniqueId TextElementUniqueId
|
||||||
|
);
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
public async Task ExportAsync(string path)
|
||||||
|
{
|
||||||
|
var jsonSettings = new JsonSerializerOptions
|
||||||
|
{
|
||||||
|
WriteIndented = true,
|
||||||
|
Converters = { new JsonUniqueIdConverter() },
|
||||||
|
};
|
||||||
|
|
||||||
|
await using var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
|
||||||
|
await JsonSerializer.SerializeAsync(fileStream,
|
||||||
|
new JsonData
|
||||||
|
{
|
||||||
|
Settings = this.Settings.Select(n => n.ToJsonSetting()),
|
||||||
|
Sections = this.Sections.Select(n => n.ToJsonSection()),
|
||||||
|
TextElements = this.TextElements.Select(n => n.ToJsonTextElement()),
|
||||||
|
Translations = this.Translations.Select(n => n.ToJsonTranslation()),
|
||||||
|
}, jsonSettings);
|
||||||
|
}
|
||||||
}
|
}
|
@ -11,4 +11,6 @@ public interface IDataContext
|
|||||||
public DbSet<TextElement> TextElements { get; set; }
|
public DbSet<TextElement> TextElements { get; set; }
|
||||||
|
|
||||||
public DbSet<Translation> Translations { get; set; }
|
public DbSet<Translation> Translations { get; set; }
|
||||||
|
|
||||||
|
public Task ExportAsync(string path);
|
||||||
}
|
}
|
@ -1,4 +1,5 @@
|
|||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using DataModel.Database.Common;
|
||||||
|
|
||||||
namespace DataModel.Database;
|
namespace DataModel.Database;
|
||||||
|
|
||||||
@ -18,4 +19,16 @@ public sealed class Section
|
|||||||
public Section? Parent { get; set; }
|
public Section? Parent { get; set; }
|
||||||
|
|
||||||
public List<TextElement> TextElements { get; set; } = new();
|
public List<TextElement> TextElements { get; set; } = new();
|
||||||
|
|
||||||
|
internal DataContext.JsonUniqueId JsonUniqueId => new(this.DataKey, this.UniqueId, "Sec");
|
||||||
|
|
||||||
|
internal DataContext.JsonSection ToJsonSection() => new()
|
||||||
|
{
|
||||||
|
UniqueId = this.JsonUniqueId,
|
||||||
|
Name = this.Name,
|
||||||
|
DataKey = this.DataKey,
|
||||||
|
Depth = this.Depth,
|
||||||
|
ParentUniqueId = this.Parent?.JsonUniqueId ?? new DataContext.JsonUniqueId("null", Guid.Empty, "Sec"),
|
||||||
|
TextElements = this.TextElements.Select(n => n.JsonUniqueId).ToList()
|
||||||
|
};
|
||||||
}
|
}
|
@ -1,4 +1,5 @@
|
|||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using DataModel.Database.Common;
|
||||||
|
|
||||||
namespace DataModel.Database;
|
namespace DataModel.Database;
|
||||||
|
|
||||||
@ -18,4 +19,16 @@ public sealed class Setting
|
|||||||
public int IntegerValue { get; set; }
|
public int IntegerValue { get; set; }
|
||||||
|
|
||||||
public Guid GuidValue { get; set; }
|
public Guid GuidValue { get; set; }
|
||||||
|
|
||||||
|
internal DataContext.JsonUniqueId JsonUniqueId => new(this.Code, this.UniqueId, "Set");
|
||||||
|
|
||||||
|
internal DataContext.JsonSetting ToJsonSetting() => new()
|
||||||
|
{
|
||||||
|
UniqueId = this.JsonUniqueId,
|
||||||
|
Code = this.Code,
|
||||||
|
BoolValue = this.BoolValue,
|
||||||
|
GuidValue = this.GuidValue,
|
||||||
|
IntegerValue = this.IntegerValue,
|
||||||
|
TextValue = this.TextValue,
|
||||||
|
};
|
||||||
}
|
}
|
@ -1,4 +1,5 @@
|
|||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using DataModel.Database.Common;
|
||||||
|
|
||||||
namespace DataModel.Database;
|
namespace DataModel.Database;
|
||||||
|
|
||||||
@ -18,4 +19,16 @@ public sealed class TextElement
|
|||||||
public Section Section { get; set; }
|
public Section Section { get; set; }
|
||||||
|
|
||||||
public List<Translation> Translations { get; set; } = new();
|
public List<Translation> Translations { get; set; } = new();
|
||||||
|
|
||||||
|
internal DataContext.JsonUniqueId JsonUniqueId => new(this.Code, this.UniqueId, "TXT");
|
||||||
|
|
||||||
|
internal DataContext.JsonTextElement ToJsonTextElement() => new()
|
||||||
|
{
|
||||||
|
UniqueId = this.JsonUniqueId,
|
||||||
|
Code = this.Code,
|
||||||
|
Name = this.Name,
|
||||||
|
IsMultiLine = this.IsMultiLine,
|
||||||
|
SectionUniqueId = this.Section.JsonUniqueId,
|
||||||
|
Translations = this.Translations.Select(n => n.JsonUniqueId).ToList(),
|
||||||
|
};
|
||||||
}
|
}
|
@ -1,4 +1,6 @@
|
|||||||
namespace DataModel.Database;
|
using DataModel.Database.Common;
|
||||||
|
|
||||||
|
namespace DataModel.Database;
|
||||||
|
|
||||||
public sealed class Translation
|
public sealed class Translation
|
||||||
{
|
{
|
||||||
@ -13,4 +15,15 @@ public sealed class Translation
|
|||||||
public string Text { get; set; } = string.Empty;
|
public string Text { get; set; } = string.Empty;
|
||||||
|
|
||||||
public bool TranslateManual { get; set; } = false;
|
public bool TranslateManual { get; set; } = false;
|
||||||
|
|
||||||
|
internal DataContext.JsonUniqueId JsonUniqueId => new($"{this.TextElement.Code}::{this.Culture}", this.UniqueId, "Trans");
|
||||||
|
|
||||||
|
internal DataContext.JsonTranslation ToJsonTranslation() => new()
|
||||||
|
{
|
||||||
|
UniqueId = this.JsonUniqueId,
|
||||||
|
Culture = this.Culture,
|
||||||
|
TranslateManual = this.TranslateManual,
|
||||||
|
TextElementUniqueId = this.TextElement.JsonUniqueId,
|
||||||
|
Text = this.Text,
|
||||||
|
};
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user