diff options
author | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-08-14 04:26:27 +0200 |
---|---|---|
committer | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-08-14 04:26:27 +0200 |
commit | 8fdc48e5b21b1eea61534b181585858727500f34 (patch) | |
tree | 71cc2655ca1e2b5556c2c7c1ad550c5df6b499ef /LibMatrix.ExampleBot/Bot/FileStorageProvider.cs | |
parent | Split LibMatrix into separate repo (diff) | |
download | LibMatrix-8fdc48e5b21b1eea61534b181585858727500f34.tar.xz |
Move more projects over
Diffstat (limited to 'LibMatrix.ExampleBot/Bot/FileStorageProvider.cs')
-rw-r--r-- | LibMatrix.ExampleBot/Bot/FileStorageProvider.cs | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/LibMatrix.ExampleBot/Bot/FileStorageProvider.cs b/LibMatrix.ExampleBot/Bot/FileStorageProvider.cs new file mode 100644 index 0000000..249aba3 --- /dev/null +++ b/LibMatrix.ExampleBot/Bot/FileStorageProvider.cs @@ -0,0 +1,35 @@ +using System.Text.Json; +using LibMatrix.Extensions; +using LibMatrix.Interfaces.Services; +using Microsoft.Extensions.Logging; + +namespace LibMatrix.ExampleBot.Bot; + +public class FileStorageProvider : IStorageProvider { + private readonly ILogger<FileStorageProvider> _logger; + + public string TargetPath { get; } + + /// <summary> + /// Creates a new instance of <see cref="FileStorageProvider" />. + /// </summary> + /// <param name="targetPath"></param> + public FileStorageProvider(string targetPath) { + new Logger<FileStorageProvider>(new LoggerFactory()).LogInformation("test"); + Console.WriteLine($"Initialised FileStorageProvider with path {targetPath}"); + TargetPath = targetPath; + if(!Directory.Exists(targetPath)) { + Directory.CreateDirectory(targetPath); + } + } + + public async Task SaveObjectAsync<T>(string key, T value) => await File.WriteAllTextAsync(Path.Join(TargetPath, key), ObjectExtensions.ToJson(value)); + + public async Task<T?> LoadObjectAsync<T>(string key) => JsonSerializer.Deserialize<T>(await File.ReadAllTextAsync(Path.Join(TargetPath, key))); + + public async Task<bool> ObjectExistsAsync(string key) => File.Exists(Path.Join(TargetPath, key)); + + public async Task<List<string>> GetAllKeysAsync() => Directory.GetFiles(TargetPath).Select(Path.GetFileName).ToList(); + + public async Task DeleteObjectAsync(string key) => File.Delete(Path.Join(TargetPath, key)); +} |