From 3ed00f732a284b5a3e96e52d4e3a71869135869b Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Mon, 26 Jun 2023 02:43:54 +0200 Subject: Dependency injection stuff --- MatrixRoomUtils.Bot/Bot/FileStorageProvider.cs | 31 ++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 MatrixRoomUtils.Bot/Bot/FileStorageProvider.cs (limited to 'MatrixRoomUtils.Bot/Bot/FileStorageProvider.cs') diff --git a/MatrixRoomUtils.Bot/Bot/FileStorageProvider.cs b/MatrixRoomUtils.Bot/Bot/FileStorageProvider.cs new file mode 100644 index 0000000..8d99828 --- /dev/null +++ b/MatrixRoomUtils.Bot/Bot/FileStorageProvider.cs @@ -0,0 +1,31 @@ +using System.Text.Json; +using MatrixRoomUtils.Core.Extensions; +using MatrixRoomUtils.Core.Interfaces.Services; + +namespace MatrixRoomUtils.Bot; + +public class FileStorageProvider : IStorageProvider { + public string TargetPath { get; } + + /// + /// Creates a new instance of . + /// + /// + public FileStorageProvider(string targetPath) { + Console.WriteLine($"Initialised FileStorageProvider with path {targetPath}"); + TargetPath = targetPath; + if(!Directory.Exists(targetPath)) { + Directory.CreateDirectory(targetPath); + } + } + + public async Task SaveObject(string key, T value) => await File.WriteAllTextAsync(Path.Join(TargetPath, key), ObjectExtensions.ToJson(value)); + + public async Task LoadObject(string key) => JsonSerializer.Deserialize(await File.ReadAllTextAsync(Path.Join(TargetPath, key))); + + public async Task ObjectExists(string key) => File.Exists(Path.Join(TargetPath, key)); + + public async Task> GetAllKeys() => Directory.GetFiles(TargetPath).Select(Path.GetFileName).ToList(); + + public async Task DeleteObject(string key) => File.Delete(Path.Join(TargetPath, key)); +} \ No newline at end of file -- cgit 1.4.1