Adding Async methods to verious services
This commit is contained in:
@@ -13,48 +13,69 @@ public class DefaultLocalFileManager : ISzFileManager
|
|||||||
Directory.CreateDirectory(TemplatesPath);
|
Directory.CreateDirectory(TemplatesPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#region Synchronous Methods
|
||||||
public bool SaveFile(string path, string fileContent)
|
public bool SaveFile(string path, string fileContent)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var dir = Path.GetDirectoryName(path);
|
PrepareDirectory(path);
|
||||||
if (dir is null) return false;
|
|
||||||
Directory.CreateDirectory(dir);
|
|
||||||
|
|
||||||
File.WriteAllText(path, fileContent);
|
File.WriteAllText(path, fileContent);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Error saving file: {e.Message}");
|
SZ.Logger.LogError($"Error saving file: {e.Message}");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public string? LoadFile(string path)
|
public string? LoadFile(string path)
|
||||||
{
|
{
|
||||||
try
|
try { return File.ReadAllText(path); }
|
||||||
{
|
catch { return null; }
|
||||||
return File.ReadAllText(path);
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"Error loading file: {e.Message}");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool DeleteFile(string path)
|
public bool DeleteFile(string path)
|
||||||
|
{
|
||||||
|
try { File.Delete(path); return true; }
|
||||||
|
catch { return false; }
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Asynchronous Methods
|
||||||
|
public async Task<bool> SaveFileAsync(string path, string fileContent, CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
File.Delete(path);
|
PrepareDirectory(path);
|
||||||
|
// Use the native async File I/O
|
||||||
|
await File.WriteAllTextAsync(path, fileContent, ct);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
SZ.Logger.LogError("LocalFileManager: Could not delete file. " + e.Message);
|
SZ.Logger.LogError($"Error saving file async: {e.Message}");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<string?> LoadFileAsync(string path, CancellationToken ct = default)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (!File.Exists(path)) return null;
|
||||||
|
return await File.ReadAllTextAsync(path, ct);
|
||||||
|
}
|
||||||
|
catch { return null; }
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
private void PrepareDirectory(string filePath)
|
||||||
|
{
|
||||||
|
var dir = Path.GetDirectoryName(filePath);
|
||||||
|
if (dir != null && !Directory.Exists(dir))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(dir);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -7,9 +7,6 @@ public interface ISzDatabaseHandler
|
|||||||
public bool InternetAllowed { get; set; }
|
public bool InternetAllowed { get; set; }
|
||||||
public Dictionary<string, string> SzDbUrls { get; set; }
|
public Dictionary<string, string> SzDbUrls { get; set; }
|
||||||
|
|
||||||
public Task<SzDataset?> TryRetrieveDatasetAsync(Guid uuid);
|
public Task<SzDataset?> TryRetrieveDatasetAsync(Guid uuid, CancellationToken ct = default);
|
||||||
public Task<ISzTemplate?> TryRetrieveTemplateAsync(Guid uuid);
|
public Task<ISzTemplate?> TryRetrieveTemplateAsync(Guid uuid, CancellationToken ct = default);
|
||||||
|
|
||||||
public SzDataset? TryRetrieveDataset(Guid uuid);
|
|
||||||
public ISzTemplate? TryRetrieveTemplate(Guid uuid);
|
|
||||||
}
|
}
|
||||||
@@ -1,15 +1,15 @@
|
|||||||
using System.Security.Cryptography.X509Certificates;
|
|
||||||
using SzCore.DataObjects;
|
|
||||||
|
|
||||||
namespace SzCore;
|
namespace SzCore;
|
||||||
|
|
||||||
public interface ISzFileManager
|
public interface ISzFileManager
|
||||||
{
|
{
|
||||||
public string DataPath {get;}
|
string DataPath { get; }
|
||||||
public string DatasetsPath {get;}
|
string DatasetsPath { get; }
|
||||||
public string TemplatesPath {get;}
|
string TemplatesPath { get; }
|
||||||
|
|
||||||
public bool SaveFile(string path, string fileContent);
|
bool SaveFile(string path, string fileContent);
|
||||||
public string? LoadFile(string path);
|
string? LoadFile(string path);
|
||||||
public bool DeleteFile(string path);
|
bool DeleteFile(string path);
|
||||||
|
|
||||||
|
Task<bool> SaveFileAsync(string path, string fileContent, CancellationToken ct = default);
|
||||||
|
Task<string?> LoadFileAsync(string path, CancellationToken ct = default);
|
||||||
}
|
}
|
||||||
@@ -1,12 +1,11 @@
|
|||||||
using System.ComponentModel.Design;
|
|
||||||
using SzCore.DataObjects;
|
using SzCore.DataObjects;
|
||||||
|
|
||||||
namespace SzCore;
|
namespace SzCore;
|
||||||
|
|
||||||
public class SzDataHandler
|
public class SzDataHandler
|
||||||
{
|
{
|
||||||
private Dictionary<string, SzDataset> _loadedDatasets = [];
|
private readonly Dictionary<string, SzDataset> _loadedDatasets = [];
|
||||||
private Dictionary<string, ISzTemplate> _loadedTemplates = [];
|
private readonly Dictionary<string, ISzTemplate> _loadedTemplates = [];
|
||||||
|
|
||||||
public SzDataHandler() { }
|
public SzDataHandler() { }
|
||||||
|
|
||||||
@@ -16,13 +15,14 @@ public class SzDataHandler
|
|||||||
_loadedTemplates.Clear();
|
_loadedTemplates.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Datasets
|
#region Datasets (Async)
|
||||||
public SzResult<bool> SaveDataset(SzDataset dataset)
|
public async Task<SzResult<bool>> SaveDatasetAsync(SzDataset dataset, CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
var datasetPath = Path.Combine(SZ.LocalFileManager.DatasetsPath, dataset.Id, "dataset.json");
|
var datasetPath = Path.Combine(SZ.LocalFileManager.DatasetsPath, dataset.Id, "dataset.json");
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var success = SZ.LocalFileManager.SaveFile(datasetPath, SZ.Parser.SerializeDatasetToJson(dataset));
|
var json = SZ.Parser.SerializeDatasetToJson(dataset);
|
||||||
|
var success = await SZ.LocalFileManager.SaveFileAsync(datasetPath, json, ct);
|
||||||
return success ? SzResult<bool>.Success(true) : SzResult<bool>.Failure("Failed to write dataset file.");
|
return success ? SzResult<bool>.Success(true) : SzResult<bool>.Failure("Failed to write dataset file.");
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
@@ -32,53 +32,47 @@ public class SzDataHandler
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public SzResult<bool> DeleteDataset(string datasetId)
|
public async Task<SzResult<SzDataset>> LoadDatasetAsync(string datasetId, CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
|
if (_loadedDatasets.TryGetValue(datasetId, out SzDataset? value))
|
||||||
|
return SzResult<SzDataset>.Success(value);
|
||||||
|
|
||||||
|
var datasetPath = Path.Combine(SZ.LocalFileManager.DatasetsPath, datasetId, "dataset.json");
|
||||||
|
|
||||||
|
var json = await SZ.LocalFileManager.LoadFileAsync(datasetPath, ct);
|
||||||
|
if (json is null)
|
||||||
|
return SzResult<SzDataset>.Failure($"Could not find dataset file for ID '{datasetId}'");
|
||||||
|
|
||||||
|
var parsedDataset = SZ.Parser.DeserializeDataset(json);
|
||||||
|
if (parsedDataset is null)
|
||||||
|
return SzResult<SzDataset>.Failure($"Failed to parse dataset JSON for ID '{datasetId}'");
|
||||||
|
|
||||||
|
_loadedDatasets.TryAdd(parsedDataset.Id, parsedDataset);
|
||||||
|
return SzResult<SzDataset>.Success(parsedDataset);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<SzResult<bool>> DeleteDatasetAsync(string datasetId)
|
||||||
|
{
|
||||||
|
return await Task.Run(() => {
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Directory.Delete(Path.Combine(SZ.LocalFileManager.DatasetsPath, datasetId), true);
|
var path = Path.Combine(SZ.LocalFileManager.DatasetsPath, datasetId);
|
||||||
|
if (Directory.Exists(path)) Directory.Delete(path, true);
|
||||||
|
_loadedDatasets.Remove(datasetId);
|
||||||
return SzResult<bool>.Success(true);
|
return SzResult<bool>.Success(true);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
SZ.Logger.LogError($"Could not delete Dataset with id '{datasetId}': {e.Message}");
|
|
||||||
return SzResult<bool>.Failure(e.Message);
|
return SzResult<bool>.Failure(e.Message);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public SzResult<SzDataset> LoadDataset(string datasetId)
|
public async Task<SzResult<SzDataset>> CreateDatasetAsync(string name, string templateId, string? id = null, Guid? templateUuid = null)
|
||||||
{
|
{
|
||||||
if (_loadedDatasets.TryGetValue(datasetId, out SzDataset? value))
|
if (string.IsNullOrEmpty(name)) return SzResult<SzDataset>.Failure("Name empty.");
|
||||||
{
|
|
||||||
return SzResult<SzDataset>.Success(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
var datasetPath = Path.Combine(SZ.LocalFileManager.DataPath, "datasets", datasetId, "dataset.json");
|
var templateResult = await LoadTemplateAsync<SzDataObjectTemplate>(templateId);
|
||||||
|
|
||||||
var json = SZ.LocalFileManager.LoadFile(datasetPath);
|
|
||||||
if (json is null)
|
|
||||||
{
|
|
||||||
SZ.Logger.LogWarning($"Could not load dataset with ID '{datasetId}'");
|
|
||||||
return SzResult<SzDataset>.Failure($"Could not find dataset file for ID '{datasetId}'");
|
|
||||||
}
|
|
||||||
|
|
||||||
var parsedDataset = SZ.Parser.DeserializeDataset(json);
|
|
||||||
if (parsedDataset is null)
|
|
||||||
{
|
|
||||||
SZ.Logger.LogWarning($"Could not load dataset with ID '{datasetId}'");
|
|
||||||
return SzResult<SzDataset>.Failure($"Failed to parse dataset JSON for ID '{datasetId}'");
|
|
||||||
}
|
|
||||||
|
|
||||||
_loadedDatasets.Add(parsedDataset.Id, parsedDataset);
|
|
||||||
return SzResult<SzDataset>.Success(parsedDataset);
|
|
||||||
}
|
|
||||||
|
|
||||||
public SzResult<SzDataset> CreateDataset(string name, string templateId, string? id = null, Guid? templateUuid = null)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(name))
|
|
||||||
return SzResult<SzDataset>.Failure("Dataset name cannot be empty.");
|
|
||||||
|
|
||||||
var templateResult = LoadTemplate<SzDataObjectTemplate>(templateId);
|
|
||||||
if (!templateResult.IsSuccess || templateResult.Value is null)
|
if (!templateResult.IsSuccess || templateResult.Value is null)
|
||||||
return SzResult<SzDataset>.Failure($"Template '{templateId}' not found.");
|
return SzResult<SzDataset>.Failure($"Template '{templateId}' not found.");
|
||||||
|
|
||||||
@@ -92,75 +86,48 @@ public class SzDataHandler
|
|||||||
Uuid = Guid.NewGuid()
|
Uuid = Guid.NewGuid()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return SzResult<SzDataset>.Success(newDataset);
|
return SzResult<SzDataset>.Success(newDataset);
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Templates
|
#region Templates (Async)
|
||||||
public SzResult<bool> SaveTemplate(ISzTemplate template)
|
public async Task<SzResult<bool>> SaveTemplateAsync(ISzTemplate template, CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
var templatePath = Path.Combine(SZ.LocalFileManager.TemplatesPath, template.Id, "template.json");
|
var templatePath = Path.Combine(SZ.LocalFileManager.TemplatesPath, template.Id, "template.json");
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var success = SZ.LocalFileManager.SaveFile(templatePath, SZ.Parser.SerializeTemplateToJson(template));
|
var json = SZ.Parser.SerializeTemplateToJson(template);
|
||||||
|
var success = await SZ.LocalFileManager.SaveFileAsync(templatePath, json, ct);
|
||||||
return success ? SzResult<bool>.Success(true) : SzResult<bool>.Failure("Failed to write template file.");
|
return success ? SzResult<bool>.Success(true) : SzResult<bool>.Failure("Failed to write template file.");
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
SZ.Logger.LogError("Error saving template: " + e.Message);
|
|
||||||
return SzResult<bool>.Failure(e.Message);
|
return SzResult<bool>.Failure(e.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public SzResult<bool> DeleteTemplate(string templateId)
|
public async Task<SzResult<T>> LoadTemplateAsync<T>(string templateId, CancellationToken ct = default) where T : ISzTemplate
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Directory.Delete(Path.Combine(SZ.LocalFileManager.TemplatesPath, templateId), true);
|
|
||||||
return SzResult<bool>.Success(true);
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
SZ.Logger.LogError($"Could not delete Template with id '{templateId}': {e.Message}");
|
|
||||||
return SzResult<bool>.Failure(e.Message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public SzResult<T> LoadTemplate<T>(string templateId) where T : ISzTemplate
|
|
||||||
{
|
{
|
||||||
if (_loadedTemplates.TryGetValue(templateId, out ISzTemplate? cachedTemplate))
|
if (_loadedTemplates.TryGetValue(templateId, out ISzTemplate? cachedTemplate))
|
||||||
{
|
|
||||||
return SzResult<T>.Success((T)cachedTemplate);
|
return SzResult<T>.Success((T)cachedTemplate);
|
||||||
|
|
||||||
|
var path = Path.Combine(SZ.LocalFileManager.TemplatesPath, templateId, "template.json");
|
||||||
|
var json = await SZ.LocalFileManager.LoadFileAsync(path, ct);
|
||||||
|
|
||||||
|
if (json is null) return SzResult<T>.Failure($"Template '{templateId}' not found.");
|
||||||
|
|
||||||
|
var parsed = SZ.Parser.DeserializeTemplate<T>(json);
|
||||||
|
if (parsed is null) return SzResult<T>.Failure("Parse error.");
|
||||||
|
|
||||||
|
_loadedTemplates.TryAdd(parsed.Id, parsed);
|
||||||
|
return SzResult<T>.Success(parsed);
|
||||||
}
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
var templatePah = Path.Combine(SZ.LocalFileManager.TemplatesPath, templateId, "template.json");
|
#region Synchronous Logic
|
||||||
|
// These remain synchronous because they are purely in-memory operations.
|
||||||
var json = SZ.LocalFileManager.LoadFile(templatePah);
|
|
||||||
if (json is null)
|
|
||||||
{
|
|
||||||
SZ.Logger.LogWarning($"Could not load template with ID '{templateId}'");
|
|
||||||
return SzResult<T>.Failure($"Could not find template file for ID '{templateId}'");
|
|
||||||
}
|
|
||||||
|
|
||||||
var parsedTemplate = SZ.Parser.DeserializeTemplate<T>(json);
|
|
||||||
if (parsedTemplate is null)
|
|
||||||
{
|
|
||||||
SZ.Logger.LogWarning($"Could not load template with ID '{templateId}'");
|
|
||||||
return SzResult<T>.Failure($"Failed to parse template JSON for ID '{templateId}'");
|
|
||||||
}
|
|
||||||
|
|
||||||
_loadedTemplates.TryAdd(parsedTemplate.Id, parsedTemplate);
|
|
||||||
|
|
||||||
return SzResult<T>.Success(parsedTemplate);
|
|
||||||
}
|
|
||||||
|
|
||||||
public SzResult<SzDataObjectTemplate> CreateDataObjectTemplate(string name, string dataObjectType, string? id = null, string? description = null, List<SzTemplateField>? fields = null)
|
public SzResult<SzDataObjectTemplate> CreateDataObjectTemplate(string name, string dataObjectType, string? id = null, string? description = null, List<SzTemplateField>? fields = null)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(name)) return SzResult<SzDataObjectTemplate>.Failure("Name cannot be empty.");
|
|
||||||
if (string.IsNullOrEmpty(dataObjectType)) return SzResult<SzDataObjectTemplate>.Failure("DataObjectType cannot be empty.");
|
|
||||||
|
|
||||||
var newTemplate = new SzDataObjectTemplate()
|
var newTemplate = new SzDataObjectTemplate()
|
||||||
{
|
{
|
||||||
Name = name,
|
Name = name,
|
||||||
@@ -170,48 +137,27 @@ public class SzDataHandler
|
|||||||
Description = description ?? ""
|
Description = description ?? ""
|
||||||
};
|
};
|
||||||
|
|
||||||
if (fields is null) return SzResult<SzDataObjectTemplate>.Success(newTemplate);
|
if (fields != null)
|
||||||
foreach (var field in fields)
|
|
||||||
{
|
{
|
||||||
newTemplate.TemplateFields.Add(field.Id, field);
|
foreach (var field in fields) newTemplate.TemplateFields.TryAdd(field.Id, field);
|
||||||
}
|
}
|
||||||
|
|
||||||
return SzResult<SzDataObjectTemplate>.Success(newTemplate);
|
return SzResult<SzDataObjectTemplate>.Success(newTemplate);
|
||||||
}
|
}
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Other
|
public SzResult<SzDataObject> CreateDataObject(SzDataObjectTemplate template, string name, string? id = null)
|
||||||
public SzResult<SzTemplateField> CreateTemplateField(string id, SzFieldType fieldType, bool isList = false, string? defaultValue = null, bool isSpecialType = false, string? specialTypeValue = null)
|
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(id)) return SzResult<SzTemplateField>.Failure("ID cannot be empty.");
|
var dataObject = new SzDataObject()
|
||||||
|
|
||||||
var newField = new SzTemplateField()
|
|
||||||
{
|
{
|
||||||
Id = id,
|
Name = name,
|
||||||
FieldType = fieldType,
|
Id = id ?? name.ToLower().Replace(" ", "-").Trim()
|
||||||
IsList = isList,
|
|
||||||
IsSpecialType = isSpecialType,
|
|
||||||
DefaultValue = defaultValue ?? "",
|
|
||||||
SpecialTypeValue = specialTypeValue ?? ""
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return SzResult<SzTemplateField>.Success(newField);
|
var fieldsResult = CreateFieldsFromTemplate(template);
|
||||||
}
|
if (!fieldsResult.IsSuccess) return SzResult<SzDataObject>.Failure(fieldsResult.Error);
|
||||||
|
|
||||||
public SzResult<SzField> CreateField(string id, SzFieldType fieldType, bool isList = false, string? value = null)
|
dataObject.Fields = fieldsResult.Value!;
|
||||||
{
|
return SzResult<SzDataObject>.Success(dataObject);
|
||||||
if (string.IsNullOrEmpty(id))
|
|
||||||
return SzResult<SzField>.Failure("Could not create field, the id parameter was empty");
|
|
||||||
|
|
||||||
var newField = new SzField()
|
|
||||||
{
|
|
||||||
Id = id,
|
|
||||||
FieldType = fieldType,
|
|
||||||
IsList = isList,
|
|
||||||
Value = value ?? ""
|
|
||||||
};
|
|
||||||
|
|
||||||
return SzResult<SzField>.Success(newField);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public SzResult<Dictionary<string, SzField>> CreateFieldsFromTemplate(ISzTemplate template)
|
public SzResult<Dictionary<string, SzField>> CreateFieldsFromTemplate(ISzTemplate template)
|
||||||
@@ -219,87 +165,23 @@ public class SzDataHandler
|
|||||||
var fields = new Dictionary<string, SzField>();
|
var fields = new Dictionary<string, SzField>();
|
||||||
foreach (var tempField in template.TemplateFields.Values)
|
foreach (var tempField in template.TemplateFields.Values)
|
||||||
{
|
{
|
||||||
var newField = CreateField(tempField.Id, tempField.FieldType, tempField.IsList);
|
fields.Add(tempField.Id, new SzField { Id = tempField.Id, FieldType = tempField.FieldType, IsList = tempField.IsList });
|
||||||
if (!newField.IsSuccess || newField.Value is null)
|
|
||||||
return SzResult<Dictionary<string, SzField>>.Failure($"Could not create field: {newField.Error}");
|
|
||||||
fields.Add(newField.Value.Id, newField.Value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return SzResult<Dictionary<string, SzField>>.Success(fields);
|
return SzResult<Dictionary<string, SzField>>.Success(fields);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SzResult<SzDataObject> CreateDataObject(SzDataObjectTemplate template, string name, string? id = null)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(name))
|
|
||||||
return SzResult<SzDataObject>.Failure("Could not create object, name was empty");
|
|
||||||
|
|
||||||
var dataObject = new SzDataObject()
|
|
||||||
{
|
|
||||||
Name = name,
|
|
||||||
Id = id ?? name.ToLower().Replace(" ", "-").Trim()
|
|
||||||
};
|
|
||||||
|
|
||||||
var createFieldsResult = CreateFieldsFromTemplate(template);
|
|
||||||
if (!createFieldsResult.IsSuccess || createFieldsResult.Value is null)
|
|
||||||
return SzResult<SzDataObject>.Failure($"Could not create data object's fields: {createFieldsResult.Error}");
|
|
||||||
dataObject.Fields = createFieldsResult.Value;
|
|
||||||
|
|
||||||
return SzResult<SzDataObject>.Success(dataObject);
|
|
||||||
}
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Tests
|
#region Bulk Operations
|
||||||
private void LoadAllDatasets()
|
public async Task LoadAllDatasetsAsync(CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
_loadedDatasets.Clear();
|
_loadedDatasets.Clear();
|
||||||
if (!Directory.Exists(SZ.LocalFileManager.DatasetsPath))
|
if (!Directory.Exists(SZ.LocalFileManager.DatasetsPath)) return;
|
||||||
|
|
||||||
|
var dirs = Directory.GetDirectories(SZ.LocalFileManager.DatasetsPath);
|
||||||
|
foreach (var dir in dirs)
|
||||||
{
|
{
|
||||||
throw new Exception("Could not load datasets, the dataset directory does not exist.");
|
var id = Path.GetFileName(dir);
|
||||||
}
|
await LoadDatasetAsync(id, ct);
|
||||||
|
|
||||||
foreach (var dir in Directory.GetDirectories(SZ.LocalFileManager.DatasetsPath))
|
|
||||||
{
|
|
||||||
var datasetFilePath = Path.Combine(dir, "dataset.json");
|
|
||||||
if (!File.Exists(datasetFilePath)) continue;
|
|
||||||
|
|
||||||
var datasetJson = SZ.LocalFileManager.LoadFile(datasetFilePath);
|
|
||||||
if (datasetJson == null) continue;
|
|
||||||
|
|
||||||
var parsedDataset = SZ.Parser.DeserializeDataset(datasetJson);
|
|
||||||
if (parsedDataset == null)
|
|
||||||
{
|
|
||||||
SZ.Logger.LogWarning($"Parse error: Could not parse file {datasetFilePath} as an SzDataset");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
_loadedDatasets.Add(parsedDataset.Id, parsedDataset);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void LoadAllTemplates()
|
|
||||||
{
|
|
||||||
_loadedTemplates.Clear();
|
|
||||||
if (!Directory.Exists(SZ.LocalFileManager.TemplatesPath))
|
|
||||||
{
|
|
||||||
throw new Exception("Could not load templates, the template directory does not exist.");
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var dir in Directory.GetDirectories(SZ.LocalFileManager.TemplatesPath))
|
|
||||||
{
|
|
||||||
var templateFilePath = Path.Combine(dir, "template.json");
|
|
||||||
if (!File.Exists(templateFilePath)) continue;
|
|
||||||
|
|
||||||
var templateJson = SZ.LocalFileManager.LoadFile(templateFilePath);
|
|
||||||
if (templateJson == null) continue;
|
|
||||||
|
|
||||||
var parsedTemplate = SZ.Parser.DeserializeTemplate<ISzTemplate>(templateJson);
|
|
||||||
if (parsedTemplate == null)
|
|
||||||
{
|
|
||||||
SZ.Logger.LogWarning($"Parse error: Could not parse file {templateFilePath} as an ISzTemplate");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
_loadedTemplates.Add(parsedTemplate.Id, parsedTemplate);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
@@ -6,17 +6,17 @@ namespace SzCore;
|
|||||||
|
|
||||||
public class SzEvaluator
|
public class SzEvaluator
|
||||||
{
|
{
|
||||||
|
public async Task<SzResult<bool>> EvaluateDatasetAsync(SzDataset dataset, CancellationToken ct = default)
|
||||||
public SzResult<bool> EvaluateDataset(SzDataset dataset)
|
|
||||||
{
|
{
|
||||||
var errors = new StringBuilder();
|
var errors = new StringBuilder();
|
||||||
|
|
||||||
var template = SZ.DataHandler.LoadTemplate<SzDataObjectTemplate>(dataset.DataObjectTemplateId);
|
var templateResult = await SZ.DataHandler.LoadTemplateAsync<SzDataObjectTemplate>(dataset.DataObjectTemplateId, ct);
|
||||||
if (!template.IsSuccess || template.Value is null) return SzResult<bool>.Failure($"Template with id {dataset.DataObjectTemplateId} for Dataset ID {dataset.Id} could not be loaded");
|
if (!templateResult.IsSuccess || templateResult.Value is null)
|
||||||
|
return SzResult<bool>.Failure($"Template with id {dataset.DataObjectTemplateId} for Dataset ID {dataset.Id} could not be loaded: {templateResult.Error}");
|
||||||
|
|
||||||
foreach (var dataObject in dataset.DataObjects.Values)
|
foreach (var dataObject in dataset.DataObjects.Values)
|
||||||
{
|
{
|
||||||
var evalResult = EvaluateDataObject(dataObject, template.Value);
|
var evalResult = EvaluateDataObject(dataObject, templateResult.Value);
|
||||||
if (!evalResult.IsSuccess) errors.Append(evalResult.Error);
|
if (!evalResult.IsSuccess) errors.Append(evalResult.Error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user