public async Task Update(VariableSetDocument document, string prevId = null) { if (string.IsNullOrEmpty(document.Id)) { throw new ApplicationException("VariableSet ID cannot be null or empty!"); } var _prevId = prevId ?? document.Id; if (!string.Equals(document.Id, _prevId, StringComparison.OrdinalIgnoreCase)) { if (documents.TryGetValue(document.Id, out var _)) { throw new ApplicationException("A VariableSet with the specified ID already exists!"); } if (!string.IsNullOrEmpty(_prevId) && documents.TryRemove(_prevId, out var _prevDoc)) { File.Delete(_prevDoc.Filename); } } documents[document.Id] = document; if (string.IsNullOrEmpty(document.Filename)) { document.Filename = Path.Combine(path, $"{document.Id}.json"); } await document.SaveJson(); }
public void Load(string path) { this.path = path; documents.Clear(); if (!Directory.Exists(path)) { return; } foreach (var file in Directory.EnumerateFiles(path, "*.json", SearchOption.AllDirectories)) { var id = Path.GetFileNameWithoutExtension(file) ?? string.Empty; var document = new VariableSetDocument(file) { Id = id, }; documents[id] = document; } }
public bool TryGet(string id, out VariableSetDocument document) { return(documents.TryGetValue(id, out document)); }