Converting to Async

This commit is contained in:
2026-01-29 20:22:30 -06:00
parent 0dd597ee4e
commit 30236924a0
5 changed files with 148 additions and 124 deletions

View File

@@ -9,13 +9,13 @@ public class SzDataHandler
public SzDataHandler() { }
private void ClearCache()
public void ClearCache()
{
_loadedDatasets.Clear();
_loadedTemplates.Clear();
}
#region Datasets (Async)
#region Datasets
public async Task<SzResult<bool>> SaveDatasetAsync(SzDataset dataset, CancellationToken ct = default)
{
var datasetPath = Path.Combine(SZ.LocalFileManager.DatasetsPath, dataset.Id, "dataset.json");
@@ -90,7 +90,7 @@ public class SzDataHandler
}
#endregion
#region Templates (Async)
#region Templates
public async Task<SzResult<bool>> SaveTemplateAsync(ISzTemplate template, CancellationToken ct = default)
{
var templatePath = Path.Combine(SZ.LocalFileManager.TemplatesPath, template.Id, "template.json");
@@ -122,10 +122,41 @@ public class SzDataHandler
_loadedTemplates.TryAdd(parsed.Id, parsed);
return SzResult<T>.Success(parsed);
}
public async Task<SzResult<bool>> DeleteTemplateAsync(string templateId)
{
return await Task.Run(() => {
try
{
var path = Path.Combine(SZ.LocalFileManager.TemplatesPath, templateId);
if (Directory.Exists(path)) Directory.Delete(path, true);
_loadedTemplates.Remove(templateId);
return SzResult<bool>.Success(true);
}
catch (Exception e)
{
return SzResult<bool>.Failure(e.Message);
}
});
}
#endregion
#region Synchronous Logic
// These remain synchronous because they are purely in-memory operations.
#region Synchronous Creation Logic
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.");
return SzResult<SzTemplateField>.Success(new SzTemplateField()
{
Id = id,
FieldType = fieldType,
IsList = isList,
IsSpecialType = isSpecialType,
DefaultValue = defaultValue ?? "",
SpecialTypeValue = specialTypeValue ?? ""
});
}
public SzResult<SzDataObjectTemplate> CreateDataObjectTemplate(string name, string dataObjectType, string? id = null, string? description = null, List<SzTemplateField>? fields = null)
{
var newTemplate = new SzDataObjectTemplate()