diff --git a/SzCli/LocalFileManager.cs b/SzCli/LocalFileManager.cs new file mode 100644 index 0000000..ae5827e --- /dev/null +++ b/SzCli/LocalFileManager.cs @@ -0,0 +1,34 @@ +using SzLib; + +namespace SzCli; + +public class LocalFileManager : ISzFileManager +{ + public string DataPath + { + get + { + var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data"); + if (!Directory.Exists(path)) + { + Directory.CreateDirectory(path); + } + + return path; + } + } + + public bool SaveFile(string path, string fileContent) + { + try + { + File.WriteAllText(path, fileContent); + return true; + } + catch (Exception e) + { + Console.WriteLine($"Error saving file: {e.Message}"); + return false; + } + } +} \ No newline at end of file diff --git a/SzCli/Program.cs b/SzCli/Program.cs index a169e48..9375ebb 100644 --- a/SzCli/Program.cs +++ b/SzCli/Program.cs @@ -1,9 +1,26 @@ +using System; +using SzLib; + namespace SzCli; public class Program { public static void Main(string[] args) { + var parser = new SzParser(new LocalFileManager()); + + Console.WriteLine("Creating test dataset..."); + var dataset = Test.CreateTestDataset(); + Console.WriteLine($"Successfully created test dataset with id {dataset.Id}"); + Console.WriteLine("Attempting to save dataset..."); + var saved = parser.SaveDataset(dataset); + Console.WriteLine($"Successfully saved dataset? {saved}"); + + Console.WriteLine("Attempting to load dataset..."); + var loadedDataset = parser.LoadDataset(dataset.Id); + Console.WriteLine($"Successfully loaded dataset? {loadedDataset != null}. Id is {loadedDataset.Id}"); + + Console.WriteLine(loadedDataset.DataObjects["test"].Fields["value"].Value); } } \ No newline at end of file diff --git a/SzCli/Test.cs b/SzCli/Test.cs new file mode 100644 index 0000000..f742101 --- /dev/null +++ b/SzCli/Test.cs @@ -0,0 +1,36 @@ +using SzLib.DataObjects; + +namespace SzCli; + +public static class Test +{ + public static SzDataset CreateTestDataset() + { + var ds = new SzDataset() + { + Name = "TestDataset", + Id = "test-dataset", + DataObjectTemplateId = "0", + DataObjectType = "item", + Uuid = Guid.NewGuid(), + }; + + var testDataObject = new SzDataObject() + { + Name = "TestObject", + Id = "test", + }; + + var testField = new SzField() + { + Id = "value", + FieldType = SzFieldType.Number, + Value = "200" + }; + + testDataObject.Fields.Add(testField.Id, testField); + ds.DataObjects.Add(testDataObject.Id, testDataObject); + + return ds; + } +} \ No newline at end of file diff --git a/SzLib/DataObjects/SzDataObject.cs b/SzLib/DataObjects/SzDataObject.cs index 19f58ef..bc43c58 100644 --- a/SzLib/DataObjects/SzDataObject.cs +++ b/SzLib/DataObjects/SzDataObject.cs @@ -2,7 +2,7 @@ namespace SzLib.DataObjects; public class SzDataObject { - public required string Name; - public required string Id; - public Dictionary Fields = []; + public required string Name {get; set;} + public required string Id {get; set;} + public Dictionary Fields {get; set;} = []; } \ No newline at end of file diff --git a/SzLib/DataObjects/SzDataObjectTemplate.cs b/SzLib/DataObjects/SzDataObjectTemplate.cs index 9036dd8..ff765fd 100644 --- a/SzLib/DataObjects/SzDataObjectTemplate.cs +++ b/SzLib/DataObjects/SzDataObjectTemplate.cs @@ -2,9 +2,9 @@ namespace SzLib.DataObjects; public class SzDataObjectTemplate { - public required string Name; - public required string Id; - public Guid? Uuid; - public required string DataObjectType; - public Dictionary TemplateFields = []; + public required string Name {get; set;} + public required string Id {get; set;} + public Guid? Uuid {get; set;} + public required string DataObjectType {get; set;} + public Dictionary TemplateFields {get; set;} = []; } \ No newline at end of file diff --git a/SzLib/DataObjects/SzDataset.cs b/SzLib/DataObjects/SzDataset.cs index c712bc0..a95bedf 100644 --- a/SzLib/DataObjects/SzDataset.cs +++ b/SzLib/DataObjects/SzDataset.cs @@ -2,11 +2,11 @@ namespace SzLib.DataObjects; public class SzDataset { - public required string Name; - public required string Id; - public Guid? Uuid; + public required string Name {get; set;} + public required string Id {get; set;} + public Guid? Uuid {get; set;} - public required string DataObjectType; - public required string DataObjectTemplateId; - public Dictionary DataObjects = []; + public required string DataObjectType {get; set;} + public required string DataObjectTemplateId {get; set;} + public Dictionary DataObjects {get; set;} = []; } diff --git a/SzLib/DataObjects/SzField.cs b/SzLib/DataObjects/SzField.cs index e202ac8..524e0c0 100644 --- a/SzLib/DataObjects/SzField.cs +++ b/SzLib/DataObjects/SzField.cs @@ -2,8 +2,8 @@ namespace SzLib.DataObjects; public class SzField { - public required string Id; - public required SzFieldType FieldType; - public bool IsList = false; - public string Value = ""; + public required string Id {get; set;} + public required SzFieldType FieldType {get; set;} + public bool IsList {get; set;} = false; + public string Value {get; set;} = ""; } \ No newline at end of file diff --git a/SzLib/DataObjects/SzTemplateField.cs b/SzLib/DataObjects/SzTemplateField.cs index 2eac7ae..036d935 100644 --- a/SzLib/DataObjects/SzTemplateField.cs +++ b/SzLib/DataObjects/SzTemplateField.cs @@ -2,10 +2,10 @@ namespace SzLib.DataObjects; public class SzTemplateField { - public required string Name; - public required string Id; - public bool IsList = false; - public string DefaultValue = ""; - public bool IsSpecialType = false; - public string SpecialTypeValue = ""; + public required string Name {get; set;} + public required string Id {get; set;} + public bool IsList {get; set;} = false; + public string DefaultValue {get; set;} = ""; + public bool IsSpecialType {get; set;} = false; + public string SpecialTypeValue {get; set;} = ""; } \ No newline at end of file diff --git a/SzLib/ISzFileManager.cs b/SzLib/ISzFileManager.cs index 88f5fc3..64ea745 100644 --- a/SzLib/ISzFileManager.cs +++ b/SzLib/ISzFileManager.cs @@ -2,5 +2,7 @@ namespace SzLib; public interface ISzFileManager { - public string DataPath {get; set;} + public string DataPath {get;} + + public bool SaveFile(string path, string fileContent); } \ No newline at end of file diff --git a/SzLib/SzParser.cs b/SzLib/SzParser.cs index c155157..02a2ff6 100644 --- a/SzLib/SzParser.cs +++ b/SzLib/SzParser.cs @@ -3,37 +3,71 @@ using SzLib.DataObjects; namespace SzLib; -public class SzParser +public class SzParser(ISzFileManager szFileManager) { - public readonly ISzFileManager FileManager; + private readonly JsonSerializerOptions _jsonOptions = new() { WriteIndented = true }; - private JsonSerializerOptions _jsonOptions = new() - { - WriteIndented = true, - }; - - public SzParser(ISzFileManager szFileManager) - { - FileManager = szFileManager; - } - - public (string result, SzParseError error) SerializeDatasetToJson(SzDataset dataset) + public string SerializeDatasetToJson(SzDataset dataset) { try { var jsonString = JsonSerializer.Serialize(dataset, _jsonOptions); - return (jsonString, SzParseError.None); + return jsonString; } catch (JsonException e) { - return ("", SzParseError.CouldNotSerialize); + throw new Exception("Parse Error: " + e.Message); + } + } + + public SzDataset? DeserializeDataset(string jsonString) + { + try + { + var result = JsonSerializer.Deserialize(jsonString, _jsonOptions); + return result; + } + catch (Exception e) + { + throw new Exception("Could not deserialize JSON to type SzDataset: " + e.Message, e); + } + } + + public bool SaveDataset(SzDataset dataset) + { + var datasetDir = Path.Combine(szFileManager.DataPath, "datasets", dataset.Id); + Directory.CreateDirectory(datasetDir); + try + { + var json = SerializeDatasetToJson(dataset); + File.WriteAllText(Path.Combine(datasetDir, "dataset.json"), json); + return true; + } + catch (Exception e) + { + Console.WriteLine("Error saving dataset: " + e.Message); + return false; + } + } + + public SzDataset? LoadDataset(string datasetId) + { + var datasetPath = Path.Combine(szFileManager.DataPath, "datasets", datasetId, "dataset.json"); + if (!File.Exists(datasetPath)) + { + Console.WriteLine("Dataset not found: " + datasetId); + return null; + } + + try + { + return DeserializeDataset(File.ReadAllText(datasetPath)); + } + catch (Exception e) + { + Console.WriteLine($"Error loading dataset: {e.Message}"); + return null; } } } -public enum SzParseError -{ - None, - CouldNotSerialize, - GenericError, -} \ No newline at end of file