Dependency injection stuff
1 files changed, 31 insertions, 0 deletions
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; }
+
+ /// <summary>
+ /// Creates a new instance of <see cref="FileStorageProvider" />.
+ /// </summary>
+ /// <param name="targetPath"></param>
+ public FileStorageProvider(string targetPath) {
+ Console.WriteLine($"Initialised FileStorageProvider with path {targetPath}");
+ TargetPath = targetPath;
+ if(!Directory.Exists(targetPath)) {
+ Directory.CreateDirectory(targetPath);
+ }
+ }
+
+ public async Task SaveObject<T>(string key, T value) => await File.WriteAllTextAsync(Path.Join(TargetPath, key), ObjectExtensions.ToJson(value));
+
+ public async Task<T?> LoadObject<T>(string key) => JsonSerializer.Deserialize<T>(await File.ReadAllTextAsync(Path.Join(TargetPath, key)));
+
+ public async Task<bool> ObjectExists(string key) => File.Exists(Path.Join(TargetPath, key));
+
+ public async Task<List<string>> 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
|