Commit eb75fb8a authored by gdlcf88's avatar gdlcf88

Product purchasable check feature, close #18

parent c26fc4e8
......@@ -23,5 +23,7 @@ namespace EasyAbp.EShop.Products.Products
Task<ProductDto> GetAsync(Guid id, Guid storeId);
Task<ProductDto> DeleteSkuAsync(Guid productId, Guid productSkuId, Guid storeId);
Task<GetProductPurchasableStatusResult> GetPurchasableStatusAsync(Guid productId, Guid productSkuId, Guid storeId);
}
}
\ No newline at end of file
......@@ -24,17 +24,20 @@ namespace EasyAbp.EShop.Products.Products
protected override string GetPolicyName { get; set; } = null;
protected override string GetListPolicyName { get; set; } = null;
private readonly IProductPurchasableStatusProvider _productPurchasableStatusProvider;
private readonly IAttributeOptionIdsSerializer _attributeOptionIdsSerializer;
private readonly IProductStoreRepository _productStoreRepository;
private readonly IProductCategoryRepository _productCategoryRepository;
private readonly IProductRepository _repository;
public ProductAppService(
IProductPurchasableStatusProvider productPurchasableStatusProvider,
IAttributeOptionIdsSerializer attributeOptionIdsSerializer,
IProductStoreRepository productStoreRepository,
IProductCategoryRepository productCategoryRepository,
IProductRepository repository) : base(repository)
{
_productPurchasableStatusProvider = productPurchasableStatusProvider;
_attributeOptionIdsSerializer = attributeOptionIdsSerializer;
_productStoreRepository = productStoreRepository;
_productCategoryRepository = productCategoryRepository;
......@@ -351,6 +354,15 @@ namespace EasyAbp.EShop.Products.Products
return ObjectMapper.Map<Product, ProductDto>(product);
}
public async Task<GetProductPurchasableStatusResult> GetPurchasableStatusAsync(Guid productId, Guid productSkuId, Guid storeId)
{
var product = await _repository.GetAsync(productId);
var productSku = product.ProductSkus.Single(sku => sku.Id == productSkuId);
return await _productPurchasableStatusProvider.GetPurchasableStatusAsync(product, productSku, storeId);
}
protected virtual async Task UpdateProductCategoriesAsync(Guid productId, IEnumerable<Guid> categoryIds)
{
await _productCategoryRepository.DeleteAsync(x => x.ProductId.Equals(productId));
......
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.Threading.Tasks;
namespace EasyAbp.EShop.Products.Products
{
public interface IProductPurchasableStatusProvider
{
Task CheckPurchasableAsync(Product product, ProductSku productSku, Guid storeId);
Task<GetProductPurchasableStatusResult> GetPurchasableStatusAsync(Product product, ProductSku productSku,
Guid storeId);
}
}
\ No newline at end of file
using System;
using Volo.Abp;
namespace EasyAbp.EShop.Products.Products
{
public class ProductIsNotPurchasableException : BusinessException
{
public ProductIsNotPurchasableException(Guid id, string reason) : base(
message: $"Product {id} cannot be purchased, the reason is: {reason}")
{
}
}
}
\ 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