Browse Source

Simplify code in authorization handlers

pull/157/head
gdlcf88 4 years ago
parent
commit
7d66b9de1a
  1. 50
      plugins/Booking/src/EasyAbp.EShop.Orders.Booking.Application/EasyAbp/EShop/Orders/Booking/Authorization/BookingOrderCreationAuthorizationHandler.cs
  2. 67
      plugins/Booking/src/EasyAbp.EShop.Payments.Booking.Application/EasyAbp/EShop/Payments/Booking/Authorization/BookingPaymentCreationAuthorizationHandler.cs
  3. 57
      plugins/Booking/src/EasyAbp.EShop.Plugins.Booking.Application.Contracts/EasyAbp/EShop/Orders/CreateOrderLineDtoExtensions.cs
  4. 51
      plugins/Booking/src/EasyAbp.EShop.Plugins.Booking.Application.Contracts/EasyAbp/EShop/Orders/OrderLineExtensions.cs

50
plugins/Booking/src/EasyAbp.EShop.Orders.Booking.Application/EasyAbp/EShop/Orders/Booking/Authorization/BookingOrderCreationAuthorizationHandler.cs

@ -119,32 +119,23 @@ namespace EasyAbp.EShop.Orders.Booking.Authorization
protected virtual OccupyAssetInfoModel CreateOccupyAssetInfoModel(Guid assetId, CreateOrderLineDto orderLine)
{
var bookingDate =
Check.NotNull(orderLine.FindBookingDate(), BookingOrderProperties.OrderLineBookingDate)!.Value;
var bookingStartingTime = Check.NotNull(orderLine.FindBookingStartingTime(),
BookingOrderProperties.OrderLineBookingStartingTime)!.Value;
var bookingDuration = Check.NotNull(orderLine.FindBookingDuration(),
BookingOrderProperties.OrderLineBookingDuration)!.Value;
return new OccupyAssetInfoModel(assetId, bookingDate, bookingStartingTime, bookingDuration);
return new OccupyAssetInfoModel(
assetId,
orderLine.GetBookingDate(),
orderLine.GetBookingStartingTime(),
orderLine.GetBookingDuration()
);
}
protected virtual OccupyAssetByCategoryInfoModel CreateOccupyAssetByCategoryInfoModel(Guid assetCategoryId,
CreateOrderLineDto orderLine)
{
var bookingDate =
Check.NotNull(orderLine.FindBookingDate(), BookingOrderProperties.OrderLineBookingDate)!.Value;
var bookingStartingTime = Check.NotNull(orderLine.FindBookingStartingTime(),
BookingOrderProperties.OrderLineBookingStartingTime)!.Value;
var bookingDuration = Check.NotNull(orderLine.FindBookingDuration(),
BookingOrderProperties.OrderLineBookingDuration)!.Value;
return new OccupyAssetByCategoryInfoModel(
assetCategoryId, bookingDate, bookingStartingTime, bookingDuration);
assetCategoryId,
orderLine.GetBookingDate(),
orderLine.GetBookingStartingTime(),
orderLine.GetBookingDuration()
);
}
protected virtual async Task<bool> IsAssetInfoValidAsync(CreateOrderLineDto orderLine,
@ -157,8 +148,8 @@ namespace EasyAbp.EShop.Orders.Booking.Authorization
StoreId = resource.Input.StoreId,
ProductId = orderLine.ProductId,
ProductSkuId = orderLine.ProductSkuId,
AssetId = orderLine.FindBookingAssetId(),
PeriodSchemeId = orderLine.FindBookingPeriodSchemeId()
AssetId = orderLine.GetBookingAssetId(),
PeriodSchemeId = orderLine.GetBookingPeriodSchemeId()
}
)).Items.FirstOrDefault();
@ -175,8 +166,8 @@ namespace EasyAbp.EShop.Orders.Booking.Authorization
StoreId = resource.Input.StoreId,
ProductId = orderLine.ProductId,
ProductSkuId = orderLine.ProductSkuId,
AssetCategoryId = orderLine.FindBookingAssetCategoryId(),
PeriodSchemeId = orderLine.FindBookingPeriodSchemeId()
AssetCategoryId = orderLine.GetBookingAssetCategoryId(),
PeriodSchemeId = orderLine.GetBookingPeriodSchemeId()
}
)).Items.FirstOrDefault();
@ -185,15 +176,10 @@ namespace EasyAbp.EShop.Orders.Booking.Authorization
protected virtual async Task<bool> IsPeriodInfoValidAsync(CreateOrderLineDto orderLine)
{
var periodSchemeId = orderLine.FindBookingPeriodSchemeId();
var periodId = orderLine.FindBookingPeriodId();
if (periodSchemeId is null || periodId is null)
{
return false;
}
var periodSchemeId = orderLine.GetBookingPeriodSchemeId();
var periodId = orderLine.GetBookingPeriodId();
var periodScheme = await _periodSchemeAppService.GetAsync(periodSchemeId.Value);
var periodScheme = await _periodSchemeAppService.GetAsync(periodSchemeId);
var period = periodScheme.Periods.Find(x => x.Id == periodId);
return period is not null;

67
plugins/Booking/src/EasyAbp.EShop.Payments.Booking.Application/EasyAbp/EShop/Payments/Booking/Authorization/BookingPaymentCreationAuthorizationHandler.cs

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Threading.Tasks;
@ -80,17 +81,7 @@ namespace EasyAbp.EShop.Payments.Booking.Authorization
return false;
}
var bookingDate =
Check.NotNull(orderLine.FindBookingDate(), BookingOrderProperties.OrderLineBookingDate)!.Value;
var bookingStartingTime = Check.NotNull(orderLine.FindBookingStartingTime(),
BookingOrderProperties.OrderLineBookingStartingTime)!.Value;
var bookingDuration = Check.NotNull(orderLine.FindBookingDuration(),
BookingOrderProperties.OrderLineBookingDuration)!.Value;
models.Add(new OccupyAssetInfoModel(
assetId.Value, bookingDate, bookingStartingTime, bookingDuration));
models.Add(CreateOccupyAssetInfoModel(assetId.Value, orderLine));
}
else if (assetCategoryId is not null)
{
@ -99,17 +90,7 @@ namespace EasyAbp.EShop.Payments.Booking.Authorization
return false;
}
var bookingDate =
Check.NotNull(orderLine.FindBookingDate(), BookingOrderProperties.OrderLineBookingDate)!.Value;
var bookingStartingTime = Check.NotNull(orderLine.FindBookingStartingTime(),
BookingOrderProperties.OrderLineBookingStartingTime)!.Value;
var bookingDuration = Check.NotNull(orderLine.FindBookingDuration(),
BookingOrderProperties.OrderLineBookingDuration)!.Value;
byCategoryModels.Add(new OccupyAssetByCategoryInfoModel(
assetCategoryId.Value, bookingDate, bookingStartingTime, bookingDuration));
byCategoryModels.Add(CreateOccupyAssetByCategoryInfoModel(assetCategoryId.Value, orderLine));
}
else
{
@ -133,6 +114,27 @@ namespace EasyAbp.EShop.Payments.Booking.Authorization
return true;
}
protected virtual OccupyAssetInfoModel CreateOccupyAssetInfoModel(Guid assetId, OrderLineDto orderLine)
{
return new OccupyAssetInfoModel(
assetId,
orderLine.GetBookingDate(),
orderLine.GetBookingStartingTime(),
orderLine.GetBookingDuration()
);
}
protected virtual OccupyAssetByCategoryInfoModel CreateOccupyAssetByCategoryInfoModel(Guid assetCategoryId,
OrderLineDto orderLine)
{
return new OccupyAssetByCategoryInfoModel(
assetCategoryId,
orderLine.GetBookingDate(),
orderLine.GetBookingStartingTime(),
orderLine.GetBookingDuration()
);
}
protected virtual async Task<bool> IsAssetInfoValidAsync(OrderDto order, OrderLineDto orderLine)
{
@ -143,8 +145,8 @@ namespace EasyAbp.EShop.Payments.Booking.Authorization
StoreId = order.StoreId,
ProductId = orderLine.ProductId,
ProductSkuId = orderLine.ProductSkuId,
AssetId = orderLine.FindBookingAssetId(),
PeriodSchemeId = orderLine.FindBookingPeriodSchemeId()
AssetId = orderLine.GetBookingAssetId(),
PeriodSchemeId = orderLine.GetBookingPeriodSchemeId()
}
)).Items.FirstOrDefault();
@ -160,8 +162,8 @@ namespace EasyAbp.EShop.Payments.Booking.Authorization
StoreId = order.StoreId,
ProductId = orderLine.ProductId,
ProductSkuId = orderLine.ProductSkuId,
AssetCategoryId = orderLine.FindBookingAssetCategoryId(),
PeriodSchemeId = orderLine.FindBookingPeriodSchemeId()
AssetCategoryId = orderLine.GetBookingAssetCategoryId(),
PeriodSchemeId = orderLine.GetBookingPeriodSchemeId()
}
)).Items.FirstOrDefault();
@ -170,15 +172,10 @@ namespace EasyAbp.EShop.Payments.Booking.Authorization
protected virtual async Task<bool> IsPeriodInfoValidAsync(OrderLineDto orderLine)
{
var periodSchemeId = orderLine.FindBookingPeriodSchemeId();
var periodId = orderLine.FindBookingPeriodId();
if (periodSchemeId is null || periodId is null)
{
return false;
}
var periodSchemeId = orderLine.GetBookingPeriodSchemeId();
var periodId = orderLine.GetBookingPeriodId();
var periodScheme = await _periodSchemeAppService.GetAsync(periodSchemeId.Value);
var periodScheme = await _periodSchemeAppService.GetAsync(periodSchemeId);
var period = periodScheme.Periods.Find(x => x.Id == periodId);
return period is not null;

57
plugins/Booking/src/EasyAbp.EShop.Plugins.Booking.Application.Contracts/EasyAbp/EShop/Orders/CreateOrderLineDtoExtensions.cs

@ -1,5 +1,6 @@
using System;
using EasyAbp.EShop.Orders.Orders.Dtos;
using Volo.Abp;
using Volo.Abp.Data;
namespace EasyAbp.EShop.Orders;
@ -10,34 +11,76 @@ public static class CreateOrderLineDtoExtensions
{
return orderLine.GetProperty<Guid?>(BookingOrderProperties.OrderLineBookingAssetId);
}
public static Guid GetBookingAssetId(this CreateOrderLineDto orderLine)
{
return Check.NotNull(FindBookingAssetId(orderLine),
BookingOrderProperties.OrderLineBookingAssetId)!.Value;
}
public static Guid? FindBookingAssetCategoryId(this CreateOrderLineDto orderLine)
{
return orderLine.GetProperty<Guid?>(BookingOrderProperties.OrderLineBookingAssetCategoryId);
}
public static Guid GetBookingAssetCategoryId(this CreateOrderLineDto orderLine)
{
return Check.NotNull(FindBookingAssetCategoryId(orderLine),
BookingOrderProperties.OrderLineBookingAssetCategoryId)!.Value;
}
public static Guid? FindBookingPeriodSchemeId(this CreateOrderLineDto orderLine)
{
return orderLine.GetProperty<Guid?>(BookingOrderProperties.OrderLineBookingPeriodSchemeId);
}
public static Guid GetBookingPeriodSchemeId(this CreateOrderLineDto orderLine)
{
return Check.NotNull(FindBookingPeriodSchemeId(orderLine),
BookingOrderProperties.OrderLineBookingPeriodSchemeId)!.Value;
}
public static Guid? FindBookingPeriodId(this CreateOrderLineDto orderLine)
{
return orderLine.GetProperty<Guid?>(BookingOrderProperties.OrderLineBookingPeriodId);
}
public static Guid GetBookingPeriodId(this CreateOrderLineDto orderLine)
{
return Check.NotNull(FindBookingPeriodId(orderLine),
BookingOrderProperties.OrderLineBookingPeriodId)!.Value;
}
public static DateTime? FindBookingDate(this CreateOrderLineDto orderLine)
{
return orderLine.GetProperty<DateTime?>(BookingOrderProperties.OrderLineBookingDate);
}
public static DateTime GetBookingDate(this CreateOrderLineDto orderLine)
{
return Check.NotNull(FindBookingDate(orderLine),
BookingOrderProperties.OrderLineBookingDate)!.Value;
}
public static TimeSpan? FindBookingStartingTime(this CreateOrderLineDto orderLine)
{
return orderLine.GetProperty<TimeSpan?>(BookingOrderProperties.OrderLineBookingStartingTime);
}
public static TimeSpan GetBookingStartingTime(this CreateOrderLineDto orderLine)
{
return Check.NotNull(FindBookingStartingTime(orderLine),
BookingOrderProperties.OrderLineBookingStartingTime)!.Value;
}
public static TimeSpan? FindBookingDuration(this CreateOrderLineDto orderLine)
{
return orderLine.GetProperty<TimeSpan?>(BookingOrderProperties.OrderLineBookingDuration);
}
}
public static TimeSpan GetBookingDuration(this CreateOrderLineDto orderLine)
{
return Check.NotNull(FindBookingDuration(orderLine),
BookingOrderProperties.OrderLineBookingDuration)!.Value;
}
}

51
plugins/Booking/src/EasyAbp.EShop.Plugins.Booking.Application.Contracts/EasyAbp/EShop/Orders/OrderLineExtensions.cs

@ -1,5 +1,6 @@
using System;
using EasyAbp.EShop.Orders.Orders;
using Volo.Abp;
using Volo.Abp.Data;
namespace EasyAbp.EShop.Orders;
@ -10,34 +11,76 @@ public static class OrderLineExtensions
{
return orderLine.GetProperty<Guid?>(BookingOrderProperties.OrderLineBookingAssetId);
}
public static Guid GetBookingAssetId(this IOrderLine orderLine)
{
return Check.NotNull(FindBookingAssetId(orderLine),
BookingOrderProperties.OrderLineBookingAssetId)!.Value;
}
public static Guid? FindBookingAssetCategoryId(this IOrderLine orderLine)
{
return orderLine.GetProperty<Guid?>(BookingOrderProperties.OrderLineBookingAssetCategoryId);
}
public static Guid GetBookingAssetCategoryId(this IOrderLine orderLine)
{
return Check.NotNull(FindBookingAssetCategoryId(orderLine),
BookingOrderProperties.OrderLineBookingAssetCategoryId)!.Value;
}
public static Guid? FindBookingPeriodSchemeId(this IOrderLine orderLine)
{
return orderLine.GetProperty<Guid?>(BookingOrderProperties.OrderLineBookingPeriodSchemeId);
}
public static Guid GetBookingPeriodSchemeId(this IOrderLine orderLine)
{
return Check.NotNull(FindBookingPeriodSchemeId(orderLine),
BookingOrderProperties.OrderLineBookingPeriodSchemeId)!.Value;
}
public static Guid? FindBookingPeriodId(this IOrderLine orderLine)
{
return orderLine.GetProperty<Guid?>(BookingOrderProperties.OrderLineBookingPeriodId);
}
public static Guid GetBookingPeriodId(this IOrderLine orderLine)
{
return Check.NotNull(FindBookingPeriodId(orderLine),
BookingOrderProperties.OrderLineBookingPeriodId)!.Value;
}
public static DateTime? FindBookingDate(this IOrderLine orderLine)
{
return orderLine.GetProperty<DateTime?>(BookingOrderProperties.OrderLineBookingDate);
}
public static DateTime GetBookingDate(this IOrderLine orderLine)
{
return Check.NotNull(FindBookingDate(orderLine),
BookingOrderProperties.OrderLineBookingDate)!.Value;
}
public static TimeSpan? FindBookingStartingTime(this IOrderLine orderLine)
{
return orderLine.GetProperty<TimeSpan?>(BookingOrderProperties.OrderLineBookingStartingTime);
}
public static TimeSpan GetBookingStartingTime(this IOrderLine orderLine)
{
return Check.NotNull(FindBookingStartingTime(orderLine),
BookingOrderProperties.OrderLineBookingStartingTime)!.Value;
}
public static TimeSpan? FindBookingDuration(this IOrderLine orderLine)
{
return orderLine.GetProperty<TimeSpan?>(BookingOrderProperties.OrderLineBookingDuration);
}
public static TimeSpan GetBookingDuration(this IOrderLine orderLine)
{
return Check.NotNull(FindBookingDuration(orderLine),
BookingOrderProperties.OrderLineBookingDuration)!.Value;
}
}
Loading…
Cancel
Save