Adding creation of data into data handler, marking the way for szdb handling
This commit is contained in:
33
SzCore/Defaults/DefaultDatabaseHandler.cs
Normal file
33
SzCore/Defaults/DefaultDatabaseHandler.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using SzCore.DataObjects;
|
||||
|
||||
namespace SzCore.Defaults;
|
||||
|
||||
public class DefaultDatabaseHandler : ISzDatabaseHandler
|
||||
{
|
||||
public bool InternetAllowed { get; set; } = true;
|
||||
public Dictionary<string, string> SzDbUrls { get; set; } = [];
|
||||
|
||||
public SzDataset? TryRetrieveDataset(Guid uuid)
|
||||
{
|
||||
SZ.Logger.LogWarning("The default database helper is not implemented yet");
|
||||
return null;
|
||||
}
|
||||
|
||||
public Task<SzDataset?> TryRetrieveDatasetAsync(Guid uuid)
|
||||
{
|
||||
SZ.Logger.LogWarning("The default database helper is not implemented yet");
|
||||
return null;
|
||||
}
|
||||
|
||||
public ISzTemplate? TryRetrieveTemplate(Guid uuid)
|
||||
{
|
||||
SZ.Logger.LogWarning("The default database helper is not implemented yet");
|
||||
return null;
|
||||
}
|
||||
|
||||
public Task<ISzTemplate?> TryRetrieveTemplateAsync(Guid uuid)
|
||||
{
|
||||
SZ.Logger.LogWarning("The default database helper is not implemented yet");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -43,4 +43,18 @@ public class DefaultLocalFileManager : ISzFileManager
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public bool DeleteFile(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Delete(path);
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
SZ.Logger.LogError("LocalFileManager: Could not delete file. " + e.Message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,15 @@
|
||||
using SzCore.DataObjects;
|
||||
|
||||
namespace SzCore;
|
||||
|
||||
public interface ISzDatabaseHandler
|
||||
{
|
||||
public bool InternetAllowed { get; set; }
|
||||
public Dictionary<string, string> SzDbUrls { get; set; }
|
||||
|
||||
public Task<SzDataset?> TryRetrieveDatasetAsync(Guid uuid);
|
||||
public Task<ISzTemplate?> TryRetrieveTemplateAsync(Guid uuid);
|
||||
|
||||
public SzDataset? TryRetrieveDataset(Guid uuid);
|
||||
public ISzTemplate? TryRetrieveTemplate(Guid uuid);
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using SzCore.DataObjects;
|
||||
|
||||
namespace SzCore;
|
||||
@@ -10,4 +11,5 @@ public interface ISzFileManager
|
||||
|
||||
public bool SaveFile(string path, string fileContent);
|
||||
public string? LoadFile(string path);
|
||||
public bool DeleteFile(string path);
|
||||
}
|
||||
|
||||
20
SzCore/SZ.cs
20
SzCore/SZ.cs
@@ -6,6 +6,7 @@ public static class SZ
|
||||
{
|
||||
private static ISzFileManager? _fileManager;
|
||||
private static ISzLogger? _logger;
|
||||
private static ISzDatabaseHandler? _dbHandler;
|
||||
|
||||
public static bool IsInitalized { get; private set; } = false;
|
||||
|
||||
@@ -33,6 +34,16 @@ public static class SZ
|
||||
}
|
||||
}
|
||||
|
||||
public static ISzDatabaseHandler DatabaseHandler
|
||||
{
|
||||
get
|
||||
{
|
||||
CheckInitialization();
|
||||
if (_dbHandler is null) throw new Exception("SZ._dbHandler is null");
|
||||
return _dbHandler;
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly Dictionary<Type, object> _services = [];
|
||||
private static readonly List<Type> _protectedServices = [];
|
||||
|
||||
@@ -42,12 +53,13 @@ public static class SZ
|
||||
/// <param name="fileManager">An ISzFileManager instance.</param>
|
||||
/// <param name="logger">An ISzLogger instance.</param>
|
||||
/// /// <exception cref="Exception">Throws if SZ has already been initalized</exception>
|
||||
public static void Init(ISzFileManager fileManager, ISzLogger logger)
|
||||
public static void Init(ISzFileManager fileManager, ISzLogger logger, ISzDatabaseHandler databaseHandler)
|
||||
{
|
||||
if (IsInitalized) throw new Exception("Cannot initalize SZ more than once.");
|
||||
|
||||
_logger = logger;
|
||||
_fileManager = fileManager;
|
||||
_dbHandler = databaseHandler;
|
||||
|
||||
if (_logger is null)
|
||||
{
|
||||
@@ -59,10 +71,16 @@ public static class SZ
|
||||
throw new Exception("SZ.Init failed: LocalFileManager was null");
|
||||
}
|
||||
|
||||
if (_dbHandler is null)
|
||||
{
|
||||
throw new Exception("SZ.Init failed: DatabaseHandler was null");
|
||||
}
|
||||
|
||||
IsInitalized = true;
|
||||
|
||||
AddService(fileManager, true);
|
||||
AddService(logger, true);
|
||||
AddService(databaseHandler, true);
|
||||
|
||||
logger.Log(" -- SZ CORE INITALIZED -- ");
|
||||
}
|
||||
|
||||
@@ -10,6 +10,14 @@ public class SzDataHandler
|
||||
|
||||
public SzDataHandler(){}
|
||||
|
||||
// The client should periodically call this to free memory, probably
|
||||
private void ClearCache()
|
||||
{
|
||||
_loadedDatasets.Clear();
|
||||
_loadedTemplates.Clear();
|
||||
}
|
||||
|
||||
#region Datasets
|
||||
public bool SaveDataset(SzDataset dataset)
|
||||
{
|
||||
var datasetPath = Path.Combine(SZ.LocalFileManager.DatasetsPath, dataset.Id, "dataset.json");
|
||||
@@ -24,6 +32,20 @@ public class SzDataHandler
|
||||
}
|
||||
}
|
||||
|
||||
public bool DeleteDataset(string datasetId)
|
||||
{
|
||||
try
|
||||
{
|
||||
Directory.Delete(Path.Combine(SZ.LocalFileManager.DatasetsPath, datasetId), true);
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
SZ.Logger.LogError($"Could not delete Dataset with id '{datasetId}': {e.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public SzDataset? LoadDataset(string datasetId)
|
||||
{
|
||||
if (_loadedDatasets.TryGetValue(datasetId, out SzDataset? value))
|
||||
@@ -51,6 +73,14 @@ public class SzDataHandler
|
||||
return parsedDataset;
|
||||
}
|
||||
|
||||
public SzDataset CreateDataset()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Templates
|
||||
public bool SaveTemplate(ISzTemplate template)
|
||||
{
|
||||
var templatePath = Path.Combine(SZ.LocalFileManager.TemplatesPath, template.Id, "template.json");
|
||||
@@ -65,6 +95,20 @@ public class SzDataHandler
|
||||
}
|
||||
}
|
||||
|
||||
public bool DeleteTemplate(string templateId)
|
||||
{
|
||||
try
|
||||
{
|
||||
Directory.Delete(Path.Combine(SZ.LocalFileManager.TemplatesPath, templateId), true);
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
SZ.Logger.LogError($"Could not delete Template with id '{templateId}': {e.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public T? LoadTemplate<T>(string templateId) where T : ISzTemplate
|
||||
{
|
||||
if (_loadedTemplates.TryGetValue(templateId, out ISzTemplate? existing))
|
||||
@@ -93,13 +137,49 @@ public class SzDataHandler
|
||||
return parsedTemplate;
|
||||
}
|
||||
|
||||
// The client should periodically call this to free memory, probably
|
||||
private void ClearCache()
|
||||
public SzDataObjectTemplate? CreateDataObjectTemplate(string name, string dataObjectType, string? id = null, string? description = null, List<SzTemplateField>? fields = null)
|
||||
{
|
||||
_loadedDatasets.Clear();
|
||||
_loadedTemplates.Clear();
|
||||
}
|
||||
if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(dataObjectType)) return null;
|
||||
|
||||
string newId;
|
||||
if (!string.IsNullOrEmpty(id))
|
||||
{
|
||||
newId = id;
|
||||
}
|
||||
else
|
||||
{
|
||||
newId = name.ToLower().Replace(" ", "-").Trim();
|
||||
}
|
||||
|
||||
var newTemplate = new SzDataObjectTemplate()
|
||||
{
|
||||
Name = name,
|
||||
Id = newId,
|
||||
DataObjectType = dataObjectType,
|
||||
Uuid = Guid.NewGuid(),
|
||||
Description = description ?? ""
|
||||
};
|
||||
|
||||
if (fields is not null)
|
||||
{
|
||||
foreach (var field in fields)
|
||||
{
|
||||
newTemplate.TemplateFields.Add(field.Id, field);
|
||||
}
|
||||
}
|
||||
|
||||
return newTemplate;
|
||||
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Other
|
||||
|
||||
public Sz
|
||||
|
||||
#endregion
|
||||
|
||||
#region Tests
|
||||
// Only use these two methods for test purposes, we probably don't want to load all datasets and templates to memory in production
|
||||
private void LoadAllDatasets()
|
||||
{
|
||||
@@ -154,4 +234,5 @@ public class SzDataHandler
|
||||
_loadedTemplates.Add(parsedTemplate.Id, parsedTemplate);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
@@ -6,9 +6,29 @@ namespace SzCore;
|
||||
|
||||
public class SzEvaluator
|
||||
{
|
||||
public SzEvaluationResult EvaluateDataObject(SzDataObject dataObject, SzDataObjectTemplate template)
|
||||
|
||||
public SzOperationResult EvaluateDataset(SzDataset dataset)
|
||||
{
|
||||
if (dataObject is null || template is null) return new SzEvaluationResult(false, "dataObject or template is null");
|
||||
if (dataset is null) return new SzOperationResult(false, "Dataset cannot be null");
|
||||
|
||||
var errors = new StringBuilder();
|
||||
|
||||
var template = SZ.SzDataHandler.LoadTemplate<SzDataObjectTemplate>(dataset.DataObjectTemplateId);
|
||||
if (template is null) return new SzOperationResult(false, $"Template with id {dataset.DataObjectTemplateId} for Dataset ID {dataset.Id} could not be loaded");
|
||||
|
||||
foreach (var dataObject in dataset.DataObjects.Values)
|
||||
{
|
||||
var evalResult = EvaluateDataObject(dataObject, template);
|
||||
if (!evalResult.Pass) errors.Append(evalResult.Errors);
|
||||
}
|
||||
|
||||
var errString = errors.ToString();
|
||||
return new SzOperationResult(string.IsNullOrEmpty(errString), errString);
|
||||
}
|
||||
|
||||
public SzOperationResult EvaluateDataObject(SzDataObject dataObject, SzDataObjectTemplate template)
|
||||
{
|
||||
if (dataObject is null || template is null) return new SzOperationResult(false, "dataObject or template is null");
|
||||
|
||||
var errors = new StringBuilder();
|
||||
|
||||
@@ -36,10 +56,10 @@ public class SzEvaluator
|
||||
}
|
||||
|
||||
var errString = errors.ToString().Trim();
|
||||
return new SzEvaluationResult(string.IsNullOrEmpty(errString), string.IsNullOrEmpty(errString) ? "" : errString);
|
||||
return new SzOperationResult(string.IsNullOrEmpty(errString), string.IsNullOrEmpty(errString) ? "" : errString);
|
||||
}
|
||||
|
||||
public SzEvaluationResult DoesFieldMatchTemplateField(SzField field, SzTemplateField templateField)
|
||||
public SzOperationResult DoesFieldMatchTemplateField(SzField field, SzTemplateField templateField)
|
||||
{
|
||||
var errors = new StringBuilder();
|
||||
|
||||
@@ -48,13 +68,13 @@ public class SzEvaluator
|
||||
if (field.IsList != templateField.IsList) errors.AppendLine($"Field '{field.Id}' IsList mismatch: Expected {templateField.IsList}");
|
||||
|
||||
var errString = errors.ToString().Trim();
|
||||
return new SzEvaluationResult(string.IsNullOrEmpty(errString), errString);
|
||||
return new SzOperationResult(string.IsNullOrEmpty(errString), errString);
|
||||
}
|
||||
|
||||
public SzEvaluationResult EvaluateFieldValue(SzField field)
|
||||
public SzOperationResult EvaluateFieldValue(SzField field)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(field.Value))
|
||||
return new SzEvaluationResult(true, "");
|
||||
return new SzOperationResult(true, "");
|
||||
|
||||
var errors = new StringBuilder();
|
||||
|
||||
@@ -91,12 +111,6 @@ public class SzEvaluator
|
||||
}
|
||||
|
||||
var errString = errors.ToString().Trim();
|
||||
return new SzEvaluationResult(string.IsNullOrEmpty(errString), errString);
|
||||
return new SzOperationResult(string.IsNullOrEmpty(errString), errString);
|
||||
}
|
||||
}
|
||||
|
||||
public class SzEvaluationResult(bool pass, string errors = "None")
|
||||
{
|
||||
public bool Pass = pass;
|
||||
public string Errors = errors;
|
||||
}
|
||||
|
||||
7
SzCore/SzOperationResult.cs
Normal file
7
SzCore/SzOperationResult.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace SzCore;
|
||||
|
||||
public class SzOperationResult(bool pass, string errors = "None")
|
||||
{
|
||||
public bool Pass = pass;
|
||||
public string Errors = errors;
|
||||
}
|
||||
Reference in New Issue
Block a user