-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShippingRepository.cs
More file actions
79 lines (66 loc) · 3.16 KB
/
Copy pathShippingRepository.cs
File metadata and controls
79 lines (66 loc) · 3.16 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
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using CMS.Commerce;
using CMS.DataEngine;
using CMS.Helpers;
using CMS.Websites.Routing;
namespace DancingGoat.Commerce;
/// <summary>
/// Repository for managing shipping method information retrieval operations.
/// </summary>
public class ShippingRepository
{
private readonly IWebsiteChannelContext websiteChannelContext;
private readonly IProgressiveCache cache;
private readonly ICacheDependencyBuilderFactory cacheDependencyBuilderFactory;
private readonly IInfoProvider<ShippingMethodInfo> shippingMethodInfoProvider;
/// <summary>
/// Initializes a new instance of the <see cref="ShippingRepository"/> class.
/// </summary>
/// <param name="websiteChannelContext">The website channel context.</param>
/// <param name="cache">The cache.</param>
/// <param name="cacheDependencyBuilderFactory">The cache dependency builder factory.</param>
/// <param name="shippingMethodInfoProvider">The shipping method info provider.</param>
public ShippingRepository(IWebsiteChannelContext websiteChannelContext, IProgressiveCache cache, ICacheDependencyBuilderFactory cacheDependencyBuilderFactory,
IInfoProvider<ShippingMethodInfo> shippingMethodInfoProvider)
{
this.websiteChannelContext = websiteChannelContext;
this.cache = cache;
this.cacheDependencyBuilderFactory = cacheDependencyBuilderFactory;
this.shippingMethodInfoProvider = shippingMethodInfoProvider;
}
/// <summary>
/// Returns a cached list of all <see cref="ShippingMethodInfo"/>.
/// </summary>
public async Task<IEnumerable<ShippingMethodInfo>> GetShipping(CancellationToken cancellationToken)
{
if (websiteChannelContext.IsPreview)
{
return await GetShippingInternal(cancellationToken);
}
var cacheSettings = new CacheSettings(5, websiteChannelContext.WebsiteChannelName, nameof(ShippingRepository), nameof(GetShipping));
return await cache.LoadAsync(async (cacheSettings) =>
{
var result = await GetShippingInternal(cancellationToken);
if (cacheSettings.Cached = result != null && result.Any())
{
var cacheDependencyBuilder = cacheDependencyBuilderFactory.Create();
var cacheDependencies = cacheDependencyBuilder
.ForInfoObjects<ShippingMethodInfo>()
.All()
.Builder()
.Build();
cacheSettings.CacheDependency = cacheDependencies;
}
return result;
}, cacheSettings);
}
private async Task<IEnumerable<ShippingMethodInfo>> GetShippingInternal(CancellationToken cancellationToken)
{
return await shippingMethodInfoProvider.Get()
.WhereTrue(nameof(ShippingMethodInfo.ShippingMethodEnabled))
.GetEnumerableTypedResultAsync(cancellationToken: cancellationToken);
}
}