Skip to content

Commit

Permalink
VCST-2223: update to Configurable Products (#23)
Browse files Browse the repository at this point in the history
* feat: update to Configurable Products (#23)

---------

Co-authored-by: Евгений Колосов <[email protected]>
  • Loading branch information
ksavosteev and Ljutyj authored Dec 3, 2024
1 parent dc05f46 commit 11aba02
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageReference Include="FluentValidation" Version="11.10.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
<PackageReference Include="VirtoCommerce.Xapi.Core" Version="3.809.0" />
<PackageReference Include="VirtoCommerce.XCatalog.Core" Version="3.817.0" />
<PackageReference Include="VirtoCommerce.XCatalog.Core" Version="3.818.0" />
<PackageReference Include="VirtoCommerce.CartModule.Core" Version="3.820.0" />
<PackageReference Include="VirtoCommerce.PaymentModule.Core" Version="3.804.0" />
<PackageReference Include="VirtoCommerce.ShippingModule.Core" Version="3.802.0" />
Expand Down
17 changes: 15 additions & 2 deletions src/VirtoCommerce.XCart.Data/Commands/AddCartItemCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using VirtoCommerce.CatalogModule.Core.Model.Search;
using VirtoCommerce.CatalogModule.Core.Search;
using VirtoCommerce.Platform.Core.Common;
using VirtoCommerce.XCart.Core;
using VirtoCommerce.XCart.Core.Commands;
using VirtoCommerce.XCart.Core.Commands.BaseCommands;
Expand All @@ -14,15 +17,19 @@ public class AddCartItemCommandHandler : CartCommandHandler<AddCartItemCommand>
{
private readonly ICartProductService _cartProductService;
private readonly IMediator _mediator;
private readonly IProductConfigurationSearchService _productConfigurationSearchService;

public AddCartItemCommandHandler(
ICartAggregateRepository cartAggregateRepository,
ICartProductService cartProductService,
IMediator mediator)
IMediator mediator,
IProductConfigurationSearchService productConfigurationSearchService
)
: base(cartAggregateRepository)
{
_cartProductService = cartProductService;
_mediator = mediator;
_productConfigurationSearchService = productConfigurationSearchService;
}

public override async Task<CartAggregate> Handle(AddCartItemCommand request, CancellationToken cancellationToken)
Expand All @@ -38,7 +45,13 @@ public override async Task<CartAggregate> Handle(AddCartItemCommand request, Can
CartProduct = product,
};

if (product?.Product?.IsConfigurable == true)
var configurations = await _productConfigurationSearchService.SearchNoCloneAsync(new ProductConfigurationSearchCriteria
{
ProductId = request.ProductId
});
var configuration = configurations.Results.FirstOrDefault();

if (configuration?.IsActive == true)
{
var createConfigurableProductCommand = new CreateConfiguredLineItemCommand
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using VirtoCommerce.CatalogModule.Core.Services;
using VirtoCommerce.CatalogModule.Core.Model.Configuration;
using VirtoCommerce.CatalogModule.Core.Model.Search;
using VirtoCommerce.CatalogModule.Core.Search;
using VirtoCommerce.Platform.Core.Common;
using VirtoCommerce.Xapi.Core.Infrastructure;
using VirtoCommerce.XCart.Core;
using VirtoCommerce.XCart.Core.Models;
Expand All @@ -12,23 +15,30 @@ namespace VirtoCommerce.XCatalog.Data.Queries;

public class GetProductConfigurationQueryHandler : IQueryHandler<GetProductConfigurationQuery, ProductConfigurationQueryResponse>
{
private readonly IConfigurableProductService _configurableProductService;
private readonly IProductConfigurationSearchService _productConfigurationSearchService;
private readonly IConfiguredLineItemContainerService _configuredLineItemContainerService;
private readonly ICartProductsLoaderService _cartProductService;

public GetProductConfigurationQueryHandler(
IConfigurableProductService configurableProductService,
IProductConfigurationSearchService productConfigurationSearchService,
IConfiguredLineItemContainerService configuredLineItemContainerService,
ICartProductsLoaderService cartProductService)
{
_configurableProductService = configurableProductService;
_productConfigurationSearchService = productConfigurationSearchService;
_configuredLineItemContainerService = configuredLineItemContainerService;
_cartProductService = cartProductService;
}

public async Task<ProductConfigurationQueryResponse> Handle(GetProductConfigurationQuery request, CancellationToken cancellationToken)
{
var configuration = await _configurableProductService.GetProductConfigurationAsync(request.ConfigurableProductId);
var result = new ProductConfigurationQueryResponse();

var configuration = await GetConfiguration(request);

if (configuration == null)
{
return result;
}

var container = await _configuredLineItemContainerService.CreateContainerAsync(request);

Expand All @@ -40,7 +50,6 @@ public async Task<ProductConfigurationQueryResponse> Handle(GetProductConfigurat

var productByIds = cartProducts.ToDictionary(x => x.Product.Id, x => x);

var result = new ProductConfigurationQueryResponse();
foreach (var section in configuration.Sections)
{
var configurationSection = new ExpProductConfigurationSection
Expand All @@ -49,7 +58,6 @@ public async Task<ProductConfigurationQueryResponse> Handle(GetProductConfigurat
Name = section.Name,
IsRequired = section.IsRequired,
Description = section.Description,
Type = section.Type,
};
result.ConfigurationSections.Add(configurationSection);

Expand All @@ -76,4 +84,13 @@ public async Task<ProductConfigurationQueryResponse> Handle(GetProductConfigurat

return result;
}

private async Task<ProductConfiguration> GetConfiguration(GetProductConfigurationQuery request)
{
var criteria = AbstractTypeFactory<ProductConfigurationSearchCriteria>.TryCreateInstance();
criteria.ProductId = request.ConfigurableProductId;

var configurationsResult = await _productConfigurationSearchService.SearchNoCloneAsync(criteria);
return configurationsResult.Results.FirstOrDefault();
}
}
2 changes: 1 addition & 1 deletion src/VirtoCommerce.XCart.Web/module.manifest
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<dependency id="VirtoCommerce.Pricing" version="3.809.0" />
<dependency id="VirtoCommerce.Shipping" version="3.802.0" />
<dependency id="VirtoCommerce.Xapi" version="3.809.0" />
<dependency id="VirtoCommerce.XCatalog" version="3.817.0" />
<dependency id="VirtoCommerce.XCatalog" version="3.818.0" />
</dependencies>

<title>Cart Experience API</title>
Expand Down

0 comments on commit 11aba02

Please sign in to comment.