Browse Source

Add pre-order response

pull/184/head
Jadyn 4 years ago
parent
commit
e491387cad
  1. 11
      plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application.Contracts/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/Dtos/FlashSalePlanPreOrderDto.cs
  2. 2
      plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application.Contracts/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/IFlashSalePlanAppService.cs
  3. 24
      plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSalePlanAppService.cs
  4. 2
      plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSalesOptions.cs
  5. 2
      plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.HttpApi/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSalePlanController.cs

11
plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application.Contracts/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/Dtos/FlashSalePlanPreOrderDto.cs

@ -0,0 +1,11 @@
using System;
using Volo.Abp.Application.Dtos;
namespace EasyAbp.EShop.Plugins.FlashSales.FlashSalePlans.Dtos;
public class FlashSalePlanPreOrderDto : ExtensibleEntityDto
{
public DateTime ExpiresTime { get; set; }
public double Expires { get; set; }
}

2
plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application.Contracts/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/IFlashSalePlanAppService.cs

@ -13,7 +13,7 @@ public interface IFlashSalePlanAppService :
FlashSalePlanCreateDto,
FlashSalePlanUpdateDto>
{
Task PreOrderAsync(Guid id);
Task<FlashSalePlanPreOrderDto> PreOrderAsync(Guid id);
Task<bool> OrderAsync(Guid id, CreateOrderInput input);
}

24
plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSalePlanAppService.cs

@ -19,6 +19,7 @@ using Volo.Abp.DistributedLocking;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.Timing;
using Volo.Abp.Users;
namespace EasyAbp.EShop.Plugins.FlashSales.FlashSalePlans;
@ -186,15 +187,18 @@ public class FlashSalePlanAppService :
.WhereIf(input.End.HasValue, x => x.BeginTime <= input.End.Value);
}
public virtual async Task PreOrderAsync(Guid id)
public virtual async Task<FlashSalePlanPreOrderDto> PreOrderAsync(Guid id)
{
var plan = await GetFlashSalePlanCacheAsync(id);
var product = await ProductAppService.GetAsync(plan.ProductId);
var productSku = product.GetSkuById(plan.ProductSkuId);
var expiresTime = DateTimeOffset.Now.Add(Options.PreOrderExpires);
await ValidatePreOrderAsync(plan, product, productSku);
await SetPreOrderCacheAsync(plan, product, productSku);
await SetPreOrderCacheAsync(plan, product, productSku, expiresTime);
return new FlashSalePlanPreOrderDto { ExpiresTime = Clock.Normalize(expiresTime.LocalDateTime), Expires = Options.PreOrderExpires.TotalSeconds };
}
public virtual async Task<bool> OrderAsync(Guid id, CreateOrderInput input)
@ -288,7 +292,7 @@ public class FlashSalePlanAppService :
await PreOrderDistributedCache.RemoveAsync(await GetPreOrderCacheKeyAsync(plan));
}
protected virtual async Task SetPreOrderCacheAsync(FlashSalePlanCacheItem plan, ProductDto product, ProductSkuDto productSku)
protected virtual async Task SetPreOrderCacheAsync(FlashSalePlanCacheItem plan, ProductDto product, ProductSkuDto productSku, DateTimeOffset expirationTime)
{
var hashToken = await FlashSalePlanHasher.HashAsync(plan.LastModificationTime, product.LastModificationTime, productSku.LastModificationTime);
@ -301,7 +305,7 @@ public class FlashSalePlanAppService :
InventoryProviderName = product.InventoryProviderName,
}, new DistributedCacheEntryOptions()
{
AbsoluteExpirationRelativeToNow = Options.PreOrderExpirationTime
AbsoluteExpiration = expirationTime
});
}
@ -328,18 +332,6 @@ public class FlashSalePlanAppService :
#endregion
protected virtual async Task<bool> CompareHashTokenAsync(string originHashToken, FlashSalePlanCacheItem plan, ProductDto product, ProductSkuDto productSku)
{
if (originHashToken.IsNullOrWhiteSpace())
{
return false;
}
var hashToken = await FlashSalePlanHasher.HashAsync(plan.LastModificationTime, product.LastModificationTime, productSku.LastModificationTime);
return string.Equals(hashToken, originHashToken, StringComparison.InvariantCulture);
}
protected virtual Task ValidatePreOrderAsync(FlashSalePlanCacheItem plan, ProductDto product, ProductSkuDto productSku)
{
if (!product.IsPublished)

2
plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSalesOptions.cs

@ -7,5 +7,5 @@ public class FlashSalesOptions
/// <summary>
/// Default: 3 minutes
/// </summary>
public TimeSpan PreOrderExpirationTime { get; set; } = TimeSpan.FromMinutes(3);
public TimeSpan PreOrderExpires { get; set; } = TimeSpan.FromMinutes(3);
}

2
plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.HttpApi/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSalePlanController.cs

@ -52,7 +52,7 @@ public class FlashSalePlanController :
}
[HttpPost("{id}/pre-order")]
public virtual Task PreOrderAsync(Guid id)
public virtual Task<FlashSalePlanPreOrderDto> PreOrderAsync(Guid id)
{
return Service.PreOrderAsync(id);
}

Loading…
Cancel
Save