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)
|
public async Task WriteAsync(string key, string encryptedData, CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
Directory.CreateDirectory(_options.DirectoryPath);
|
|
||||||
|
|
||||||
var data = await LoadAsync(ct);
|
var data = await LoadAsync(ct);
|
||||||
data[key] = encryptedData;
|
data[key] = encryptedData;
|
||||||
|
await SaveAsync(data, ct);
|
||||||
|
}
|
||||||
|
|
||||||
using var stream = new FileStream(
|
public async Task<bool> DeleteAsync(string key, CancellationToken ct = default)
|
||||||
_options.FullPath,
|
{
|
||||||
FileMode.Create,
|
var data = await LoadAsync(ct);
|
||||||
FileAccess.Write,
|
var result = data.Remove(key);
|
||||||
FileShare.None
|
await SaveAsync(data, ct);
|
||||||
);
|
return result;
|
||||||
|
|
||||||
await JsonSerializer.SerializeAsync(stream, data, JsonOptions, ct);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<Dictionary<string, string>> LoadAsync(CancellationToken ct)
|
private async Task<Dictionary<string, string>> LoadAsync(CancellationToken ct)
|
||||||
@@ -63,4 +61,18 @@ public sealed class JsonFileStorageProvider(JsonStorageOptions options)
|
|||||||
|
|
||||||
return data ?? [];
|
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()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
if (Directory.Exists(_tmpDirectory))
|
if (Directory.Exists(_tmpDirectory))
|
||||||
|
|||||||
Reference in New Issue
Block a user