mirror of https://github.com/EasyAbp/EShop.git
committed by
GitHub
14 changed files with 82 additions and 246 deletions
@ -1,17 +0,0 @@ |
|||||
using System; |
|
||||
using Volo.Abp.Application.Dtos; |
|
||||
|
|
||||
namespace EasyAbp.EShop.Products.ProductInventories.Dtos |
|
||||
{ |
|
||||
[Serializable] |
|
||||
public class ProductInventoryDto : ExtensibleFullAuditedEntityDto<Guid> |
|
||||
{ |
|
||||
public Guid ProductId { get; set; } |
|
||||
|
|
||||
public Guid ProductSkuId { get; set; } |
|
||||
|
|
||||
public int Inventory { get; set; } |
|
||||
|
|
||||
public long Sold { get; set; } |
|
||||
} |
|
||||
} |
|
||||
@ -1,18 +0,0 @@ |
|||||
using System; |
|
||||
using Volo.Abp.ObjectExtending; |
|
||||
|
|
||||
namespace EasyAbp.EShop.Products.ProductInventories.Dtos |
|
||||
{ |
|
||||
[Serializable] |
|
||||
public class UpdateProductInventoryDto : ExtensibleObject |
|
||||
{ |
|
||||
public Guid ProductId { get; set; } |
|
||||
|
|
||||
public Guid ProductSkuId { get; set; } |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Reduce inventory if the value is less than 0
|
|
||||
/// </summary>
|
|
||||
public int ChangedInventory { get; set; } |
|
||||
} |
|
||||
} |
|
||||
@ -1,15 +0,0 @@ |
|||||
using System; |
|
||||
using System.Threading.Tasks; |
|
||||
using EasyAbp.EShop.Products.ProductInventories.Dtos; |
|
||||
using Volo.Abp.Application.Dtos; |
|
||||
using Volo.Abp.Application.Services; |
|
||||
|
|
||||
namespace EasyAbp.EShop.Products.ProductInventories |
|
||||
{ |
|
||||
public interface IProductInventoryAppService : IApplicationService |
|
||||
{ |
|
||||
Task<ProductInventoryDto> GetAsync(Guid productId, Guid productSkuId); |
|
||||
|
|
||||
Task<ProductInventoryDto> UpdateAsync(UpdateProductInventoryDto input); |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,13 @@ |
|||||
|
using System; |
||||
|
using Volo.Abp.ObjectExtending; |
||||
|
|
||||
|
namespace EasyAbp.EShop.Products.Products.Dtos; |
||||
|
|
||||
|
[Serializable] |
||||
|
public class ChangeProductInventoryDto : ExtensibleObject |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Reduce inventory if the value is less than 0
|
||||
|
/// </summary>
|
||||
|
public int ChangedInventory { get; set; } |
||||
|
} |
||||
@ -0,0 +1,14 @@ |
|||||
|
using System; |
||||
|
using Volo.Abp.ObjectExtending; |
||||
|
|
||||
|
namespace EasyAbp.EShop.Products.Products.Dtos; |
||||
|
|
||||
|
[Serializable] |
||||
|
public class ChangeProductInventoryResultDto : ExtensibleObject |
||||
|
{ |
||||
|
public bool Changed { get; set; } |
||||
|
|
||||
|
public int ChangedInventory { get; set; } |
||||
|
|
||||
|
public int CurrentInventory { get; set; } |
||||
|
} |
||||
@ -1,104 +0,0 @@ |
|||||
using EasyAbp.EShop.Products.Permissions; |
|
||||
using EasyAbp.EShop.Products.ProductInventories.Dtos; |
|
||||
using EasyAbp.EShop.Products.Products; |
|
||||
using EasyAbp.EShop.Stores.Authorization; |
|
||||
using Microsoft.AspNetCore.Authorization; |
|
||||
using System; |
|
||||
using System.Threading.Tasks; |
|
||||
using Volo.Abp.Application.Services; |
|
||||
using Volo.Abp.Domain.Entities; |
|
||||
|
|
||||
namespace EasyAbp.EShop.Products.ProductInventories |
|
||||
{ |
|
||||
public class ProductInventoryAppService : ApplicationService, IProductInventoryAppService |
|
||||
{ |
|
||||
private readonly IProductRepository _productRepository; |
|
||||
private readonly IProductInventoryRepository _repository; |
|
||||
private readonly DefaultProductInventoryProvider _defaultProductInventoryProvider; |
|
||||
|
|
||||
public ProductInventoryAppService( |
|
||||
IProductRepository productRepository, |
|
||||
IProductInventoryRepository repository, |
|
||||
DefaultProductInventoryProvider defaultProductInventoryProvider) |
|
||||
{ |
|
||||
_productRepository = productRepository; |
|
||||
_repository = repository; |
|
||||
_defaultProductInventoryProvider = defaultProductInventoryProvider; |
|
||||
} |
|
||||
|
|
||||
[Authorize(ProductsPermissions.ProductInventory.Default)] |
|
||||
public virtual async Task<ProductInventoryDto> GetAsync(Guid productId, Guid productSkuId) |
|
||||
{ |
|
||||
var productInventory = await _repository.FindAsync(x => x.ProductSkuId == productSkuId); |
|
||||
|
|
||||
if (productInventory == null) |
|
||||
{ |
|
||||
var product = await _productRepository.GetAsync(productId); |
|
||||
|
|
||||
if (!product.ProductSkus.Exists(x => x.Id == productSkuId)) |
|
||||
{ |
|
||||
throw new EntityNotFoundException(typeof(ProductSku), productSkuId); |
|
||||
} |
|
||||
|
|
||||
productInventory = new ProductInventory(GuidGenerator.Create(), CurrentTenant.Id, productId, |
|
||||
productSkuId, 0, 0); |
|
||||
|
|
||||
await _repository.InsertAsync(productInventory, true); |
|
||||
} |
|
||||
|
|
||||
return ObjectMapper.Map<ProductInventory, ProductInventoryDto>(productInventory); |
|
||||
} |
|
||||
|
|
||||
public virtual async Task<ProductInventoryDto> UpdateAsync(UpdateProductInventoryDto input) |
|
||||
{ |
|
||||
var product = await _productRepository.GetAsync(input.ProductId); |
|
||||
|
|
||||
if (!product.ProductSkus.Exists(x => x.Id == input.ProductSkuId)) |
|
||||
{ |
|
||||
throw new EntityNotFoundException(typeof(ProductSku), input.ProductSkuId); |
|
||||
} |
|
||||
|
|
||||
await AuthorizationService.CheckMultiStorePolicyAsync(product.StoreId, |
|
||||
ProductsPermissions.ProductInventory.Update, ProductsPermissions.ProductInventory.CrossStore); |
|
||||
|
|
||||
var productInventory = await _repository.FindAsync(x => x.ProductSkuId == input.ProductSkuId); |
|
||||
|
|
||||
if (productInventory == null) |
|
||||
{ |
|
||||
productInventory = |
|
||||
new ProductInventory(GuidGenerator.Create(), CurrentTenant.Id, input.ProductId, input.ProductSkuId, |
|
||||
0, 0); |
|
||||
|
|
||||
await _repository.InsertAsync(productInventory, true); |
|
||||
} |
|
||||
|
|
||||
await ChangeInventoryAsync(product, productInventory, input.ChangedInventory); |
|
||||
|
|
||||
return ObjectMapper.Map<ProductInventory, ProductInventoryDto>(productInventory); |
|
||||
} |
|
||||
|
|
||||
protected virtual async Task ChangeInventoryAsync(Product product, ProductInventory productInventory, |
|
||||
int changedInventory) |
|
||||
{ |
|
||||
var model = new InventoryQueryModel(product.TenantId, product.StoreId, product.Id, |
|
||||
productInventory.ProductSkuId); |
|
||||
|
|
||||
if (changedInventory >= 0) |
|
||||
{ |
|
||||
if (!await _defaultProductInventoryProvider.TryIncreaseInventoryAsync(model, changedInventory, false)) |
|
||||
{ |
|
||||
throw new InventoryChangeFailedException(productInventory.ProductId, productInventory.ProductSkuId, |
|
||||
productInventory.Inventory, changedInventory); |
|
||||
} |
|
||||
} |
|
||||
else |
|
||||
{ |
|
||||
if (!await _defaultProductInventoryProvider.TryReduceInventoryAsync(model, -changedInventory, false)) |
|
||||
{ |
|
||||
throw new InventoryChangeFailedException(productInventory.ProductId, productInventory.ProductSkuId, |
|
||||
productInventory.Inventory, changedInventory); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,33 +0,0 @@ |
|||||
using System; |
|
||||
using System.Threading.Tasks; |
|
||||
using EasyAbp.EShop.Products.ProductInventories.Dtos; |
|
||||
using Microsoft.AspNetCore.Mvc; |
|
||||
using Volo.Abp; |
|
||||
using Volo.Abp.Application.Dtos; |
|
||||
|
|
||||
namespace EasyAbp.EShop.Products.ProductInventories |
|
||||
{ |
|
||||
[RemoteService(Name = EShopProductsRemoteServiceConsts.RemoteServiceName)] |
|
||||
[Route("/api/e-shop/products/product-inventory")] |
|
||||
public class ProductInventoryController : ProductsController, IProductInventoryAppService |
|
||||
{ |
|
||||
private readonly IProductInventoryAppService _service; |
|
||||
|
|
||||
public ProductInventoryController(IProductInventoryAppService service) |
|
||||
{ |
|
||||
_service = service; |
|
||||
} |
|
||||
|
|
||||
[HttpGet] |
|
||||
public Task<ProductInventoryDto> GetAsync(Guid productId, Guid productSkuId) |
|
||||
{ |
|
||||
return _service.GetAsync(productId, productSkuId); |
|
||||
} |
|
||||
|
|
||||
[HttpPut] |
|
||||
public Task<ProductInventoryDto> UpdateAsync(UpdateProductInventoryDto input) |
|
||||
{ |
|
||||
return _service.UpdateAsync(input); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,26 +0,0 @@ |
|||||
using Shouldly; |
|
||||
using System.Threading.Tasks; |
|
||||
using Xunit; |
|
||||
|
|
||||
namespace EasyAbp.EShop.Products.ProductInventories |
|
||||
{ |
|
||||
public class ProductInventoryAppServiceTests : ProductsApplicationTestBase |
|
||||
{ |
|
||||
private readonly IProductInventoryAppService _productInventoryAppService; |
|
||||
|
|
||||
public ProductInventoryAppServiceTests() |
|
||||
{ |
|
||||
_productInventoryAppService = GetRequiredService<IProductInventoryAppService>(); |
|
||||
} |
|
||||
|
|
||||
[Fact] |
|
||||
public async Task Test1() |
|
||||
{ |
|
||||
// Arrange
|
|
||||
|
|
||||
// Act
|
|
||||
|
|
||||
// Assert
|
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue