More datahandler stuff, removed SzOperationResult.cs and made a new SzResult.cs and changed Evaluator and DataHander to utilize it
This commit is contained in:
@@ -10,7 +10,6 @@ public class SzDataHandler
|
||||
|
||||
public SzDataHandler(){}
|
||||
|
||||
// The client should periodically call this to free memory, probably
|
||||
private void ClearCache()
|
||||
{
|
||||
_loadedDatasets.Clear();
|
||||
@@ -18,39 +17,40 @@ public class SzDataHandler
|
||||
}
|
||||
|
||||
#region Datasets
|
||||
public bool SaveDataset(SzDataset dataset)
|
||||
public SzResult<bool> SaveDataset(SzDataset dataset)
|
||||
{
|
||||
var datasetPath = Path.Combine(SZ.LocalFileManager.DatasetsPath, dataset.Id, "dataset.json");
|
||||
try
|
||||
{
|
||||
return SZ.LocalFileManager.SaveFile(datasetPath, SZ.SzParser.SerializeDatasetToJson(dataset));
|
||||
var success = SZ.LocalFileManager.SaveFile(datasetPath, SZ.Parser.SerializeDatasetToJson(dataset));
|
||||
return success ? SzResult<bool>.Success(true) : SzResult<bool>.Failure("Failed to write dataset file.");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
SZ.Logger.LogError("Error saving dataset: " + e.Message);
|
||||
return false;
|
||||
return SzResult<bool>.Failure(e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public bool DeleteDataset(string datasetId)
|
||||
public SzResult<bool> DeleteDataset(string datasetId)
|
||||
{
|
||||
try
|
||||
{
|
||||
Directory.Delete(Path.Combine(SZ.LocalFileManager.DatasetsPath, datasetId), true);
|
||||
return true;
|
||||
return SzResult<bool>.Success(true);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
SZ.Logger.LogError($"Could not delete Dataset with id '{datasetId}': {e.Message}");
|
||||
return false;
|
||||
return SzResult<bool>.Failure(e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public SzDataset? LoadDataset(string datasetId)
|
||||
public SzResult<SzDataset> LoadDataset(string datasetId)
|
||||
{
|
||||
if (_loadedDatasets.TryGetValue(datasetId, out SzDataset? value))
|
||||
{
|
||||
return value;
|
||||
return SzResult<SzDataset>.Success(value);
|
||||
}
|
||||
|
||||
var datasetPath = Path.Combine(SZ.LocalFileManager.DataPath, "datasets", datasetId, "dataset.json");
|
||||
@@ -59,61 +59,78 @@ public class SzDataHandler
|
||||
if (json is null)
|
||||
{
|
||||
SZ.Logger.LogWarning($"Could not load dataset with ID '{datasetId}'");
|
||||
return null;
|
||||
return SzResult<SzDataset>.Failure($"Could not find dataset file for ID '{datasetId}'");
|
||||
}
|
||||
|
||||
var parsedDataset = SZ.SzParser.DeserializeDataset(json);
|
||||
var parsedDataset = SZ.Parser.DeserializeDataset(json);
|
||||
if (parsedDataset is null)
|
||||
{
|
||||
SZ.Logger.LogWarning($"Could not load dataset with ID '{datasetId}'");
|
||||
return null;
|
||||
return SzResult<SzDataset>.Failure($"Failed to parse dataset JSON for ID '{datasetId}'");
|
||||
}
|
||||
|
||||
_loadedDatasets.Add(parsedDataset.Id, parsedDataset);
|
||||
return parsedDataset;
|
||||
return SzResult<SzDataset>.Success(parsedDataset);
|
||||
}
|
||||
|
||||
public SzDataset CreateDataset()
|
||||
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)
|
||||
return SzResult<SzDataset>.Failure($"Template '{templateId}' not found.");
|
||||
|
||||
var newDataset = new SzDataset()
|
||||
{
|
||||
Name = name,
|
||||
Id = id ?? name.ToLower().Replace(" ", "-").Trim(),
|
||||
DataObjectTemplateId = templateResult.Value.Id,
|
||||
DataObjectTemplateUuid = templateUuid,
|
||||
DataObjectType = templateResult.Value.DataObjectType,
|
||||
Uuid = Guid.NewGuid()
|
||||
};
|
||||
|
||||
return SzResult<SzDataset>.Success(newDataset);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Templates
|
||||
public bool SaveTemplate(ISzTemplate template)
|
||||
public SzResult<bool> SaveTemplate(ISzTemplate template)
|
||||
{
|
||||
var templatePath = Path.Combine(SZ.LocalFileManager.TemplatesPath, template.Id, "template.json");
|
||||
try
|
||||
{
|
||||
return SZ.LocalFileManager.SaveFile(templatePath, SZ.SzParser.SerializeTemplateToJson(template));
|
||||
var success = SZ.LocalFileManager.SaveFile(templatePath, SZ.Parser.SerializeTemplateToJson(template));
|
||||
return success ? SzResult<bool>.Success(true) : SzResult<bool>.Failure("Failed to write template file.");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
SZ.Logger.LogError("Error saving template: " + e.Message);
|
||||
return false;
|
||||
return SzResult<bool>.Failure(e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public bool DeleteTemplate(string templateId)
|
||||
public SzResult<bool> DeleteTemplate(string templateId)
|
||||
{
|
||||
try
|
||||
{
|
||||
Directory.Delete(Path.Combine(SZ.LocalFileManager.TemplatesPath, templateId), true);
|
||||
return true;
|
||||
return SzResult<bool>.Success(true);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
SZ.Logger.LogError($"Could not delete Template with id '{templateId}': {e.Message}");
|
||||
return false;
|
||||
return SzResult<bool>.Failure(e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public T? LoadTemplate<T>(string templateId) where T : ISzTemplate
|
||||
public SzResult<T> LoadTemplate<T>(string templateId) where T : ISzTemplate
|
||||
{
|
||||
if (_loadedTemplates.TryGetValue(templateId, out ISzTemplate? existing))
|
||||
if (_loadedTemplates.TryGetValue(templateId, out ISzTemplate? cachedTemplate))
|
||||
{
|
||||
return (T)existing;
|
||||
return SzResult<T>.Success((T)cachedTemplate);
|
||||
}
|
||||
|
||||
var templatePah = Path.Combine(SZ.LocalFileManager.TemplatesPath, templateId, "template.json");
|
||||
@@ -121,63 +138,50 @@ public class SzDataHandler
|
||||
var json = SZ.LocalFileManager.LoadFile(templatePah);
|
||||
if (json is null)
|
||||
{
|
||||
SZ.Logger.LogWarning($"Could not load tetmplate with ID '{templateId}'");
|
||||
return default;
|
||||
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.SzParser.DeserializeTemplate<T>(json);
|
||||
var parsedTemplate = SZ.Parser.DeserializeTemplate<T>(json);
|
||||
if (parsedTemplate is null)
|
||||
{
|
||||
SZ.Logger.LogWarning($"Could not load template with ID '{templateId}'");
|
||||
return default;
|
||||
return SzResult<T>.Failure($"Failed to parse template JSON for ID '{templateId}'");
|
||||
}
|
||||
|
||||
_loadedTemplates.TryAdd(parsedTemplate.Id, parsedTemplate);
|
||||
|
||||
return parsedTemplate;
|
||||
return SzResult<T>.Success(parsedTemplate);
|
||||
}
|
||||
|
||||
public 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) || string.IsNullOrEmpty(dataObjectType)) return null;
|
||||
|
||||
string newId;
|
||||
if (!string.IsNullOrEmpty(id))
|
||||
{
|
||||
newId = id;
|
||||
}
|
||||
else
|
||||
{
|
||||
newId = name.ToLower().Replace(" ", "-").Trim();
|
||||
}
|
||||
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()
|
||||
{
|
||||
Name = name,
|
||||
Id = newId,
|
||||
Id = id ?? name.ToLower().Replace(" ", "-").Trim(),
|
||||
DataObjectType = dataObjectType,
|
||||
Uuid = Guid.NewGuid(),
|
||||
Description = description ?? ""
|
||||
};
|
||||
|
||||
if (fields is not null)
|
||||
if (fields is null) return SzResult<SzDataObjectTemplate>.Success(newTemplate);
|
||||
foreach (var field in fields)
|
||||
{
|
||||
foreach (var field in fields)
|
||||
{
|
||||
newTemplate.TemplateFields.Add(field.Id, field);
|
||||
}
|
||||
newTemplate.TemplateFields.Add(field.Id, field);
|
||||
}
|
||||
|
||||
return newTemplate;
|
||||
|
||||
return SzResult<SzDataObjectTemplate>.Success(newTemplate);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Other
|
||||
|
||||
public SzTemplateField? CreateTemplateField(string id, SzFieldType fieldType, bool isList = false, string? defaultValue = null, bool isSpecialType = false, string? specialTypeValue = 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 null;
|
||||
if (string.IsNullOrEmpty(id)) return SzResult<SzTemplateField>.Failure("ID cannot be empty.");
|
||||
|
||||
var newField = new SzTemplateField()
|
||||
{
|
||||
@@ -189,13 +193,21 @@ public class SzDataHandler
|
||||
SpecialTypeValue = specialTypeValue ?? ""
|
||||
};
|
||||
|
||||
return newField;
|
||||
return SzResult<SzTemplateField>.Success(newField);
|
||||
}
|
||||
|
||||
public SzResult<List<SzField>> CreateFields(ISzTemplate template)
|
||||
{
|
||||
return SzResult<List<SzField>>.Failure("Not implemented");
|
||||
}
|
||||
|
||||
public SzResult<SzDataObject> CreateDataObject()
|
||||
{
|
||||
return SzResult<SzDataObject>.Failure("Not implemented");
|
||||
}
|
||||
#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()
|
||||
{
|
||||
_loadedDatasets.Clear();
|
||||
@@ -212,7 +224,7 @@ public class SzDataHandler
|
||||
var datasetJson = SZ.LocalFileManager.LoadFile(datasetFilePath);
|
||||
if (datasetJson == null) continue;
|
||||
|
||||
var parsedDataset = SZ.SzParser.DeserializeDataset(datasetJson);
|
||||
var parsedDataset = SZ.Parser.DeserializeDataset(datasetJson);
|
||||
if (parsedDataset == null)
|
||||
{
|
||||
SZ.Logger.LogWarning($"Parse error: Could not parse file {datasetFilePath} as an SzDataset");
|
||||
@@ -239,7 +251,7 @@ public class SzDataHandler
|
||||
var templateJson = SZ.LocalFileManager.LoadFile(templateFilePath);
|
||||
if (templateJson == null) continue;
|
||||
|
||||
var parsedTemplate = SZ.SzParser.DeserializeTemplate<ISzTemplate>(templateJson);
|
||||
var parsedTemplate = SZ.Parser.DeserializeTemplate<ISzTemplate>(templateJson);
|
||||
if (parsedTemplate == null)
|
||||
{
|
||||
SZ.Logger.LogWarning($"Parse error: Could not parse file {templateFilePath} as an ISzTemplate");
|
||||
|
||||
Reference in New Issue
Block a user