-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebHooksConfigController.cs
146 lines (123 loc) · 5.12 KB
/
WebHooksConfigController.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Countersoft.Foundation.Commons.Extensions;
using Countersoft.Gemini.Commons.Entity;
using Countersoft.Gemini.Contracts;
using Countersoft.Gemini.Extensibility.Apps;
using Countersoft.Gemini.Infrastructure.Apps;
namespace Webhooks
{
[AppType(AppTypeEnum.Config),
AppControlGuid("FF74C706-49BA-4D81-8F4D-A55C26D2CADF"),
AppKey("Webhooks"),
AppGuid(AppConstants.AppId),
AppAuthor("Countersoft"),
AppName("Webhooks"),
AppDescription("Call web hooks when items are create and / or updated"),
AppRequiresConfigScreen(true)]
[OutputCache(Duration = 0, NoStore = true, Location = System.Web.UI.OutputCacheLocation.None)]
public class WebHooksConfigController : BaseAppController
{
public override WidgetResult Caption(Countersoft.Gemini.Commons.Dto.IssueDto issue = null)
{
WidgetResult result = new WidgetResult();
result.Success = true;
result.Markup.Html = "Webhooks";
return result;
}
public override WidgetResult Show(Countersoft.Gemini.Commons.Dto.IssueDto issue = null)
{
return new WidgetResult();
}
public override WidgetResult Configuration()
{
var data = GetData();
WidgetResult result = new WidgetResult();
result.Markup = new WidgetMarkup("views/webhooks.cshtml", data.Value);
result.Success = true;
return result;
}
[AppUrl("getproperty")]
public ActionResult GetProperty(string id, string property)
{
var data = GetData();
var row = data.Value.Find(u => u.Url.Equals(id, StringComparison.InvariantCultureIgnoreCase));
if (row == null) return JsonError();
switch (property.ToLowerInvariant())
{
case "description":
return Content(row.Description);
case "url":
return Content(row.Url);
case "create":
return Content(row.Create.ToString());
case "update":
return Content(row.Update.ToString());
}
GeminiContext.GlobalConfigurationWidgetStore.Save<List<WebhooksData>>(AppConstants.AppId, data.Value);
return JsonSuccess();
}
[AppUrl("saveproperty")]
public ActionResult SaveProperty(string id, string property, string value)
{
var data = GetData();
var row = data.Value.Find(u => u.Url.Equals(id, StringComparison.InvariantCultureIgnoreCase));
if (row == null) return JsonError();
string returnData = value;
switch (property.ToLowerInvariant())
{
case "description":
row.Description = value;
break;
case "url":
row.Url = value;
break;
case "create":
row.Create = value.ToBool();
returnData = row.Create ? "Yes" : string.Empty;
break;
case "update":
row.Update = value.ToBool();
returnData = row.Update ? "Yes" : string.Empty;
break;
}
GeminiContext.GlobalConfigurationWidgetStore.Save<List<WebhooksData>>(AppConstants.AppId, data.Value);
return Content(returnData);
}
[AppUrl("save")]
public ActionResult Save(WebhooksData hook)
{
var data = GetData();
if (data.Value.Find(u => u.Url.Equals(hook.Url, StringComparison.InvariantCultureIgnoreCase)) != null) return JsonError("Duplicate Entry");
data.Value.Add(hook);
GeminiContext.GlobalConfigurationWidgetStore.Save<List<WebhooksData>>(AppConstants.AppId, data.Value);
return JsonSuccess();
}
[AppUrl("delete")]
public ActionResult Delete(string url)
{
var data = GetData();
var dataUrl = data.Value.Find(u => u.Url.Equals(url, StringComparison.InvariantCultureIgnoreCase));
if (dataUrl == null)
{
return JsonError("Cannot find url");
}
data.Value.Remove(dataUrl);
GeminiContext.GlobalConfigurationWidgetStore.Save<List<WebhooksData>>(AppConstants.AppId, data.Value);
return JsonSuccess();
}
private GlobalConfigurationWidgetData<List<WebhooksData>> GetData()
{
var data = GeminiContext.GlobalConfigurationWidgetStore.Get<List<WebhooksData>>(AppConstants.AppId);
if (data == null || data.Value == null)
{
data = new GlobalConfigurationWidgetData<List<WebhooksData>>();
data.Value = new List<WebhooksData>();
}
return data;
}
}
}