Skip to content

Commit 62eeea4

Browse files
author
Ben White
committed
Provide a mechanism for IProductFeedGeneratrorService implementations to declare the format they generate. Use this format declaration to return the correct output within ProductFeedController.
1 parent 6e724c5 commit 62eeea4

File tree

5 files changed

+42
-7
lines changed

5 files changed

+42
-7
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Umbraco.Commerce.ProductFeeds.Core.Features.FeedGenerators.Application
8+
{
9+
public enum FeedFormat
10+
{
11+
Unknown,
12+
Xml,
13+
Json
14+
}
15+
}

src/Umbraco.Commerce.ProductFeeds.Core/Features/FeedGenerators/Application/IProductFeedGeneratorService.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Text.Json;
12
using System.Xml;
23
using Umbraco.Commerce.ProductFeeds.Core.Features.FeedSettings.Application;
34

@@ -14,6 +15,12 @@ public interface IProductFeedGeneratorService
1415
/// Returns a user friendly name of the value extractor.
1516
/// </summary>
1617
public string DisplayName { get; }
17-
Task<XmlDocument> GenerateFeedAsync(ProductFeedSettingReadModel feedSetting);
18+
19+
/// <summary>
20+
/// Returns the feed format that this generator can generate.
21+
/// </summary>
22+
public FeedFormat Format { get; }
23+
Task<XmlDocument> GenerateXmlFeedAsync(ProductFeedSettingReadModel feedSetting) => throw new NotImplementedException();
24+
Task<JsonDocument> GenerateJsonFeedAsync(ProductFeedSettingReadModel feedSetting) => throw new NotImplementedException();
1825
}
1926
}

src/Umbraco.Commerce.ProductFeeds.Core/Features/FeedGenerators/Implementations/GoogleMerchantCenterFeedService.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public class GoogleMerchantCenterFeedService : IProductFeedGeneratorService
4040

4141
public string DisplayName => "Google Merchant Center Feed";
4242

43+
public FeedFormat Format => FeedFormat.Xml;
44+
4345
public GoogleMerchantCenterFeedService(
4446
ILogger<GoogleMerchantCenterFeedService> logger,
4547
ICurrencyService currencyService,
@@ -62,7 +64,7 @@ public GoogleMerchantCenterFeedService(
6264
/// <param name="feedSetting"></param>
6365
/// <returns></returns>
6466
/// <exception cref="IdPropertyNodeMappingNotFoundException"></exception>
65-
public async Task<XmlDocument> GenerateFeedAsync(ProductFeedSettingReadModel feedSetting)
67+
public async Task<XmlDocument> GenerateXmlFeedAsync(ProductFeedSettingReadModel feedSetting)
6668
{
6769
ArgumentNullException.ThrowIfNull(feedSetting, nameof(feedSetting));
6870

src/Umbraco.Commerce.ProductFeeds.Startup/Initializations/CustomRouting.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void CreateRoutes(IEndpointRouteBuilder endpoints)
2727
new
2828
{
2929
controller = "ProductFeed",
30-
Action = "Xml",
30+
Action = "Generate",
3131
});
3232
}
3333
}

src/Umbraco.Commerce.ProductFeeds.Web/Apis/Publics/ProductFeedController.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Text.Json;
12
using System.Threading.Tasks;
23
using System.Xml;
34
using Microsoft.AspNetCore.Mvc;
@@ -19,7 +20,7 @@ public ProductFeedController(
1920
_feedConfigService = feedConfigService;
2021
}
2122

22-
public async Task<IActionResult> Xml(string path)
23+
public async Task<IActionResult> Generate(string path)
2324
{
2425
ProductFeedSettingReadModel? feedSettings = await _feedConfigService
2526
.FindSettingAsync(new FindSettingParams { FeedRelativePath = path })
@@ -30,10 +31,20 @@ public async Task<IActionResult> Xml(string path)
3031
}
3132

3233
IProductFeedGeneratorService feedGenerator = _feedGeneratorFactory.GetGenerator(feedSettings.FeedGeneratorId);
33-
XmlDocument feed = await feedGenerator.GenerateFeedAsync(feedSettings);
3434

35-
var result = new XmlActionResult(feed) { Formatting = Formatting.Indented };
36-
return result;
35+
switch (feedGenerator.Format)
36+
{
37+
case FeedFormat.Xml:
38+
XmlDocument xmlFeed = await feedGenerator.GenerateXmlFeedAsync(feedSettings);
39+
var result = new XmlActionResult(xmlFeed) { Formatting = Formatting.Indented };
40+
return result;
41+
case FeedFormat.Json:
42+
JsonDocument jsonFeed = await feedGenerator.GenerateJsonFeedAsync(feedSettings);
43+
var jsonResult = new JsonResult(jsonFeed.RootElement);
44+
return jsonResult;
45+
default:
46+
return Problem("Unknown feed format.");
47+
}
3748
}
3849
}
3950
}

0 commit comments

Comments
 (0)