Commit 378d145a authored by gdlcf88's avatar gdlcf88

Improved product purchasable check, related to: #18

parent eb75fb8a
using System; using System;
using System.Collections.Generic;
using EasyAbp.EShop.Stores.Stores; using EasyAbp.EShop.Stores.Stores;
using JetBrains.Annotations; using JetBrains.Annotations;
using Volo.Abp.Domain.Entities.Auditing; using Volo.Abp.Domain.Entities.Auditing;
...@@ -40,5 +41,7 @@ namespace EasyAbp.EShop.Orders.Orders ...@@ -40,5 +41,7 @@ namespace EasyAbp.EShop.Orders.Orders
[CanBeNull] [CanBeNull]
public virtual string StaffRemark { get; protected set; } public virtual string StaffRemark { get; protected set; }
public virtual List<OrderLine> OrderLines { get; protected set; }
} }
} }
\ No newline at end of file
using System; using System;
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using EasyAbp.EShop.Products.Products.Dtos; using EasyAbp.EShop.Products.Products.Dtos;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services; using Volo.Abp.Application.Services;
namespace EasyAbp.EShop.Products.Products namespace EasyAbp.EShop.Products.Products
...@@ -24,6 +24,7 @@ namespace EasyAbp.EShop.Products.Products ...@@ -24,6 +24,7 @@ namespace EasyAbp.EShop.Products.Products
Task<ProductDto> DeleteSkuAsync(Guid productId, Guid productSkuId, Guid storeId); Task<ProductDto> DeleteSkuAsync(Guid productId, Guid productSkuId, Guid storeId);
Task<GetProductPurchasableStatusResult> GetPurchasableStatusAsync(Guid productId, Guid productSkuId, Guid storeId); Task<CheckProductPurchasableResult> CheckPurchasableAsync(Guid productId, Guid productSkuId, Guid storeId,
Dictionary<string, object> extraProperties);
} }
} }
\ No newline at end of file
...@@ -24,20 +24,20 @@ namespace EasyAbp.EShop.Products.Products ...@@ -24,20 +24,20 @@ namespace EasyAbp.EShop.Products.Products
protected override string GetPolicyName { get; set; } = null; protected override string GetPolicyName { get; set; } = null;
protected override string GetListPolicyName { get; set; } = null; protected override string GetListPolicyName { get; set; } = null;
private readonly IProductPurchasableStatusProvider _productPurchasableStatusProvider; private readonly IProductManager _productManager;
private readonly IAttributeOptionIdsSerializer _attributeOptionIdsSerializer; private readonly IAttributeOptionIdsSerializer _attributeOptionIdsSerializer;
private readonly IProductStoreRepository _productStoreRepository; private readonly IProductStoreRepository _productStoreRepository;
private readonly IProductCategoryRepository _productCategoryRepository; private readonly IProductCategoryRepository _productCategoryRepository;
private readonly IProductRepository _repository; private readonly IProductRepository _repository;
public ProductAppService( public ProductAppService(
IProductPurchasableStatusProvider productPurchasableStatusProvider, IProductManager productManager,
IAttributeOptionIdsSerializer attributeOptionIdsSerializer, IAttributeOptionIdsSerializer attributeOptionIdsSerializer,
IProductStoreRepository productStoreRepository, IProductStoreRepository productStoreRepository,
IProductCategoryRepository productCategoryRepository, IProductCategoryRepository productCategoryRepository,
IProductRepository repository) : base(repository) IProductRepository repository) : base(repository)
{ {
_productPurchasableStatusProvider = productPurchasableStatusProvider; _productManager = productManager;
_attributeOptionIdsSerializer = attributeOptionIdsSerializer; _attributeOptionIdsSerializer = attributeOptionIdsSerializer;
_productStoreRepository = productStoreRepository; _productStoreRepository = productStoreRepository;
_productCategoryRepository = productCategoryRepository; _productCategoryRepository = productCategoryRepository;
...@@ -354,13 +354,14 @@ namespace EasyAbp.EShop.Products.Products ...@@ -354,13 +354,14 @@ namespace EasyAbp.EShop.Products.Products
return ObjectMapper.Map<Product, ProductDto>(product); return ObjectMapper.Map<Product, ProductDto>(product);
} }
public async Task<GetProductPurchasableStatusResult> GetPurchasableStatusAsync(Guid productId, Guid productSkuId, Guid storeId) public async Task<CheckProductPurchasableResult> CheckPurchasableAsync(Guid productId, Guid productSkuId,
Guid storeId, Dictionary<string, object> extraProperties)
{ {
var product = await _repository.GetAsync(productId); var product = await _repository.GetAsync(productId);
var productSku = product.ProductSkus.Single(sku => sku.Id == productSkuId); var productSku = product.ProductSkus.Single(sku => sku.Id == productSkuId);
return await _productPurchasableStatusProvider.GetPurchasableStatusAsync(product, productSku, storeId); return await _productManager.GetPurchasableStatusAsync(product, productSku, storeId, extraProperties);
} }
protected virtual async Task UpdateProductCategoriesAsync(Guid productId, IEnumerable<Guid> categoryIds) protected virtual async Task UpdateProductCategoriesAsync(Guid productId, IEnumerable<Guid> categoryIds)
......
namespace EasyAbp.EShop.Products.Products
{
public class CheckProductPurchasableResult
{
public bool IsPurchasable { get; set; }
public string Reason { get; set; }
public CheckProductPurchasableResult(
bool isPurchasable,
string reason = null)
{
IsPurchasable = isPurchasable;
Reason = reason;
}
}
}
\ No newline at end of file
namespace EasyAbp.EShop.Products.Products
{
public class GetProductPurchasableStatusResult
{
public ProductPurchasableStatus PurchasableStatus { get; set; }
public string Reason { get; set; }
}
}
\ No newline at end of file
namespace EasyAbp.EShop.Products.Products
{
public enum ProductPurchasableStatus
{
CanBePurchased = 1,
CanNotBePurchased = 2
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
namespace EasyAbp.EShop.Products.Products
{
public class BasicProductPurchasableCheckHandler : IProductPurchasableCheckHandler, ITransientDependency
{
private readonly IProductInventoryProvider _productInventoryProvider;
public BasicProductPurchasableCheckHandler(
IProductInventoryProvider productInventoryProvider)
{
_productInventoryProvider = productInventoryProvider;
}
public async Task<CheckProductPurchasableResult> CheckAsync(Product product, ProductSku productSku, Guid storeId,
Dictionary<string, object> extraProperties)
{
if (!await IsProductPublishedAsync(product))
{
return new CheckProductPurchasableResult(false, "Unpublished project");
}
if (!await IsInventorySufficientAsync(product, productSku, storeId, extraProperties))
{
return new CheckProductPurchasableResult(false, "Insufficient inventory");
}
return new CheckProductPurchasableResult(true);
}
protected virtual Task<bool> IsProductPublishedAsync(Product product)
{
return Task.FromResult(product.IsPublished);
}
protected virtual async Task<bool> IsInventorySufficientAsync(Product product, ProductSku productSku, Guid storeId, Dictionary<string, object> extraProperties)
{
if (!extraProperties.TryGetValue("Quantity", out var quantity))
{
throw new ProductPurchasableCheckHandlerMissingPropertyException(
nameof(BasicProductPurchasableCheckHandler), "Quantity");
}
return await _productInventoryProvider.IsInventorySufficientAsync(product, productSku, storeId,
Convert.ToInt32(quantity));
}
}
}
\ No newline at end of file
using System;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
namespace EasyAbp.EShop.Products.Products
{
[Dependency(TryRegister = true)]
public class DefaultProductInventoryProvider : IProductInventoryProvider, ITransientDependency
{
public virtual async Task<bool> IsInventorySufficientAsync(Product product, ProductSku productSku, Guid storeId, int quantity)
{
var inventory = await GetInventoryAsync(product, productSku, storeId);
return product.InventoryStrategy == InventoryStrategy.NoNeed || inventory - quantity >= 0;
}
public virtual Task<int> GetInventoryAsync(Product product, ProductSku productSku, Guid storeId)
{
return Task.FromResult(productSku.Inventory);
}
}
}
\ No newline at end of file
using System;
using System.Threading.Tasks;
namespace EasyAbp.EShop.Products.Products
{
public interface IProductInventoryProvider
{
Task<bool> IsInventorySufficientAsync(Product product, ProductSku productSku, Guid storeId, int quantity);
Task<int> GetInventoryAsync(Product product, ProductSku productSku, Guid storeId);
}
}
\ No newline at end of file
using System; using System;
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.Domain.Services;
namespace EasyAbp.EShop.Products.Products namespace EasyAbp.EShop.Products.Products
{ {
public interface IProductPurchasableStatusProvider public interface IProductManager : IDomainService
{ {
Task CheckPurchasableAsync(Product product, ProductSku productSku, Guid storeId); Task CheckPurchasableAsync(Product product, ProductSku productSku, Guid storeId,
Dictionary<string, object> extraProperties);
Task<GetProductPurchasableStatusResult> GetPurchasableStatusAsync(Product product, ProductSku productSku, Task<CheckProductPurchasableResult> GetPurchasableStatusAsync(Product product, ProductSku productSku,
Guid storeId); Guid storeId, Dictionary<string, object> extraProperties);
} }
} }
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace EasyAbp.EShop.Products.Products
{
public interface IProductPurchasableCheckHandler
{
Task<CheckProductPurchasableResult> CheckAsync(Product product, ProductSku productSku, Guid storeId,
Dictionary<string, object> extraProperties);
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Domain.Services;
namespace EasyAbp.EShop.Products.Products
{
public class ProductManager : DomainService, IProductManager
{
public async Task CheckPurchasableAsync(Product product, ProductSku productSku, Guid storeId,
Dictionary<string, object> extraProperties)
{
var result = await GetPurchasableStatusAsync(product, productSku, storeId, extraProperties);
if (!result.IsPurchasable)
{
throw new ProductIsNotPurchasableException(product.Id, result.Reason);
}
}
public async Task<CheckProductPurchasableResult> GetPurchasableStatusAsync(Product product,
ProductSku productSku, Guid storeId, Dictionary<string, object> extraProperties)
{
var handlers = ServiceProvider.GetServices<IProductPurchasableCheckHandler>();
foreach (var handler in handlers)
{
var result = await handler.CheckAsync(product, productSku, storeId, extraProperties);
if (!result.IsPurchasable)
{
return result;
}
}
return new CheckProductPurchasableResult(true);
}
}
}
\ No newline at end of file
using System;
using Volo.Abp;
namespace EasyAbp.EShop.Products.Products
{
public class ProductPurchasableCheckHandlerMissingPropertyException : BusinessException
{
public ProductPurchasableCheckHandlerMissingPropertyException(string handlerName, string propertyKey) : base(
message: $"The {handlerName} is missing extra property: {propertyKey}")
{
}
}
}
\ No newline at end of file
using System;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
namespace EasyAbp.EShop.Products.Products
{
public class ProductPurchasableStatusProvider : IProductPurchasableStatusProvider, ITransientDependency
{
public async Task CheckPurchasableAsync(Product product, ProductSku productSku, Guid storeId)
{
var result = await GetPurchasableStatusAsync(product, productSku, storeId);
if (result.PurchasableStatus != ProductPurchasableStatus.CanBePurchased)
{
throw new ProductIsNotPurchasableException(product.Id, result.Reason);
}
}
public async Task<GetProductPurchasableStatusResult> GetPurchasableStatusAsync(Product product, ProductSku productSku, Guid storeId)
{
// Todo: Determine whether the product can be purchased.
return new GetProductPurchasableStatusResult
{
PurchasableStatus = ProductPurchasableStatus.CanBePurchased,
Reason = null
};
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment