-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathBasicCommands.cs
More file actions
133 lines (104 loc) · 4.83 KB
/
BasicCommands.cs
File metadata and controls
133 lines (104 loc) · 4.83 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright (c) Source Tree Solutions, LLC. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Author: Joe Audette
// Created: 2016-04-23
// Last Modified: 2016-05-15
//
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace NoDb
{
public class BasicCommands<T> : IBasicCommands<T> where T : class
{
public BasicCommands(
ILogger<BasicCommands<T>> logger,
IStoragePathResolver<T> pathResolver,
IStringSerializer<T> serializer
)
{
if (logger == null) { throw new ArgumentNullException(nameof(logger)); }
if (serializer == null) { throw new ArgumentNullException(nameof(serializer)); }
if (pathResolver == null) { throw new ArgumentNullException(nameof(pathResolver)); }
this.serializer = serializer;
this.pathResolver = pathResolver;
log = logger;
}
protected IStringSerializer<T> serializer;
protected IStoragePathResolver<T> pathResolver;
protected ILogger log;
public virtual async Task CreateAsync(
string projectId,
string key,
T obj,
CancellationToken cancellationToken = default(CancellationToken)
)
{
if (obj == null) throw new ArgumentException("TObject obj must be provided");
if (string.IsNullOrWhiteSpace(projectId)) throw new ArgumentException("projectId must be provided");
if (string.IsNullOrWhiteSpace(key)) throw new ArgumentException("key must be provided");
cancellationToken.ThrowIfCancellationRequested();
var pathToFile = await pathResolver.ResolvePath(
projectId,
key,
obj,
serializer.ExpectedFileExtension,
true
).ConfigureAwait(false);
if (File.Exists(pathToFile)) throw new InvalidOperationException("can't create file that already exists: " + pathToFile);
var serialized = serializer.Serialize(obj);
using (StreamWriter s = File.CreateText(pathToFile))
{
await s.WriteAsync(serialized);
}
}
public virtual async Task UpdateAsync(
string projectId,
string key,
T obj,
CancellationToken cancellationToken = default(CancellationToken)
)
{
if (obj == null) throw new ArgumentException("TObject obj must be provided");
if (string.IsNullOrWhiteSpace(projectId)) throw new ArgumentException("projectId must be provided");
if (string.IsNullOrWhiteSpace(key)) throw new ArgumentException("key must be provided");
cancellationToken.ThrowIfCancellationRequested();
var pathToFile = await pathResolver.ResolvePath(
projectId,
key,
obj,
serializer.ExpectedFileExtension,
false).ConfigureAwait(false);
if (!File.Exists(pathToFile)) throw new InvalidOperationException("can't update file that doesn't exist: " + pathToFile);
//TODO: if instead of deleting the existing file
// we just replace its contents then it opens the possibility
// for custom queries based on file creation and last modified dates
// whereas by deleting the file we lose the original creation date
File.Delete(pathToFile); // delete the old version
var serialized = serializer.Serialize(obj);
using (StreamWriter s = File.CreateText(pathToFile))
{
await s.WriteAsync(serialized);
}
}
public virtual async Task DeleteAsync(
string projectId,
string key,
CancellationToken cancellationToken = default(CancellationToken)
)
{
if (string.IsNullOrWhiteSpace(projectId)) throw new ArgumentException("projectId must be provided");
if (string.IsNullOrWhiteSpace(key)) throw new ArgumentException("key must be provided");
cancellationToken.ThrowIfCancellationRequested();
var pathToFile = await pathResolver.ResolvePath(
projectId,
key,
serializer.ExpectedFileExtension
).ConfigureAwait(false);
if (!File.Exists(pathToFile)) throw new InvalidOperationException("can't delete item that does not exist: " + pathToFile);
File.Delete(pathToFile);
}
}
}