-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductRepository.cs
More file actions
90 lines (75 loc) · 3.29 KB
/
Copy pathProductRepository.cs
File metadata and controls
90 lines (75 loc) · 3.29 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
80
81
82
83
84
85
86
87
88
89
90
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using CMS.ContentEngine;
using CMS.Websites;
using DancingGoat.Models;
using Kentico.Content.Web.Mvc;
namespace DancingGoat.Commerce;
/// <summary>
/// Repository for managing product-related data retrieval operations.
/// </summary>
public class ProductRepository
{
private readonly IContentRetriever contentRetriever;
/// <summary>
/// Initializes a new instance of the <see cref="ProductRepository"/> class.
/// </summary>
/// <param name="contentRetriever">The content retriever.</param>
public ProductRepository(IContentRetriever contentRetriever)
{
this.contentRetriever = contentRetriever;
}
/// <summary>
/// Retrieves products by their content item IDs.
/// </summary>
/// <param name="productIds">The collection of product content item IDs to retrieve.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public async Task<IEnumerable<IProductFields>> GetProductsByIds(IEnumerable<int> productIds, CancellationToken cancellationToken = default)
{
var products = await contentRetriever.RetrieveContentOfReusableSchemas<IProductFields>(
[IProductFields.REUSABLE_FIELD_SCHEMA_NAME],
new RetrieveContentOfReusableSchemasParameters
{
LinkedItemsMaxLevel = 1,
WorkspaceNames = [DancingGoatConstants.COMMERCE_WORKSPACE_NAME]
},
query => query.Where(where => where.WhereIn(nameof(IContentQueryDataContainer.ContentItemID), productIds)),
new RetrievalCacheSettings($"WhereIn_{nameof(IContentQueryDataContainer.ContentItemID)}_{string.Join("_", productIds)}"),
cancellationToken
);
return products;
}
/// <summary>
/// Retrieves the URLs of product pages associated with the specified product IDs.
/// </summary>
/// <param name="productIds">The collection of product content item IDs for which to retrieve page URLs.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public async Task<Dictionary<int, string>> GetProductPageUrls(IEnumerable<int> productIds, CancellationToken cancellationToken = default)
{
if (!productIds.Any())
{
return [];
}
var productPages = await contentRetriever.RetrievePages<ProductPage>(
new RetrievePagesParameters
{
LinkedItemsMaxLevel = 1,
PathMatch = PathMatch.Children(DancingGoatConstants.PRODUCTS_PAGE_TREE_PATH),
IncludeSecuredItems = true,
},
query => query.Linking(nameof(ProductPage.ProductPageProduct), productIds),
new RetrievalCacheSettings($"Linking_{nameof(ProductPage.ProductPageProduct)}_{string.Join("_", productIds)}"),
cancellationToken
);
var productPageUrls = productPages
.Select(p => (Page: p, Product: p.ProductPageProduct?.FirstOrDefault() as IContentItemFieldsSource))
.Where(x => x.Product is not null)
.ToDictionary(
x => x.Product.SystemFields.ContentItemID,
x => x.Page.GetUrl().RelativePath
);
return productPageUrls;
}
}