From fe8bfe7ef06c9ba31849441e60cb69c149f35a0f Mon Sep 17 00:00:00 2001 From: retr0volkov <68907954+retr0volkov@users.noreply.github.com> Date: Thu, 10 Jul 2025 10:57:42 +0300 Subject: [PATCH] JSON export added --- Models/Equipment.cs | 7 +++ Models/Image.cs | 4 ++ Models/Inspection.cs | 4 ++ Models/Point.cs | 2 + Models/Property.cs | 9 +++- Utils/DataSerialization.cs | 51 ++++++++++++++++++ ViewModels/Basic/PointViewModel.cs | 2 +- ViewModels/MainWindowViewModel.cs | 31 ++++++++++- Views/EditEquipmentDialog.axaml | 2 +- Views/MainWindow.axaml | 4 +- obj/Debug/net8.0/Avalonia/resources | Bin 343354 -> 343491 bytes obj/Debug/net8.0/GazpromApp.AssemblyInfo.cs | 2 +- .../GazpromApp.AssemblyInfoInputs.cache | 2 +- 13 files changed, 112 insertions(+), 8 deletions(-) create mode 100644 Utils/DataSerialization.cs diff --git a/Models/Equipment.cs b/Models/Equipment.cs index 242074e..a98732a 100644 --- a/Models/Equipment.cs +++ b/Models/Equipment.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; +using System.Text.Json.Serialization; namespace GazpromApp.Models; @@ -8,18 +9,24 @@ public class Equipment { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] + [JsonIgnore] public int Id { get; set; } [Required] public string? Name { get; set; } = string.Empty; [Required] public string? Description { get; set; } = string.Empty; [Required] + + [JsonIgnore] public int PointId { get; set; } + [JsonIgnore] public int ImageId { get; set; } + [JsonIgnore] public List InspectionIds { get; set; } = []; // EF stuff public Image? Image { get; set; } + [JsonIgnore] public Point? Point { get; set; } public List Properties { get; set; } = []; public List Inspections { get; set; } = []; diff --git a/Models/Image.cs b/Models/Image.cs index b12933a..2a2b5cf 100644 --- a/Models/Image.cs +++ b/Models/Image.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; +using System.Text.Json.Serialization; namespace GazpromApp.Models; public class Image @@ -8,10 +9,13 @@ public class Image [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] + [JsonIgnore] public int Id { get; set; } [Required] public byte[] Bytes { get; set; } = []; // EF stuff + [JsonIgnore] public List Points { get; set; } = []; + [JsonIgnore] public List Equipment { get; set; } = []; } \ No newline at end of file diff --git a/Models/Inspection.cs b/Models/Inspection.cs index f4fe4cd..dcb40b3 100644 --- a/Models/Inspection.cs +++ b/Models/Inspection.cs @@ -1,6 +1,7 @@ using System; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; +using System.Text.Json.Serialization; namespace GazpromApp.Models; @@ -8,6 +9,7 @@ public class Inspection { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] + [JsonIgnore] public int Id { get; set; } [Required] public string Name { get; set; } = string.Empty; @@ -18,8 +20,10 @@ public class Inspection [Required] public float PeriodValue { get; set; } + [JsonIgnore] public int EquipmentId { get; set; } // EF stuff + [JsonIgnore] public Equipment? Equipment { get; } = new(); } \ No newline at end of file diff --git a/Models/Point.cs b/Models/Point.cs index f2c6b39..873ccdb 100644 --- a/Models/Point.cs +++ b/Models/Point.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; +using System.Text.Json.Serialization; namespace GazpromApp.Models; @@ -8,6 +9,7 @@ public class Point { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] + [JsonIgnore] public int Id { get; set; } [Required] public string? Name { get; set; } = string.Empty; diff --git a/Models/Property.cs b/Models/Property.cs index 8b72b0c..906e0b7 100644 --- a/Models/Property.cs +++ b/Models/Property.cs @@ -1,6 +1,8 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; +using System.Text.Json.Serialization; namespace GazpromApp.Models; @@ -8,14 +10,19 @@ public class Property { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] + [JsonIgnore] public int Id { get; set; } + [JsonIgnore] public int ParentId { get; set; } [Required] public string Name { get; set; } = string.Empty; public string Value { get; set; } = string.Empty; // EF stuff + [JsonIgnore] public Property? Parent { get; set; } public List Properties{ get; set; } = []; + [JsonIgnore] public List Points { get; set; } = []; + [JsonIgnore] public List Equipment { get; } = []; } \ No newline at end of file diff --git a/Utils/DataSerialization.cs b/Utils/DataSerialization.cs new file mode 100644 index 0000000..0513333 --- /dev/null +++ b/Utils/DataSerialization.cs @@ -0,0 +1,51 @@ +using System; +using System.Text.Json; +using GazpromApp.Models; + +namespace GazpromApp.Utils; + +public static class DataSerialization +{ + private static void SaveData(string path, string data) + { + System.IO.File.WriteAllText(path, data); + } + + private static string LoadData(string path) + { + return System.IO.File.ReadAllText(path); + } + + public static void SavePoint(Point point, string path) + { + SaveData(path, point.Serialize()); + } + + public static Point? LoadPoint(string path) + { + return Deserialization.Deserialize(LoadData(path)); + } +} + +public static class Serialization +{ + public static string Serialize(this Point point) + { + return JsonSerializer.Serialize(point); + } +} + +public static class Deserialization +{ + public static Point? Deserialize(string point) + { + try + { + return string.IsNullOrEmpty(point) ? JsonSerializer.Deserialize(point) : null; + } + catch + { + throw new ArgumentException("Ошибка расшивроки json файла"); + } + } +} \ No newline at end of file diff --git a/ViewModels/Basic/PointViewModel.cs b/ViewModels/Basic/PointViewModel.cs index d92bfd9..841df72 100644 --- a/ViewModels/Basic/PointViewModel.cs +++ b/ViewModels/Basic/PointViewModel.cs @@ -37,7 +37,6 @@ public partial class PointViewModel : ViewModelBase Properties = point.Properties; } - // ReSharper disable once UnusedMember.Global public Point GetPoint() { return new Point @@ -45,6 +44,7 @@ public partial class PointViewModel : ViewModelBase Name = Name, Description = Description, Equipment = Equipment ?? [], + Images = Images ?? [], Properties = Properties ?? [] }; } diff --git a/ViewModels/MainWindowViewModel.cs b/ViewModels/MainWindowViewModel.cs index e34b320..c34fade 100644 --- a/ViewModels/MainWindowViewModel.cs +++ b/ViewModels/MainWindowViewModel.cs @@ -1,17 +1,22 @@ -using System.Collections.ObjectModel; +using System; +using System.IO; +using System.Collections.ObjectModel; using System.Threading.Tasks; +using System.Web; using Avalonia.Controls; +using Avalonia.Platform.Storage; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Messaging; using GazpromApp.Messages; using GazpromApp.Models; +using GazpromApp.Utils; namespace GazpromApp.ViewModels; public partial class MainWindowViewModel : ViewModelBase { - public ObservableCollection Points { get; } = []; + public ObservableCollection Points { get; } [RelayCommand] private async Task AddPoint() @@ -38,6 +43,28 @@ public partial class MainWindowViewModel : ViewModelBase IsLineVisible = Points.Count > 0; } + + [RelayCommand] + private void ImportPoint(Window w) + { + + } + + [RelayCommand] + private async Task ExportPoint(Window w) + { + var folder = await w.StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions() + { + AllowMultiple = false, + SuggestedStartLocation = + await w.StorageProvider.TryGetFolderFromPathAsync( + Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)) + }); + foreach (var point in Points) + { + DataSerialization.SavePoint(point.GetPoint(), Path.Combine(HttpUtility.HtmlDecode(folder[0].Path.AbsolutePath), point.Name + ".json") ); + } + } // Visual properties diff --git a/Views/EditEquipmentDialog.axaml b/Views/EditEquipmentDialog.axaml index f2739ba..4b45ebe 100644 --- a/Views/EditEquipmentDialog.axaml +++ b/Views/EditEquipmentDialog.axaml @@ -117,7 +117,7 @@