From 8fdc48e5b21b1eea61534b181585858727500f34 Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Mon, 14 Aug 2023 04:26:27 +0200 Subject: Move more projects over --- LibMatrix.ExampleBot/Bot/FileStorageProvider.cs | 35 +++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 LibMatrix.ExampleBot/Bot/FileStorageProvider.cs (limited to 'LibMatrix.ExampleBot/Bot/FileStorageProvider.cs') 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 _logger; + + public string TargetPath { get; } + + /// + /// Creates a new instance of . + /// + /// + public FileStorageProvider(string targetPath) { + new Logger(new LoggerFactory()).LogInformation("test"); + Console.WriteLine($"Initialised FileStorageProvider with path {targetPath}"); + TargetPath = targetPath; + if(!Directory.Exists(targetPath)) { + Directory.CreateDirectory(targetPath); + } + } + + public async Task SaveObjectAsync(string key, T value) => await File.WriteAllTextAsync(Path.Join(TargetPath, key), ObjectExtensions.ToJson(value)); + + public async Task LoadObjectAsync(string key) => JsonSerializer.Deserialize(await File.ReadAllTextAsync(Path.Join(TargetPath, key))); + + public async Task ObjectExistsAsync(string key) => File.Exists(Path.Join(TargetPath, key)); + + public async Task> GetAllKeysAsync() => Directory.GetFiles(TargetPath).Select(Path.GetFileName).ToList(); + + public async Task DeleteObjectAsync(string key) => File.Delete(Path.Join(TargetPath, key)); +} -- cgit 1.4.1