Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 04e6a90

Browse files
committed
Added SqliteMessageDraftStore.
1 parent c7c7d25 commit 04e6a90

File tree

4 files changed

+6144
-0
lines changed

4 files changed

+6144
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.Composition;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
using GitHub.Extensions;
8+
using GitHub.Logging;
9+
using Newtonsoft.Json;
10+
using Rothko;
11+
using Serilog;
12+
using SQLite;
13+
14+
namespace GitHub.Services
15+
{
16+
/// <summary>
17+
/// Stores drafts of messages in an SQL database.
18+
/// </summary>
19+
[Export(typeof(IMessageDraftStore))]
20+
[PartCreationPolicy(CreationPolicy.Shared)]
21+
public class SqliteMessageDraftStore : IMessageDraftStore
22+
{
23+
static readonly ILogger log = LogManager.ForContext<SqliteMessageDraftStore>();
24+
readonly SQLiteAsyncConnection connection;
25+
26+
[ImportingConstructor]
27+
public SqliteMessageDraftStore(IOperatingSystem os)
28+
{
29+
var path = Path.Combine(
30+
os.Environment.GetApplicationDataPath(),
31+
"drafts.db");
32+
connection = new SQLiteAsyncConnection(path);
33+
}
34+
35+
public async Task<T> GetDraft<T>(string key, string secondaryKey) where T : class
36+
{
37+
Guard.ArgumentNotEmptyString(key, nameof(key));
38+
Guard.ArgumentNotNull(secondaryKey, nameof(secondaryKey));
39+
40+
try
41+
{
42+
var result = await connection.Table<Draft>().Where(
43+
x => x.Key == key && x.SecondaryKey == secondaryKey)
44+
.FirstOrDefaultAsync()
45+
.ConfigureAwait(false);
46+
47+
if (result != null)
48+
{
49+
return JsonConvert.DeserializeObject<T>(result.Data);
50+
}
51+
}
52+
catch (Exception ex)
53+
{
54+
log.Error(ex, "Failed to load message draft into {Type}", typeof(T));
55+
}
56+
57+
return null;
58+
}
59+
60+
public async Task<IEnumerable<(string secondaryKey, T data)>> GetDrafts<T>(string key) where T : class
61+
{
62+
Guard.ArgumentNotEmptyString(key, nameof(key));
63+
64+
try
65+
{
66+
var result = await connection.Table<Draft>().Where(x => x.Key == key)
67+
.ToListAsync()
68+
.ConfigureAwait(false);
69+
70+
return result.Select(x => (x.SecondaryKey, JsonConvert.DeserializeObject<T>(x.Data)));
71+
}
72+
catch (Exception ex)
73+
{
74+
log.Error(ex, "Failed to load message drafts into {Type}", typeof(T));
75+
}
76+
77+
return null;
78+
}
79+
80+
public async Task UpdateDraft<T>(string key, string secondaryKey, T data) where T : class
81+
{
82+
Guard.ArgumentNotEmptyString(key, nameof(key));
83+
Guard.ArgumentNotNull(secondaryKey, nameof(secondaryKey));
84+
85+
try
86+
{
87+
var row = new Draft
88+
{
89+
Key = key,
90+
SecondaryKey = secondaryKey,
91+
Data = JsonConvert.SerializeObject(data),
92+
};
93+
94+
await connection.InsertOrReplaceAsync(row).ConfigureAwait(false);
95+
}
96+
catch (Exception ex)
97+
{
98+
log.Error(ex, "Failed to update message draft");
99+
}
100+
}
101+
102+
public async Task DeleteDraft(string key, string secondaryKey)
103+
{
104+
Guard.ArgumentNotEmptyString(key, nameof(key));
105+
Guard.ArgumentNotNull(secondaryKey, nameof(secondaryKey));
106+
107+
try
108+
{
109+
await connection.ExecuteAsync(
110+
"DELETE FROM Drafts WHERE Key=? AND SecondaryKey=?",
111+
key,
112+
secondaryKey).ConfigureAwait(false);
113+
}
114+
catch (Exception ex)
115+
{
116+
log.Error(ex, "Failed to update message draft");
117+
}
118+
}
119+
120+
[Table("Drafts")]
121+
private class Draft
122+
{
123+
public string Key { get; set; }
124+
public string SecondaryKey { get; set; }
125+
public string Data { get; set; }
126+
}
127+
}
128+
}

0 commit comments

Comments
 (0)