Skip to content

Commit 2b88db5

Browse files
author
Ben White
committed
Define a base class for ProductFeedGeneratorService implementations as a developer aid.
1 parent 62eeea4 commit 2b88db5

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Text.Json;
6+
using System.Threading.Tasks;
7+
using System.Xml;
8+
using Umbraco.Commerce.ProductFeeds.Core.Features.FeedGenerators.Application;
9+
using Umbraco.Commerce.ProductFeeds.Core.Features.FeedSettings.Application;
10+
using Umbraco.Commerce.ProductFeeds.Core.Features.PropertyValueExtractors.Application;
11+
12+
namespace Umbraco.Commerce.ProductFeeds.Core.Features.FeedGenerators.Implementations
13+
{
14+
public abstract class FeedGeneratorServiceBase : IProductFeedGeneratorService
15+
{
16+
17+
protected FeedGeneratorServiceBase(
18+
ISingleValuePropertyExtractorFactory singleValuePropertyExtractorFactory,
19+
IMultipleValuePropertyExtractorFactory multipleValuePropertyExtractorFactory)
20+
{
21+
SingleValuePropertyExtractorFactory = singleValuePropertyExtractorFactory;
22+
MultipleValuePropertyExtractorFactory = multipleValuePropertyExtractorFactory;
23+
}
24+
25+
public abstract string Id { get; }
26+
public abstract string DisplayName { get; }
27+
public abstract FeedFormat Format { get; }
28+
protected ISingleValuePropertyExtractorFactory SingleValuePropertyExtractorFactory { get; }
29+
protected IMultipleValuePropertyExtractorFactory MultipleValuePropertyExtractorFactory { get; }
30+
31+
public virtual Task<XmlDocument> GenerateXmlFeedAsync(ProductFeedSettingReadModel feedSetting)
32+
{
33+
throw new NotImplementedException("XML feed generation is not implemented.");
34+
}
35+
public virtual Task<JsonDocument> GenerateJsonFeedAsync(ProductFeedSettingReadModel feedSetting)
36+
{
37+
throw new NotImplementedException("JSON feed generation is not implemented.");
38+
}
39+
}
40+
}

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace Umbraco.Commerce.ProductFeeds.Core.Features.FeedGenerators.Implementat
2020
/// <summary>
2121
/// This is the feed generator that follows Google Merchant Center's standard.
2222
/// </summary>
23-
public class GoogleMerchantCenterFeedService : IProductFeedGeneratorService
23+
public class GoogleMerchantCenterFeedService : FeedGeneratorServiceBase
2424
{
2525
private const string GoogleXmlNamespaceUri = "http://base.google.com/ns/1.0";
2626

@@ -33,29 +33,26 @@ public class GoogleMerchantCenterFeedService : IProductFeedGeneratorService
3333
private readonly ICurrencyService _currencyService;
3434
private readonly IProductQueryService _productQueryService;
3535
private readonly IUmbracoCommerceApi _commerceApi;
36-
private readonly ISingleValuePropertyExtractorFactory _singleValuePropertyExtractorFactory;
37-
private readonly IMultipleValuePropertyExtractorFactory _multipleValuePropertyExtractorFactory;
3836

39-
public string Id => "GoogleMerchantCenter";
37+
public override string Id => "GoogleMerchantCenter";
4038

41-
public string DisplayName => "Google Merchant Center Feed";
39+
public override string DisplayName => "Google Merchant Center Feed";
4240

43-
public FeedFormat Format => FeedFormat.Xml;
41+
public override FeedFormat Format => FeedFormat.Xml;
4442

4543
public GoogleMerchantCenterFeedService(
4644
ILogger<GoogleMerchantCenterFeedService> logger,
4745
ICurrencyService currencyService,
4846
IProductQueryService productQueryService,
4947
IUmbracoCommerceApi commerceApi,
50-
ISingleValuePropertyExtractorFactory singleValuePropertyExtractor,
48+
ISingleValuePropertyExtractorFactory singleValuePropertyExtractorFactory,
5149
IMultipleValuePropertyExtractorFactory multipleValuePropertyExtractorFactory)
50+
: base(singleValuePropertyExtractorFactory, multipleValuePropertyExtractorFactory)
5251
{
5352
_logger = logger;
5453
_currencyService = currencyService;
5554
_productQueryService = productQueryService;
5655
_commerceApi = commerceApi;
57-
_singleValuePropertyExtractorFactory = singleValuePropertyExtractor;
58-
_multipleValuePropertyExtractorFactory = multipleValuePropertyExtractorFactory;
5956
}
6057

6158
/// <summary>
@@ -64,7 +61,7 @@ public GoogleMerchantCenterFeedService(
6461
/// <param name="feedSetting"></param>
6562
/// <returns></returns>
6663
/// <exception cref="IdPropertyNodeMappingNotFoundException"></exception>
67-
public async Task<XmlDocument> GenerateXmlFeedAsync(ProductFeedSettingReadModel feedSetting)
64+
public override async Task<XmlDocument> GenerateXmlFeedAsync(ProductFeedSettingReadModel feedSetting)
6865
{
6966
ArgumentNullException.ThrowIfNull(feedSetting, nameof(feedSetting));
7067

@@ -163,13 +160,13 @@ private async Task<XmlElement> NewItemNodeAsync(ProductFeedSettingReadModel feed
163160
// add custom properties
164161
foreach (PropertyAndNodeMapItem map in feedSetting.PropertyNameMappings)
165162
{
166-
if (_singleValuePropertyExtractorFactory.TryGetExtractor(map.ValueExtractorId, out ISingleValuePropertyExtractor? singleValueExtractor)
163+
if (SingleValuePropertyExtractorFactory.TryGetExtractor(map.ValueExtractorId, out ISingleValuePropertyExtractor? singleValueExtractor)
167164
&& singleValueExtractor != null)
168165
{
169166
string propValue = singleValueExtractor.Extract(variant, map.PropertyAlias, mainProduct);
170167
itemNode.AddChild(map.NodeName, propValue, GoogleXmlNamespaceUri);
171168
}
172-
else if (_multipleValuePropertyExtractorFactory.TryGetExtractor(map.ValueExtractorId!, out IMultipleValuePropertyExtractor? multipleValueExtractor)
169+
else if (MultipleValuePropertyExtractorFactory.TryGetExtractor(map.ValueExtractorId!, out IMultipleValuePropertyExtractor? multipleValueExtractor)
173170
&& multipleValueExtractor != null)
174171
{
175172
var values = multipleValueExtractor.Extract(variant, map.PropertyAlias, mainProduct).ToList();

0 commit comments

Comments
 (0)