-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequest.cs
More file actions
104 lines (82 loc) · 2.76 KB
/
Request.cs
File metadata and controls
104 lines (82 loc) · 2.76 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
using LiteDB;
namespace tx_wp_api {
public class ProcessingRequest {
private string _retrieveDocumentUrl;
public string Id { get; set; }
public bool Processed { get; set; }
public string? WebHookUrl { get; set; }
public string RetrieveDocumentUrl {
get { return _retrieveDocumentUrl; }
set { _retrieveDocumentUrl = value; }
}
public ProcessingRequest() { }
public ProcessingRequest(string? Id = null) {
using (var db = new LiteDatabase(@"Filename=App_Data/processingqueue.db; Connection=shared")) {
var col = db.GetCollection<ProcessingRequest>("queue");
col.EnsureIndex(x => x.Id);
col.EnsureIndex(x => x.Processed);
ILiteQueryable<ProcessingRequest> results;
// if no id is passed, get the first unprocessed request
if (Id == null)
{
results = col.Query().Where(x => x.Processed == false);
}
else
{
results = col.Query().Where(x => x.Id == Id);
}
// if no results are found, return
if (results.Count() == 0)
{
return;
}
// if results are found, set properties
if (results.First() != null) {
this.WebHookUrl = results.First().WebHookUrl;
this.Id = results.First().Id;
this.Processed = results.First().Processed;
this.RetrieveDocumentUrl = results.First().RetrieveDocumentUrl;
}
}
}
public void StoreDocument(byte[] document) {
using (var db = new LiteDatabase(@"Filename=App_Data/documents.db; Connection=shared")) {
MemoryStream stream = new MemoryStream(document);
// upload document to db
var fs = db.FileStorage;
stream.Position = 0;
var test = fs.Upload("$/templates/" + this.Id, this.Id, stream);
}
}
public string RetrieveDocument() {
using (var db = new LiteDatabase(@"Filename=App_Data/documents.db; Connection=shared")) {
var fs = db.FileStorage;
using (MemoryStream ms = new MemoryStream()) {
if (fs.Exists("$/templates/" + this.Id) == true) {
fs.Download("$/templates/" + this.Id, ms);
return Convert.ToBase64String(ms.ToArray());
}
else
return null;
}
};
}
public void Create(string requestUrl) {
this.Id = Guid.NewGuid().ToString();
this.RetrieveDocumentUrl = requestUrl.Replace("/1", "/" + this.Id);
using (var db = new LiteDatabase(@"Filename=App_Data/processingqueue.db; Connection=shared")) {
var col = db.GetCollection<ProcessingRequest>("queue");
col.Insert(this);
}
}
public void Update() {
using (var db = new LiteDatabase(@"Filename=App_Data/processingqueue.db; Connection=shared")) {
var col = db.GetCollection<ProcessingRequest>("queue");
col.EnsureIndex(x => x.Id);
var results = col.Query().Where(x => x.Id == this.Id).First();
results.Processed = this.Processed;
col.Update(results);
}
}
}
}