feat: implement delete async method on json storage provider
This commit is contained in:
+22
-10
@@ -30,19 +30,17 @@ public sealed class JsonFileStorageProvider(JsonStorageOptions options)
|
||||
|
||||
public async Task WriteAsync(string key, string encryptedData, CancellationToken ct = default)
|
||||
{
|
||||
Directory.CreateDirectory(_options.DirectoryPath);
|
||||
|
||||
var data = await LoadAsync(ct);
|
||||
data[key] = encryptedData;
|
||||
await SaveAsync(data, ct);
|
||||
}
|
||||
|
||||
using var stream = new FileStream(
|
||||
_options.FullPath,
|
||||
FileMode.Create,
|
||||
FileAccess.Write,
|
||||
FileShare.None
|
||||
);
|
||||
|
||||
await JsonSerializer.SerializeAsync(stream, data, JsonOptions, ct);
|
||||
public async Task<bool> DeleteAsync(string key, CancellationToken ct = default)
|
||||
{
|
||||
var data = await LoadAsync(ct);
|
||||
var result = data.Remove(key);
|
||||
await SaveAsync(data, ct);
|
||||
return result;
|
||||
}
|
||||
|
||||
private async Task<Dictionary<string, string>> LoadAsync(CancellationToken ct)
|
||||
@@ -63,4 +61,18 @@ public sealed class JsonFileStorageProvider(JsonStorageOptions options)
|
||||
|
||||
return data ?? [];
|
||||
}
|
||||
|
||||
private async Task SaveAsync(Dictionary<string, string> data, CancellationToken ct)
|
||||
{
|
||||
Directory.CreateDirectory(_options.DirectoryPath);
|
||||
|
||||
using var stream = new FileStream(
|
||||
_options.FullPath,
|
||||
FileMode.Create,
|
||||
FileAccess.Write,
|
||||
FileShare.None
|
||||
);
|
||||
|
||||
await JsonSerializer.SerializeAsync(stream, data, JsonOptions, ct);
|
||||
}
|
||||
}
|
||||
+12
@@ -58,6 +58,18 @@ public class JsonFileStorageProviderTests : IDisposable
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteAsync_WhenCalled_ItShouldRemoveKey()
|
||||
{
|
||||
await _sut.WriteAsync("KeyToDelete", "SomeValue");
|
||||
|
||||
var deleteResult = await _sut.DeleteAsync("KeyToDelete");
|
||||
var readResult = await _sut.ReadAsync("KeyToDelete");
|
||||
|
||||
deleteResult.Should().BeTrue();
|
||||
readResult.Should().BeEmpty();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (Directory.Exists(_tmpDirectory))
|
||||
|
||||
Reference in New Issue
Block a user