Skip to content
This repository has been archived by the owner on Jun 13, 2023. It is now read-only.

Commit

Permalink
VP-5616: Change Cart models. Change cart converters.
Browse files Browse the repository at this point in the history
  • Loading branch information
trueboroda committed Oct 23, 2020
1 parent af5095f commit 2768ea7
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 33 deletions.
70 changes: 70 additions & 0 deletions VirtoCommerce.Storefront.Model/Cart/Demo/ConfiguredGroup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using VirtoCommerce.Storefront.Model.Catalog;
using VirtoCommerce.Storefront.Model.Common;

namespace VirtoCommerce.Storefront.Model.Cart.Demo
{
public class ConfiguredGroup : Entity
{
public ConfiguredGroup(int quantity, Currency currency, Money extendedPrice,
Money extendedPriceWithTax, Money taxTotal)
{
Quantity = quantity;
Currency = currency;
ExtendedPrice = extendedPrice;
ExtendedPriceWithTax = extendedPriceWithTax;
TaxTotal = taxTotal;
}


public DateTime CreatedDate { get; set; }

public DateTime? ModifiedDate { get; set; }

public string CreatedBy { get; set; }

public string ModifiedBy { get; set; }

[JsonRequired]
public virtual IList<LineItem> Items { get; set; } = new List<LineItem>();

[JsonRequired]
public int Quantity { get; set; }

#region Pricing

[JsonRequired]
public Currency Currency { get; set; }

public Money ListPrice { get; set; }

public Money ListPriceWithTax { get; set; }

public Money SalePrice { get; set; }

public Money SalePriceWithTax { get; set; }

public Money PlacedPrice { get; set; }

public Money PlacedPriceWithTax { get; set; }

[JsonRequired]
public Money ExtendedPrice { get; set; }

[JsonRequired]
public Money ExtendedPriceWithTax { get; set; }

#endregion

#region Taxation

[JsonRequired]
public Money TaxTotal { get; set; }

#endregion

public ICollection<ProductPart> Parts { get; set; } = new List<ProductPart>();
}
}
17 changes: 0 additions & 17 deletions VirtoCommerce.Storefront.Model/Cart/Demo/ConfiguredItem.cs

This file was deleted.

2 changes: 1 addition & 1 deletion VirtoCommerce.Storefront.Model/Cart/Demo/LineItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ namespace VirtoCommerce.Storefront.Model.Cart
{
public partial class LineItem
{
public string ConfiguredProductId { get; set; }
public string ConfiguredGropupId { get; set; }
}
}
3 changes: 1 addition & 2 deletions VirtoCommerce.Storefront.Model/Cart/Demo/ShoppingCart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace VirtoCommerce.Storefront.Model.Cart
{
public partial class ShoppingCart
{

public ICollection<ConfiguredItem> ConfiguredItems { get; set; } = new List<ConfiguredItem>();
public ICollection<ConfiguredGroup> ConfiguredGroups { get; set; } = new List<ConfiguredGroup>();
}
}
11 changes: 9 additions & 2 deletions VirtoCommerce.Storefront/Domain/Cart/CartConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,11 @@ public static ShoppingCart ToShoppingCart(this cartDto.ShoppingCart cartDto, Cur
result.VolumetricWeight = (decimal)(cartDto.VolumetricWeight ?? 0);
result.Weight = (decimal)(cartDto.Weight ?? 0);

if (cartDto.ConfiguredGroups != null)
{
result.ConfiguredGroups = cartDto.ConfiguredGroups.Select(x => x.ToConfiguredGroup(result)).ToList();
}

return result;
}

Expand Down Expand Up @@ -536,6 +541,8 @@ public static cartDto.ShoppingCart ToShoppingCartDto(this ShoppingCart cart)
result.VolumetricWeight = (double)cart.VolumetricWeight;
result.Weight = (double)cart.Weight;

result.ConfiguredGroups = cart.ConfiguredGroups.Select(x => x.ToConfiguredGroup()).ToList();

return result;
}

Expand Down Expand Up @@ -682,7 +689,7 @@ public static LineItem ToLineItem(this cartDto.CartLineItem lineItemDto, Currenc
Width = (decimal?)lineItemDto.Width,
Length = (decimal?)lineItemDto.Length,
Height = (decimal?)lineItemDto.Height,
ConfiguredProductId = lineItemDto.ConfiguredProductId,
ConfiguredGropupId = lineItemDto.ConfiguredGroupId,
};


Expand Down Expand Up @@ -768,7 +775,7 @@ public static cartDto.CartLineItem ToLineItemDto(this LineItem lineItem)
DynamicProperties = lineItem.DynamicProperties.Select(ToCartDynamicPropertyDto).ToList(),
VolumetricWeight = (double)(lineItem.VolumetricWeight ?? 0),

ConfiguredProductId = lineItem.ConfiguredProductId,
ConfiguredGroupId = lineItem.ConfiguredGropupId,
};
retVal.Weight = (double?)lineItem.Weight;
retVal.Width = (double?)lineItem.Width;
Expand Down
81 changes: 81 additions & 0 deletions VirtoCommerce.Storefront/Domain/Cart/Demo/CartConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using System.Linq;
using VirtoCommerce.Storefront.Common;
using VirtoCommerce.Storefront.Model;
using VirtoCommerce.Storefront.Model.Cart;
using VirtoCommerce.Storefront.Model.Cart.Demo;
using VirtoCommerce.Storefront.Model.Catalog;
using VirtoCommerce.Storefront.Model.Common;
using VirtoCommerce.Storefront.Model.Marketing;
using VirtoCommerce.Storefront.Model.Security;
using VirtoCommerce.Storefront.Model.Stores;
using VirtoCommerce.Storefront.Model.Tax;
using cartDto = VirtoCommerce.Storefront.AutoRestClients.CartModuleApi.Models;


namespace VirtoCommerce.Storefront.Domain
{

public static partial class CartConverter
{
public static cartDto.DemoCartConfiguredGroup ToConfiguredGroup(this ConfiguredGroup group)
{
foreach (var lineItem in group.Items)
{
lineItem.Id = lineItem.Id ?? Guid.NewGuid().ToString("N");
}

return new cartDto.DemoCartConfiguredGroup
{
Id = group.Id ?? Guid.NewGuid().ToString("N"),
ItemIds = group.Items.Select(x => x.Id).ToList(),
CreatedBy = group.CreatedBy,
CreatedDate = group.CreatedDate,
ModifiedBy = group.ModifiedBy,
ModifiedDate = group.ModifiedDate,
Currency = group.Currency.Code,
ExtendedPrice = (double)group.ExtendedPrice.InternalAmount,
ExtendedPriceWithTax = (double)group.ExtendedPriceWithTax.InternalAmount,
ListPrice = (double)group.ListPrice.InternalAmount,
ListPriceWithTax = (double)group.ListPriceWithTax.InternalAmount,
PlacedPrice = (double)group.PlacedPrice.InternalAmount,
PlacedPriceWithTax = (double)group.PlacedPriceWithTax.InternalAmount,
SalePrice = (double)group.SalePrice.InternalAmount,
SalePriceWithTax = (double)group.SalePriceWithTax.InternalAmount,
TaxTotal = (double)group.TaxTotal.InternalAmount,
Quantity = group.Quantity
};
}

public static ConfiguredGroup ToConfiguredGroup(this cartDto.DemoCartConfiguredGroup group, ShoppingCart cart)
{
var result = new ConfiguredGroup(
group.Quantity ?? 0, cart.Currency, new Money(group.ExtendedPrice ?? 0, cart.Currency),
new Money(group.ExtendedPriceWithTax ?? 0, cart.Currency),
new Money(group.TaxTotal ?? 0, cart.Currency))
{
Id = group.Id,
CreatedBy = group.CreatedBy,
CreatedDate = group.CreatedDate ?? DateTime.UtcNow,
ModifiedBy = group.ModifiedBy,
ModifiedDate = group.ModifiedDate,
ListPrice = new Money(group.ListPrice ?? 0, cart.Currency),
ListPriceWithTax = new Money(group.ListPriceWithTax ?? 0, cart.Currency),
SalePrice = new Money(group.SalePrice ?? 0, cart.Currency),
SalePriceWithTax = new Money(group.SalePriceWithTax ?? 0, cart.Currency),
PlacedPrice = new Money(group.PlacedPrice ?? 0, cart.Currency),
PlacedPriceWithTax = new Money(group.PlacedPriceWithTax ?? 0, cart.Currency),
Currency = cart.Currency,

};

foreach (var item in group.ItemIds.Select(id => cart.Items.First(x => x.Id == id)))
{
result.Items.Add(item);
}

return result;
}
}
}
16 changes: 8 additions & 8 deletions VirtoCommerce.Storefront/Domain/Cart/Demo/DemoCartBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ public override Task RemoveItemAsync(string lineItemId)
{
EnsureCartExists();

var configureLineItem = Cart.ConfiguredItems.FirstOrDefault(x => x.ConfiguredLineItem?.ProductId.Equals(lineItemId, StringComparison.InvariantCulture) ?? false);
var configureLineItem = Cart.ConfiguredGroups.FirstOrDefault(x => x.ConfiguredLineItem?.ProductId.Equals(lineItemId, StringComparison.InvariantCulture) ?? false);

if (configureLineItem != null)
{
var configurablePieces = Cart.Items.Where(x => x.ConfiguredProductId?.Equals(lineItemId, StringComparison.InvariantCulture) ?? false).ToArray();
var configurablePieces = Cart.Items.Where(x => x.ConfiguredGropupId?.Equals(lineItemId, StringComparison.InvariantCulture) ?? false).ToArray();

foreach (var configuirablePieceLineItem in configurablePieces)
{
Cart.Items.Remove(configuirablePieceLineItem);
}

Cart.ConfiguredItems.Remove(configureLineItem);
Cart.ConfiguredGroups.Remove(configureLineItem);
}

return base.RemoveItemAsync(lineItemId);
Expand All @@ -55,16 +55,16 @@ public override Task ChangeItemQuantityAsync(ChangeCartItemQty changeItemQty)
{
EnsureCartExists();

var configuredProduct = Cart.ConfiguredItems?.FirstOrDefault(x =>
var configuredProduct = Cart.ConfiguredGroups?.FirstOrDefault(x =>
x.ConfiguredLineItem?.ProductId.Equals(changeItemQty.LineItemId, StringComparison.InvariantCulture) ?? false);

if (configuredProduct != null)
{
foreach (var lineItem in Cart
.Items
.Where(x =>
!string.IsNullOrEmpty(x.ConfiguredProductId) &&
x.ConfiguredProductId.Equals(configuredProduct.ConfiguredLineItem.ProductId)
!string.IsNullOrEmpty(x.ConfiguredGropupId) &&
x.ConfiguredGropupId.Equals(configuredProduct.ConfiguredLineItem.ProductId)
)
)
{
Expand All @@ -85,7 +85,7 @@ public override async Task<bool> AddItemAsync(AddCartItem addCartItem)
{
var lineItem = addCartItem.Product.ToLineItem(Cart.Language, addCartItem.Quantity);
lineItem.Product = addCartItem.Product;
lineItem.ConfiguredProductId = addCartItem.ConfiguredProductId;
lineItem.ConfiguredGropupId = addCartItem.ConfiguredProductId;

if (addCartItem.Price != null)
{
Expand Down Expand Up @@ -116,7 +116,7 @@ public override async Task<bool> AddItemAsync(AddCartItem addCartItem)

protected override async Task AddLineItemAsync(LineItem lineItem)
{
if (!string.IsNullOrEmpty(lineItem.ConfiguredProductId))
if (!string.IsNullOrEmpty(lineItem.ConfiguredGropupId))
{
lineItem.Id = null;
Cart.Items.Add(lineItem);
Expand Down
6 changes: 3 additions & 3 deletions VirtoCommerce.Storefront/Domain/Cart/Demo/DemoCartService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ public override async Task<IPagedList<ShoppingCart>> SearchCartsAsync(CartSearch

protected virtual async Task AddConfiguredItemsToCartAsync(ShoppingCart cart, Language language, Currency currency)
{
foreach (var grouping in cart.Items.Where(x => !x.ConfiguredProductId.IsNullOrEmpty()).GroupBy(x => x.ConfiguredProductId))
foreach (var grouping in cart.Items.Where(x => !x.ConfiguredGropupId.IsNullOrEmpty()).GroupBy(x => x.ConfiguredGropupId))
{
var configuredProductId = grouping.Key;
var configuredProductItems = grouping.AsEnumerable().ToArray();
var configuredProductQuantity = configuredProductItems.FirstOrDefault()?.Quantity ?? 1;

var configuredItem = new ConfiguredItem();
var configuredItem = new ConfiguredGroup();
var product = (await _catalogService.GetProductsAsync(new[] {configuredProductId}, ItemResponseGroup.None)).FirstOrDefault();

configuredItem.ConfiguredLineItem = product?.ToLineItem(language, configuredProductQuantity);
Expand All @@ -109,7 +109,7 @@ protected virtual async Task AddConfiguredItemsToCartAsync(ShoppingCart cart, La
.OrderBy(x => x.Name)
);

cart.ConfiguredItems.Add(configuredItem);
cart.ConfiguredGroups.Add(configuredItem);
}
}
}
Expand Down

0 comments on commit 2768ea7

Please sign in to comment.