-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathGistItemGateway.cs
More file actions
206 lines (166 loc) · 6.34 KB
/
GistItemGateway.cs
File metadata and controls
206 lines (166 loc) · 6.34 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
namespace TodoList.Infrastructure.GistGateway
{
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Octokit;
using TodoList.Core.Entities;
using TodoList.Core.Gateways;
using TodoList.Core.Exceptions;
public sealed class GistItemGateway : IItemGateway
{
private static GitHubClient _gitHubClient;
private GitHubClient GitHubClient
{
get
{
if (_gitHubClient != null)
return _gitHubClient;
_gitHubClient = new GitHubClient(new ProductHeaderValue("todo"));
_gitHubClient.Credentials = new Credentials(_gistToken);
return _gitHubClient;
}
}
private string _gistToken;
private string _gistId;
private List<Item> EnsureItemsAreLoaded()
{
if (string.IsNullOrWhiteSpace(_gistId))
{
List<Item> emptyItems = new List<Item>();
var gist = CreateGist(emptyItems);
return emptyItems;
}
try
{
Gist gist = LoadGist();
string jsonContents = gist.Files["todolist.json"].Content;
var items = JsonConvert.DeserializeObject<List<JsonItem>>(jsonContents);
return items.Cast<Item>().ToList();
}
catch (Octokit.NotFoundException)
{
List<Item> emptyItems = new List<Item>();
var gist = CreateGist(emptyItems);
return emptyItems;
}
}
private Gist LoadGist()
{
var gist = GitHubClient
.Gist
.Get(_gistId).GetAwaiter()
.GetResult();
return gist;
}
private Gist CreateGist(List<Item> items)
{
var jsonContents = JsonConvert.SerializeObject(items, Formatting.Indented);
var newGist = new NewGist();
newGist.Description = "todolist";
newGist.Files.Add("todolist.json", jsonContents);
var gist = GitHubClient.Gist
.Create(newGist)
.GetAwaiter()
.GetResult();
_gistId = gist.Id;
WriteGistIdToAppSettings();
return gist;
}
private void UpdateGist(List<Item> items)
{
var jsonContents = JsonConvert.SerializeObject(items, Formatting.Indented);
var gistFileUpdate = new GistFileUpdate();
gistFileUpdate.Content = jsonContents;
gistFileUpdate.NewFileName = "todolist.json";
var gistUpdate = new GistUpdate();
gistUpdate.Description = "todolist";
gistUpdate.Files.Add("todolist.json", gistFileUpdate);
GitHubClient.Gist
.Edit(_gistId, gistUpdate)
.GetAwaiter()
.GetResult();
}
private void ReadGitHubCredentialsFromAppSettings()
{
var appsettingsPath = Path.GetDirectoryName(
Assembly.GetExecutingAssembly().Location) +
Path.DirectorySeparatorChar +
"appsettings.json";
if (!File.Exists(appsettingsPath))
throw new InfrastructureException($"The appsettings file `({appsettingsPath})` does not exit.");
var contents = File.ReadAllText(appsettingsPath);
var configuration = JsonConvert.DeserializeObject<JObject>(contents);
_gistToken = configuration["GistToken"].Value<string>();
_gistId = configuration["GistId"].Value<string>();
if (string.IsNullOrWhiteSpace(_gistToken))
throw new InfrastructureException($"The Gist Token was not setup. Create one at https://github.com/settings/tokens.");
}
private void WriteGistIdToAppSettings()
{
var appsettingsPath = Path.GetDirectoryName(
Assembly.GetExecutingAssembly().Location) +
Path.DirectorySeparatorChar +
"appsettings.json";
if (!File.Exists(appsettingsPath))
throw new InfrastructureException($"The appsettings file `({appsettingsPath})` does not exit.");
var contents = File.ReadAllText(appsettingsPath);
var configuration = JsonConvert.DeserializeObject<JObject>(contents);
configuration["GistId"] = _gistId;
contents = JsonConvert.SerializeObject(configuration, Formatting.Indented);
File.WriteAllText(appsettingsPath, contents);
}
public void Add(IItem item)
{
ReadGitHubCredentialsFromAppSettings();
var items = EnsureItemsAreLoaded();
items.Add((Item) item);
UpdateGist(items);
}
public void Delete(string itemId)
{
ReadGitHubCredentialsFromAppSettings();
var items = EnsureItemsAreLoaded();
Item item = items
.SingleOrDefault(e => e.Id.ToString().StartsWith(itemId));
if (item != null)
{
items.Remove(item);
UpdateGist(items);
}
}
public IItem Get(string itemId)
{
ReadGitHubCredentialsFromAppSettings();
var items = EnsureItemsAreLoaded();
Item item = items
.SingleOrDefault(e => e.Id.ToString().StartsWith(itemId));
return item;
}
public IList<IItem> List()
{
ReadGitHubCredentialsFromAppSettings();
var items = EnsureItemsAreLoaded();
return items.Cast<IItem>().ToList();
}
public void Update(IItem item)
{
ReadGitHubCredentialsFromAppSettings();
var items = EnsureItemsAreLoaded();
Item currentItem = items
.SingleOrDefault(e => e.Id.ToString()
.StartsWith(item.Id.ToString()));
if (currentItem != null)
{
items.Remove(currentItem);
items.Add((Item) item);
UpdateGist(items);
}
}
}
}