Adding creation of data into data handler, marking the way for szdb handling
This commit is contained in:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user