-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Adding Product List base architecture * Adding ordering options * Adding title filter * Resolving applied filters from query string * Adding UI tests * Renaming interface and fixing analyzer violations * Fixing analyzer violation * Ignoring pager HTML validation error * Removing unnecessary ContainedPart from products * Minor refactorings * Accepting only one order by value * Fixing failing UI test * Update src/Modules/OrchardCore.Commerce/Services/ProductListService.cs --------- Co-authored-by: Sára El-Saig <[email protected]>
- Loading branch information
1 parent
9628996
commit 5a30434
Showing
25 changed files
with
625 additions
and
51 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
src/Modules/OrchardCore.Commerce/Drivers/ProductListPartDisplayDriver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using OrchardCore.Commerce.Models; | ||
using OrchardCore.Commerce.Services; | ||
using OrchardCore.Commerce.ViewModels; | ||
using OrchardCore.ContentManagement.Display.ContentDisplay; | ||
using OrchardCore.ContentManagement.Display.Models; | ||
using OrchardCore.DisplayManagement.Views; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using static Lombiq.HelpfulLibraries.OrchardCore.Contents.CommonContentDisplayTypes; | ||
|
||
namespace OrchardCore.Commerce.Drivers; | ||
|
||
public class ProductListPartDisplayDriver : ContentPartDisplayDriver<ProductListPart> | ||
{ | ||
private readonly IProductListService _productListService; | ||
private readonly IEnumerable<IAppliedProductListFilterParametersProvider> _productFilterProviders; | ||
|
||
public ProductListPartDisplayDriver( | ||
IProductListService productListService, | ||
IEnumerable<IAppliedProductListFilterParametersProvider> productFilterProviders) | ||
{ | ||
_productListService = productListService; | ||
_productFilterProviders = productFilterProviders; | ||
} | ||
|
||
public override IDisplayResult Display(ProductListPart part, BuildPartDisplayContext context) => | ||
Combine( | ||
Initialize<ProductListPartViewModel>( | ||
GetDisplayShapeType(context), | ||
async viewModel => await BuildViewModelAsync(viewModel, part, context)) | ||
.Location(Detail, "Content:25") | ||
.Location(Summary, "Meta:10"), | ||
Initialize<ProductListFiltersViewModel>( | ||
GetDisplayShapeType(context) + "_Filters", | ||
async viewModel => await BuildFiltersViewModelAsync(viewModel, part)) | ||
.Location(Detail, "Content:20")); | ||
|
||
private async Task BuildViewModelAsync(ProductListPartViewModel viewModel, ProductListPart part, BuildPartDisplayContext context) | ||
{ | ||
viewModel.ProductListPart = part; | ||
|
||
var filterParameters = await _productFilterProviders | ||
.MaxBy(provider => provider.Priority).GetFilterParametersAsync(part) ?? new ProductListFilterParameters(); | ||
|
||
var productList = await _productListService.GetProductsAsync(part, filterParameters); | ||
viewModel.Products = productList.Products; | ||
viewModel.Pager = (await context.New.Pager(filterParameters.Pager)).TotalItemCount(productList.TotalItemCount); | ||
viewModel.Context = context; | ||
} | ||
|
||
private async Task BuildFiltersViewModelAsync(ProductListFiltersViewModel viewModel, ProductListPart part) | ||
{ | ||
viewModel.ProductListPart = part; | ||
|
||
viewModel.FilterIds = await _productListService.GetFilterIdsAsync(part); | ||
viewModel.OrderByOptions = await _productListService.GetOrderByOptionsAsync(part); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Modules/OrchardCore.Commerce/Migrations/ProductListMigrations.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using OrchardCore.Commerce.Models; | ||
using OrchardCore.ContentManagement.Metadata; | ||
using OrchardCore.ContentManagement.Metadata.Settings; | ||
using OrchardCore.Data.Migration; | ||
|
||
namespace OrchardCore.Commerce.Migrations; | ||
|
||
public class ProductListMigrations : DataMigration | ||
{ | ||
private readonly IContentDefinitionManager _contentDefinitionManager; | ||
|
||
public ProductListMigrations(IContentDefinitionManager contentDefinitionManager) => | ||
_contentDefinitionManager = contentDefinitionManager; | ||
|
||
public int Create() | ||
{ | ||
_contentDefinitionManager | ||
.AlterPartDefinition(nameof(ProductListPart), builder => builder | ||
.Attachable() | ||
.WithDescription("Displays a list of products with optional filtering and sorting controlled by widgets.")); | ||
|
||
return 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using OrchardCore.ContentManagement; | ||
using System.Collections.Generic; | ||
|
||
namespace OrchardCore.Commerce.Models; | ||
|
||
public class ProductList | ||
{ | ||
public IEnumerable<ContentItem> Products { get; set; } | ||
public int TotalItemCount { get; set; } | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Modules/OrchardCore.Commerce/Models/ProductListFilterContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using OrchardCore.ContentManagement; | ||
using YesSql; | ||
|
||
namespace OrchardCore.Commerce.Models; | ||
|
||
public class ProductListFilterContext | ||
{ | ||
public ProductListPart ProductList { get; set; } | ||
public ProductListFilterParameters FilterParameters { get; set; } | ||
public IQuery<ContentItem> Query { get; set; } | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Modules/OrchardCore.Commerce/Models/ProductListFilterParameters.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using OrchardCore.Navigation; | ||
using System.Collections.Generic; | ||
|
||
namespace OrchardCore.Commerce.Models; | ||
|
||
public class ProductListFilterParameters | ||
{ | ||
public Pager Pager { get; set; } | ||
public string OrderBy { get; set; } | ||
public IDictionary<string, string> FilterValues { get; } = new Dictionary<string, string>(); | ||
} |
9 changes: 9 additions & 0 deletions
9
src/Modules/OrchardCore.Commerce/Models/ProductListFilters.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace OrchardCore.Commerce.Models; | ||
|
||
public class ProductListFilters | ||
{ | ||
public IList<string> OrderBy { get; } = new List<string>(); | ||
public IDictionary<string, string> FilterValues { get; } = new Dictionary<string, string>(); | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Modules/OrchardCore.Commerce/Models/ProductListPart.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using OrchardCore.ContentManagement; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace OrchardCore.Commerce.Models; | ||
|
||
[SuppressMessage( | ||
"Minor Code Smell", | ||
"S2094:Classes should not be empty", | ||
Justification = "This part doesn't store data.")] | ||
public class ProductListPart : ContentPart | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/Modules/OrchardCore.Commerce/Services/IAppliedProductListFilterParametersProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using OrchardCore.Commerce.Models; | ||
using System.Threading.Tasks; | ||
|
||
namespace OrchardCore.Commerce.Services; | ||
|
||
/// <summary> | ||
/// Provides a way to get the applied filter parameters for a product list. | ||
/// </summary> | ||
public interface IAppliedProductListFilterParametersProvider | ||
{ | ||
/// <summary> | ||
/// Gets the priority of this provider. The provider with the highest priority will be used. | ||
/// </summary> | ||
int Priority { get; } | ||
|
||
/// <summary> | ||
/// Returns the applied filter parameters for the given product list. | ||
/// </summary> | ||
Task<ProductListFilterParameters> GetFilterParametersAsync(ProductListPart productList); | ||
} |
39 changes: 39 additions & 0 deletions
39
src/Modules/OrchardCore.Commerce/Services/IProductListFilterProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using OrchardCore.Commerce.Models; | ||
using OrchardCore.ContentManagement; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using YesSql; | ||
|
||
namespace OrchardCore.Commerce.Services; | ||
|
||
/// <summary> | ||
/// Provides a way to filter a product list. | ||
/// </summary> | ||
public interface IProductListFilterProvider | ||
{ | ||
/// <summary> | ||
/// Gets the order in which the filter providers are applied. | ||
/// </summary> | ||
int Order { get; } | ||
|
||
/// <summary> | ||
/// Whether this provider can handle the given product list. If not, the next provider will be tried and this | ||
/// provider will be skipped. | ||
/// </summary> | ||
Task<bool> IsApplicableAsync(ProductListPart productList); | ||
|
||
/// <summary> | ||
/// The IDs of the order by options that this provider can handle. | ||
/// </summary> | ||
Task<IEnumerable<string>> GetOrderByOptionIdsAsync(ProductListPart productList); | ||
|
||
/// <summary> | ||
/// The IDs of the filters that this provider can handle. | ||
/// </summary> | ||
Task<IEnumerable<string>> GetFilterIdsAsync(ProductListPart productListPart); | ||
|
||
/// <summary> | ||
/// Builds the query for the given product list and filter parameters. | ||
/// </summary> | ||
Task<IQuery<ContentItem>> BuildQueryAsync(ProductListFilterContext context); | ||
} |
26 changes: 26 additions & 0 deletions
26
src/Modules/OrchardCore.Commerce/Services/IProductListService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using OrchardCore.Commerce.Models; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
|
||
namespace OrchardCore.Commerce.Services; | ||
|
||
/// <summary> | ||
/// Provides a way to get the products for a product list. | ||
/// </summary> | ||
public interface IProductListService | ||
{ | ||
/// <summary> | ||
/// Gets the products for the given product list and filter parameters. | ||
/// </summary> | ||
Task<ProductList> GetProductsAsync(ProductListPart productList, ProductListFilterParameters filterParameters); | ||
|
||
/// <summary> | ||
/// Gets the order by options for the given product list. | ||
/// </summary> | ||
Task<IEnumerable<string>> GetOrderByOptionsAsync(ProductListPart productList); | ||
|
||
/// <summary> | ||
/// Gets the filter IDs for the given product list. | ||
/// </summary> | ||
Task<IEnumerable<string>> GetFilterIdsAsync(ProductListPart productList); | ||
} |
Oops, something went wrong.