Browse Source

Map extra properties for the Product aggregate

pull/231/head
gdlcf88 3 years ago
parent
commit
28c9eb82eb
  1. 19
      modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Application/EasyAbp/EShop/Products/Products/ProductAppService.cs
  2. 43
      modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.Application.Tests/Products/ProductAppServiceTests.cs

19
modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Application/EasyAbp/EShop/Products/Products/ProductAppService.cs

@ -12,6 +12,7 @@ using Microsoft.Extensions.Options;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Caching;
using Volo.Abp.Domain.Entities;
using Volo.Abp.ObjectExtending;
namespace EasyAbp.EShop.Products.Products
{
@ -481,7 +482,7 @@ namespace EasyAbp.EShop.Products.Products
protected override Task<Product> MapToEntityAsync(CreateUpdateProductDto createInput)
{
return Task.FromResult(new Product(
var entity = new Product(
GuidGenerator.Create(),
CurrentTenant.Id,
createInput.StoreId,
@ -496,7 +497,11 @@ namespace EasyAbp.EShop.Products.Products
createInput.IsHidden,
createInput.PaymentExpireIn,
createInput.MediaResources,
createInput.DisplayOrder));
createInput.DisplayOrder);
createInput.MapExtraPropertiesTo(entity);
return Task.FromResult(entity);
}
protected override Task MapToEntityAsync(CreateUpdateProductDto updateInput, Product entity)
@ -516,12 +521,14 @@ namespace EasyAbp.EShop.Products.Products
updateInput.MediaResources,
updateInput.DisplayOrder);
updateInput.MapExtraPropertiesTo(entity);
return Task.CompletedTask;
}
protected virtual async Task<ProductSku> MapToProductSkuAsync(CreateProductSkuDto createInput)
{
return new ProductSku(
var entity = new ProductSku(
GuidGenerator.Create(),
await _attributeOptionIdsSerializer.SerializeAsync(createInput.AttributeOptionIds),
createInput.Name,
@ -534,6 +541,10 @@ namespace EasyAbp.EShop.Products.Products
createInput.MediaResources,
createInput.ProductDetailId
);
createInput.MapExtraPropertiesTo(entity);
return entity;
}
protected virtual Task MapToProductSkuAsync(UpdateProductSkuDto updateInput, ProductSku entity)
@ -550,6 +561,8 @@ namespace EasyAbp.EShop.Products.Products
updateInput.ProductDetailId
);
updateInput.MapExtraPropertiesTo(entity);
return Task.CompletedTask;
}

43
modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.Application.Tests/Products/ProductAppServiceTests.cs

@ -8,7 +8,9 @@ using EasyAbp.EShop.Products.ProductDetails;
using EasyAbp.EShop.Products.Products.Dtos;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using Volo.Abp.Data;
using Volo.Abp.Domain.Entities;
using Volo.Abp.ObjectExtending;
using Xunit;
namespace EasyAbp.EShop.Products.Products
@ -22,7 +24,7 @@ namespace EasyAbp.EShop.Products.Products
{
_productAppService = GetRequiredService<IProductAppService>();
_eShopProductsOptions = GetRequiredService<IOptions<EShopProductsOptions>>().Value;
// Arrange
_eShopProductsOptions.Groups.Configure("Default Group Name", x =>
{
@ -47,14 +49,14 @@ namespace EasyAbp.EShop.Products.Products
IsPublished = true,
ProductAttributes = new List<CreateUpdateProductAttributeDto>
{
new CreateUpdateProductAttributeDto
new()
{
DisplayName = "Default Attribute 1",
Description = "Default Description 1",
DisplayOrder = 1,
ProductAttributeOptions = new List<CreateUpdateProductAttributeOptionDto>
{
new CreateUpdateProductAttributeOptionDto
new()
{
DisplayName = "Option 1"
}
@ -63,6 +65,13 @@ namespace EasyAbp.EShop.Products.Products
}
};
ObjectExtensionManager.Instance.AddOrUpdate(new[]
{
typeof(CreateUpdateProductDto), typeof(ProductDto), typeof(Product)
}, config => { config.AddOrUpdateProperty<string>("MyExtraProperty"); });
requestDto.SetProperty("MyExtraProperty", "1234");
// Act
var response = await _productAppService.CreateAsync(requestDto);
@ -71,6 +80,7 @@ namespace EasyAbp.EShop.Products.Products
response.IsPublished.ShouldBe(true);
response.DisplayName.ShouldBe("Pencil");
response.UniqueName.ShouldBe("Unique Pencil");
response.GetProperty<string>("MyExtraProperty").ShouldBe("1234");
UsingDbContext(db =>
{
@ -102,15 +112,23 @@ namespace EasyAbp.EShop.Products.Products
productAttributeOptionId = productAttribute.ProductAttributeOptions.First().Id;
});
var response = await _productAppService.CreateSkuAsync(productId, new CreateProductSkuDto
var requestDto = new CreateProductSkuDto
{
AttributeOptionIds = new List<Guid> {productAttributeOptionId},
AttributeOptionIds = new List<Guid> { productAttributeOptionId },
Currency = "USD",
Price = 1m,
OrderMinQuantity = 1,
OrderMaxQuantity = 10
});
};
ObjectExtensionManager.Instance.AddOrUpdate(new[]
{
typeof(CreateProductSkuDto), typeof(ProductSkuDto), typeof(ProductSku)
}, config => { config.AddOrUpdateProperty<string>("MyExtraProperty"); });
requestDto.SetProperty("MyExtraProperty", "1234");
var response = await _productAppService.CreateSkuAsync(productId, requestDto);
response.ShouldNotBeNull();
response.MinimumPrice.ShouldBe(1m);
response.MaximumPrice.ShouldBe(1m);
@ -123,6 +141,7 @@ namespace EasyAbp.EShop.Products.Products
responseSku.AttributeOptionIds.First().ShouldBe(productAttributeOptionId);
responseSku.OrderMinQuantity.ShouldBe(1);
responseSku.OrderMaxQuantity.ShouldBe(10);
responseSku.GetProperty("MyExtraProperty", "1234");
}
[Fact]
@ -132,7 +151,7 @@ namespace EasyAbp.EShop.Products.Products
{
StoreId = ProductsTestData.Store1Id
});
getListResult.Items.ShouldNotBeEmpty();
var productDto = getListResult.Items.FirstOrDefault(x => x.Id == ProductsTestData.Product1Id);
@ -140,19 +159,19 @@ namespace EasyAbp.EShop.Products.Products
productDto.ShouldNotBeNull();
productDto.MinimumPrice.ShouldBe(1m);
productDto.MaximumPrice.ShouldBe(3m);
var getResult = await _productAppService.GetAsync(ProductsTestData.Product1Id);
getResult.ShouldNotBeNull();
getResult.MinimumPrice.ShouldBe(1m);
getResult.MaximumPrice.ShouldBe(3m);
}
[Fact]
public async Task Should_Check_ProductDetailId()
{
var wrongProductDetailId = Guid.NewGuid();
var requestDto = new CreateUpdateProductDto
{
ProductGroupName = "Default Group Name",
@ -187,7 +206,7 @@ namespace EasyAbp.EShop.Products.Products
await _productAppService.CreateAsync(requestDto);
})).EntityType.ShouldBe(typeof(ProductDetail));
}
[Fact]
public async Task Should_Check_Sku_ProductDetailId()
{

Loading…
Cancel
Save