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) protected virtual OccupyAssetInfoModel CreateOccupyAssetInfoModel(Guid assetId, CreateOrderLineDto orderLine)
{ {
var bookingDate = return new OccupyAssetInfoModel(
Check.NotNull(orderLine.FindBookingDate(), BookingOrderProperties.OrderLineBookingDate)!.Value; assetId,
orderLine.GetBookingDate(),
var bookingStartingTime = Check.NotNull(orderLine.FindBookingStartingTime(), orderLine.GetBookingStartingTime(),
BookingOrderProperties.OrderLineBookingStartingTime)!.Value; orderLine.GetBookingDuration()
);
var bookingDuration = Check.NotNull(orderLine.FindBookingDuration(),
BookingOrderProperties.OrderLineBookingDuration)!.Value;
return new OccupyAssetInfoModel(assetId, bookingDate, bookingStartingTime, bookingDuration);
} }
protected virtual OccupyAssetByCategoryInfoModel CreateOccupyAssetByCategoryInfoModel(Guid assetCategoryId, protected virtual OccupyAssetByCategoryInfoModel CreateOccupyAssetByCategoryInfoModel(Guid assetCategoryId,
CreateOrderLineDto orderLine) 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( return new OccupyAssetByCategoryInfoModel(
assetCategoryId, bookingDate, bookingStartingTime, bookingDuration); assetCategoryId,
orderLine.GetBookingDate(),
orderLine.GetBookingStartingTime(),
orderLine.GetBookingDuration()
);
} }
protected virtual async Task<bool> IsAssetInfoValidAsync(CreateOrderLineDto orderLine, protected virtual async Task<bool> IsAssetInfoValidAsync(CreateOrderLineDto orderLine,
@ -157,8 +148,8 @@ namespace EasyAbp.EShop.Orders.Booking.Authorization
StoreId = resource.Input.StoreId, StoreId = resource.Input.StoreId,
ProductId = orderLine.ProductId, ProductId = orderLine.ProductId,
ProductSkuId = orderLine.ProductSkuId, ProductSkuId = orderLine.ProductSkuId,
AssetId = orderLine.FindBookingAssetId(), AssetId = orderLine.GetBookingAssetId(),
PeriodSchemeId = orderLine.FindBookingPeriodSchemeId() PeriodSchemeId = orderLine.GetBookingPeriodSchemeId()
} }
)).Items.FirstOrDefault(); )).Items.FirstOrDefault();
@ -175,8 +166,8 @@ namespace EasyAbp.EShop.Orders.Booking.Authorization
StoreId = resource.Input.StoreId, StoreId = resource.Input.StoreId,
ProductId = orderLine.ProductId, ProductId = orderLine.ProductId,
ProductSkuId = orderLine.ProductSkuId, ProductSkuId = orderLine.ProductSkuId,
AssetCategoryId = orderLine.FindBookingAssetCategoryId(), AssetCategoryId = orderLine.GetBookingAssetCategoryId(),
PeriodSchemeId = orderLine.FindBookingPeriodSchemeId() PeriodSchemeId = orderLine.GetBookingPeriodSchemeId()
} }
)).Items.FirstOrDefault(); )).Items.FirstOrDefault();
@ -185,15 +176,10 @@ namespace EasyAbp.EShop.Orders.Booking.Authorization
protected virtual async Task<bool> IsPeriodInfoValidAsync(CreateOrderLineDto orderLine) protected virtual async Task<bool> IsPeriodInfoValidAsync(CreateOrderLineDto orderLine)
{ {
var periodSchemeId = orderLine.FindBookingPeriodSchemeId(); var periodSchemeId = orderLine.GetBookingPeriodSchemeId();
var periodId = orderLine.FindBookingPeriodId(); var periodId = orderLine.GetBookingPeriodId();
if (periodSchemeId is null || periodId is null)
{
return false;
}
var periodScheme = await _periodSchemeAppService.GetAsync(periodSchemeId.Value); var periodScheme = await _periodSchemeAppService.GetAsync(periodSchemeId);
var period = periodScheme.Periods.Find(x => x.Id == periodId); var period = periodScheme.Periods.Find(x => x.Id == periodId);
return period is not null; 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.Linq;
using System.Security.Principal; using System.Security.Principal;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -80,17 +81,7 @@ namespace EasyAbp.EShop.Payments.Booking.Authorization
return false; return false;
} }
var bookingDate = models.Add(CreateOccupyAssetInfoModel(assetId.Value, orderLine));
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));
} }
else if (assetCategoryId is not null) else if (assetCategoryId is not null)
{ {
@ -99,17 +90,7 @@ namespace EasyAbp.EShop.Payments.Booking.Authorization
return false; return false;
} }
var bookingDate = byCategoryModels.Add(CreateOccupyAssetByCategoryInfoModel(assetCategoryId.Value, orderLine));
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));
} }
else else
{ {
@ -133,6 +114,27 @@ namespace EasyAbp.EShop.Payments.Booking.Authorization
return true; 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) protected virtual async Task<bool> IsAssetInfoValidAsync(OrderDto order, OrderLineDto orderLine)
{ {
@ -143,8 +145,8 @@ namespace EasyAbp.EShop.Payments.Booking.Authorization
StoreId = order.StoreId, StoreId = order.StoreId,
ProductId = orderLine.ProductId, ProductId = orderLine.ProductId,
ProductSkuId = orderLine.ProductSkuId, ProductSkuId = orderLine.ProductSkuId,
AssetId = orderLine.FindBookingAssetId(), AssetId = orderLine.GetBookingAssetId(),
PeriodSchemeId = orderLine.FindBookingPeriodSchemeId() PeriodSchemeId = orderLine.GetBookingPeriodSchemeId()
} }
)).Items.FirstOrDefault(); )).Items.FirstOrDefault();
@ -160,8 +162,8 @@ namespace EasyAbp.EShop.Payments.Booking.Authorization
StoreId = order.StoreId, StoreId = order.StoreId,
ProductId = orderLine.ProductId, ProductId = orderLine.ProductId,
ProductSkuId = orderLine.ProductSkuId, ProductSkuId = orderLine.ProductSkuId,
AssetCategoryId = orderLine.FindBookingAssetCategoryId(), AssetCategoryId = orderLine.GetBookingAssetCategoryId(),
PeriodSchemeId = orderLine.FindBookingPeriodSchemeId() PeriodSchemeId = orderLine.GetBookingPeriodSchemeId()
} }
)).Items.FirstOrDefault(); )).Items.FirstOrDefault();
@ -170,15 +172,10 @@ namespace EasyAbp.EShop.Payments.Booking.Authorization
protected virtual async Task<bool> IsPeriodInfoValidAsync(OrderLineDto orderLine) protected virtual async Task<bool> IsPeriodInfoValidAsync(OrderLineDto orderLine)
{ {
var periodSchemeId = orderLine.FindBookingPeriodSchemeId(); var periodSchemeId = orderLine.GetBookingPeriodSchemeId();
var periodId = orderLine.FindBookingPeriodId(); var periodId = orderLine.GetBookingPeriodId();
if (periodSchemeId is null || periodId is null)
{
return false;
}
var periodScheme = await _periodSchemeAppService.GetAsync(periodSchemeId.Value); var periodScheme = await _periodSchemeAppService.GetAsync(periodSchemeId);
var period = periodScheme.Periods.Find(x => x.Id == periodId); var period = periodScheme.Periods.Find(x => x.Id == periodId);
return period is not null; 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 System;
using EasyAbp.EShop.Orders.Orders.Dtos; using EasyAbp.EShop.Orders.Orders.Dtos;
using Volo.Abp;
using Volo.Abp.Data; using Volo.Abp.Data;
namespace EasyAbp.EShop.Orders; namespace EasyAbp.EShop.Orders;
@ -10,34 +11,76 @@ public static class CreateOrderLineDtoExtensions
{ {
return orderLine.GetProperty<Guid?>(BookingOrderProperties.OrderLineBookingAssetId); 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) public static Guid? FindBookingAssetCategoryId(this CreateOrderLineDto orderLine)
{ {
return orderLine.GetProperty<Guid?>(BookingOrderProperties.OrderLineBookingAssetCategoryId); 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) public static Guid? FindBookingPeriodSchemeId(this CreateOrderLineDto orderLine)
{ {
return orderLine.GetProperty<Guid?>(BookingOrderProperties.OrderLineBookingPeriodSchemeId); 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) public static Guid? FindBookingPeriodId(this CreateOrderLineDto orderLine)
{ {
return orderLine.GetProperty<Guid?>(BookingOrderProperties.OrderLineBookingPeriodId); 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) public static DateTime? FindBookingDate(this CreateOrderLineDto orderLine)
{ {
return orderLine.GetProperty<DateTime?>(BookingOrderProperties.OrderLineBookingDate); 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) public static TimeSpan? FindBookingStartingTime(this CreateOrderLineDto orderLine)
{ {
return orderLine.GetProperty<TimeSpan?>(BookingOrderProperties.OrderLineBookingStartingTime); 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) public static TimeSpan? FindBookingDuration(this CreateOrderLineDto orderLine)
{ {
return orderLine.GetProperty<TimeSpan?>(BookingOrderProperties.OrderLineBookingDuration); 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 System;
using EasyAbp.EShop.Orders.Orders; using EasyAbp.EShop.Orders.Orders;
using Volo.Abp;
using Volo.Abp.Data; using Volo.Abp.Data;
namespace EasyAbp.EShop.Orders; namespace EasyAbp.EShop.Orders;
@ -10,34 +11,76 @@ public static class OrderLineExtensions
{ {
return orderLine.GetProperty<Guid?>(BookingOrderProperties.OrderLineBookingAssetId); 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) public static Guid? FindBookingAssetCategoryId(this IOrderLine orderLine)
{ {
return orderLine.GetProperty<Guid?>(BookingOrderProperties.OrderLineBookingAssetCategoryId); 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) public static Guid? FindBookingPeriodSchemeId(this IOrderLine orderLine)
{ {
return orderLine.GetProperty<Guid?>(BookingOrderProperties.OrderLineBookingPeriodSchemeId); 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) public static Guid? FindBookingPeriodId(this IOrderLine orderLine)
{ {
return orderLine.GetProperty<Guid?>(BookingOrderProperties.OrderLineBookingPeriodId); 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) public static DateTime? FindBookingDate(this IOrderLine orderLine)
{ {
return orderLine.GetProperty<DateTime?>(BookingOrderProperties.OrderLineBookingDate); 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) public static TimeSpan? FindBookingStartingTime(this IOrderLine orderLine)
{ {
return orderLine.GetProperty<TimeSpan?>(BookingOrderProperties.OrderLineBookingStartingTime); 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) public static TimeSpan? FindBookingDuration(this IOrderLine orderLine)
{ {
return orderLine.GetProperty<TimeSpan?>(BookingOrderProperties.OrderLineBookingDuration); return orderLine.GetProperty<TimeSpan?>(BookingOrderProperties.OrderLineBookingDuration);
} }
public static TimeSpan GetBookingDuration(this IOrderLine orderLine)
{
return Check.NotNull(FindBookingDuration(orderLine),
BookingOrderProperties.OrderLineBookingDuration)!.Value;
}
} }
Loading…
Cancel
Save