about summary refs log tree commit diff
path: root/LibMatrix/Interfaces/Services/IStorageProvider.cs
blob: f7e54884e0125eea2e49a2c50c5d4c2598ea3e51 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using LibMatrix.Responses;

namespace LibMatrix.Interfaces.Services;

public interface IStorageProvider {
    // save all children of a type with reflection
    public Task SaveAllChildrenAsync<T>(string key, T value) {
        Console.WriteLine($"StorageProvider<{GetType().Name}> does not implement SaveAllChildren<T>(key, value)!");
        throw new NotImplementedException();
    }

    // load all children of a type with reflection
    public Task<T?> LoadAllChildrenAsync<T>(string key) {
        Console.WriteLine($"StorageProvider<{GetType().Name}> does not implement LoadAllChildren<T>(key)!");
        throw new NotImplementedException();
    }

    public Task SaveObjectAsync<T>(string key, T value) {
        Console.WriteLine($"StorageProvider<{GetType().Name}> does not implement SaveObject<T>(key, value)!");
        throw new NotImplementedException();
    }
    
    public Task SaveObjectAsync<T>(string key, T value, JsonTypeInfo<T> jsonTypeInfo) {
        Console.WriteLine($"StorageProvider<{GetType().Name}> does not implement SaveObjectAsync<T>(key, value, typeInfo), using default implementation w/ MemoryStream!");
        var ms = new MemoryStream();
        JsonSerializer.Serialize(ms, value, jsonTypeInfo);
        return SaveStreamAsync(key, ms);
    }

    // load
    public Task<T?> LoadObjectAsync<T>(string key) {
        Console.WriteLine($"StorageProvider<{GetType().Name}> does not implement LoadObject<T>(key)!");
        throw new NotImplementedException();
    }

    public async Task<T?> LoadObjectAsync<T>(string key, JsonTypeInfo<T> jsonTypeInfo) {
        Console.WriteLine($"StorageProvider<{GetType().Name}> does not implement SaveObject<T>(key, typeInfo), using default implementation!");
        await using var stream = await LoadStreamAsync(key);
        return JsonSerializer.Deserialize(stream!, jsonTypeInfo);
    }

    // check if exists
    public Task<bool> ObjectExistsAsync(string key) {
        Console.WriteLine($"StorageProvider<{GetType().Name}> does not implement ObjectExists(key)!");
        throw new NotImplementedException();
    }

    // get all keys
    public Task<IEnumerable<string>> GetAllKeysAsync() {
        Console.WriteLine($"StorageProvider<{GetType().Name}> does not implement GetAllKeys()!");
        throw new NotImplementedException();
    }

    // delete
    public Task DeleteObjectAsync(string key) {
        Console.WriteLine($"StorageProvider<{GetType().Name}> does not implement DeleteObject(key)!");
        throw new NotImplementedException();
    }

    // save stream
    public Task SaveStreamAsync(string key, Stream stream) {
        Console.WriteLine($"StorageProvider<{GetType().Name}> does not implement SaveStream(key, stream)!");
        throw new NotImplementedException();
    }

    // load stream
    public Task<Stream?> LoadStreamAsync(string key) {
        Console.WriteLine($"StorageProvider<{GetType().Name}> does not implement LoadStream(key)!");
        throw new NotImplementedException();
    }

    // copy
    public async Task CopyObjectAsync(string sourceKey, string destKey) {
        Console.WriteLine($"StorageProvider<{GetType().Name}> does not implement CopyObject(sourceKey, destKey), using load + save!");
        var data = await LoadObjectAsync<object>(sourceKey);
        await SaveObjectAsync(destKey, data);
    }

    // move
    public async Task MoveObjectAsync(string sourceKey, string destKey) {
        Console.WriteLine($"StorageProvider<{GetType().Name}> does not implement MoveObject(sourceKey, destKey), using copy + delete!");
        await CopyObjectAsync(sourceKey, destKey);
        await DeleteObjectAsync(sourceKey);
    }
}