Skip to content

Commit 1a433b5

Browse files
authored
Merge pull request #27 from umbraco/feature/zapier-trigger-data
Feature/zapier trigger data
2 parents a078d52 + 559b654 commit 1a433b5

File tree

2 files changed

+38
-33
lines changed

2 files changed

+38
-33
lines changed

src/Umbraco.Cms.Integrations.Automation.Zapier/Controllers/PollingController.cs

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.Linq;
43

54
using Umbraco.Cms.Integrations.Automation.Zapier.Extensions;
6-
using Umbraco.Cms.Integrations.Automation.Zapier.Models.Dtos;
75
using Umbraco.Cms.Integrations.Automation.Zapier.Services;
86

97
#if NETCOREAPP
108
using Microsoft.Extensions.Options;
11-
9+
using Umbraco.Cms.Core.Models.PublishedContent;
1210
using Umbraco.Cms.Core.Services;
11+
using Umbraco.Cms.Web.Common;
1312
using Umbraco.Cms.Integrations.Automation.Zapier.Configuration;
1413
#else
1514
using Umbraco.Core.Services;
@@ -24,38 +23,25 @@ namespace Umbraco.Cms.Integrations.Automation.Zapier.Controllers
2423
/// </summary>
2524
public class PollingController : ZapierAuthorizedApiController
2625
{
27-
private IContentService _contentService;
28-
29-
private IContentTypeService _contentTypeService;
26+
private readonly IContentTypeService _contentTypeService;
3027

3128
#if NETCOREAPP
32-
public PollingController(IOptions<ZapierSettings> options, IContentService contentService, IContentTypeService contentTypeService, IUserValidationService userValidationService)
29+
private readonly UmbracoHelper _umbracoHelper;
30+
31+
public PollingController(IOptions<ZapierSettings> options, IContentService contentService, IContentTypeService contentTypeService, UmbracoHelper umbracoHelper,
32+
IUserValidationService userValidationService)
3333
: base(options, userValidationService)
3434
#else
35-
public PollingController(IContentService contentService, IContentTypeService contentTypeService, IUserValidationService userValidationService)
35+
public PollingController(IContentTypeService contentTypeService, IUserValidationService userValidationService)
3636
: base(userValidationService)
3737
#endif
3838
{
39-
_contentService = contentService;
40-
4139
_contentTypeService = contentTypeService;
42-
}
43-
44-
[Obsolete("Used only for Umbraco Zapier app v1.0.0. For updated versions use GetContentByType")]
45-
public IEnumerable<PublishedContentDto> GetSampleContent()
46-
{
47-
if (!IsAccessValid()) return null;
4840

49-
var rootNodes = _contentService.GetRootContent()
50-
.Where(p => p.Published)
51-
.OrderByDescending(p => p.PublishDate);
52-
53-
return rootNodes.Select(p => new PublishedContentDto
54-
{
55-
Id = p.Id.ToString(),
56-
Name = p.Name,
57-
PublishDate = p.PublishDate.Value.ToString()
58-
});
41+
#if NETCOREAPP
42+
_umbracoHelper = umbracoHelper;
43+
#else
44+
#endif
5945
}
6046

6147
public List<Dictionary<string, string>> GetContentByType(string alias)
@@ -65,7 +51,15 @@ public List<Dictionary<string, string>> GetContentByType(string alias)
6551
var contentType = _contentTypeService.Get(alias);
6652
if (contentType == null) return new List<Dictionary<string, string>>();
6753

68-
return new List<Dictionary<string, string>> { contentType.ToContentTypeDictionary() };
54+
#if NETCOREAPP
55+
var contentItems = _umbracoHelper.ContentAtXPath("//" + alias)
56+
.OrderByDescending(p => p.UpdateDate);
57+
#else
58+
var contentItems = Umbraco.ContentAtXPath("//" + alias)
59+
.OrderByDescending(p => p.UpdateDate);
60+
#endif
61+
62+
return new List<Dictionary<string, string>> { contentType.ToContentTypeDictionary(contentItems.FirstOrDefault()) };
6963
}
7064

7165

src/Umbraco.Cms.Integrations.Automation.Zapier/Extensions/ContentExtensions.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34

45
#if NETCOREAPP
56
using Umbraco.Cms.Core.Models;
7+
using Umbraco.Cms.Core.Models.PublishedContent;
68
#else
79
using Umbraco.Core.Models;
10+
using Umbraco.Core.Models.PublishedContent;
811
#endif
912

1013
namespace Umbraco.Cms.Integrations.Automation.Zapier.Extensions
1114
{
1215
public static class ContentExtensions
1316
{
14-
public static Dictionary<string, string> ToContentTypeDictionary(this IContentType contentType)
17+
public static Dictionary<string, string> ToContentTypeDictionary(this IContentType contentType, IPublishedContent content)
1518
{
1619
var contentDict = new Dictionary<string, string>
1720
{
18-
{Constants.ContentProperties.Id, "1" },
19-
{Constants.ContentProperties.Name, contentType.Name },
20-
{Constants.ContentProperties.PublishDate, DateTime.UtcNow.ToString("s") }
21+
{Constants.ContentProperties.Id, content != null ? content.Id.ToString() : "1" },
22+
{Constants.ContentProperties.Name, content != null ? content.Name : contentType.Name },
23+
{Constants.ContentProperties.PublishDate, content != null ? content.UpdateDate.ToString("s") : DateTime.UtcNow.ToString("s") }
2124
};
2225

2326
foreach (var propertyType in contentType.PropertyTypes)
2427
{
25-
contentDict.Add(propertyType.Alias, string.Empty);
28+
if (content == null)
29+
{
30+
contentDict.Add(propertyType.Alias, string.Empty);
31+
continue;
32+
}
33+
34+
var contentProperty = content.Properties.First(p => p.Alias == propertyType.Alias);
35+
36+
contentDict.Add(propertyType.Alias, contentProperty.GetValue().ToString());
2637
}
2738

2839
return contentDict;

0 commit comments

Comments
 (0)