From 7d66b9de1a390f862c91d4e2ebbb44b63084e031 Mon Sep 17 00:00:00 2001 From: gdlcf88 Date: Sun, 5 Jun 2022 00:58:11 +0800 Subject: [PATCH] Simplify code in authorization handlers --- ...ookingOrderCreationAuthorizationHandler.cs | 50 +++++--------- ...kingPaymentCreationAuthorizationHandler.cs | 67 +++++++++---------- .../Orders/CreateOrderLineDtoExtensions.cs | 57 ++++++++++++++-- .../EShop/Orders/OrderLineExtensions.cs | 51 ++++++++++++-- 4 files changed, 147 insertions(+), 78 deletions(-) diff --git a/plugins/Booking/src/EasyAbp.EShop.Orders.Booking.Application/EasyAbp/EShop/Orders/Booking/Authorization/BookingOrderCreationAuthorizationHandler.cs b/plugins/Booking/src/EasyAbp.EShop.Orders.Booking.Application/EasyAbp/EShop/Orders/Booking/Authorization/BookingOrderCreationAuthorizationHandler.cs index d01e9299..45b49d9b 100644 --- a/plugins/Booking/src/EasyAbp.EShop.Orders.Booking.Application/EasyAbp/EShop/Orders/Booking/Authorization/BookingOrderCreationAuthorizationHandler.cs +++ b/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 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 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; diff --git a/plugins/Booking/src/EasyAbp.EShop.Payments.Booking.Application/EasyAbp/EShop/Payments/Booking/Authorization/BookingPaymentCreationAuthorizationHandler.cs b/plugins/Booking/src/EasyAbp.EShop.Payments.Booking.Application/EasyAbp/EShop/Payments/Booking/Authorization/BookingPaymentCreationAuthorizationHandler.cs index 88b9f85c..3ac2c5f4 100644 --- a/plugins/Booking/src/EasyAbp.EShop.Payments.Booking.Application/EasyAbp/EShop/Payments/Booking/Authorization/BookingPaymentCreationAuthorizationHandler.cs +++ b/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 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 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; diff --git a/plugins/Booking/src/EasyAbp.EShop.Plugins.Booking.Application.Contracts/EasyAbp/EShop/Orders/CreateOrderLineDtoExtensions.cs b/plugins/Booking/src/EasyAbp.EShop.Plugins.Booking.Application.Contracts/EasyAbp/EShop/Orders/CreateOrderLineDtoExtensions.cs index e1921dee..987b3c74 100644 --- a/plugins/Booking/src/EasyAbp.EShop.Plugins.Booking.Application.Contracts/EasyAbp/EShop/Orders/CreateOrderLineDtoExtensions.cs +++ b/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(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(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(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(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(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(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(BookingOrderProperties.OrderLineBookingDuration); } -} \ No newline at end of file + + public static TimeSpan GetBookingDuration(this CreateOrderLineDto orderLine) + { + return Check.NotNull(FindBookingDuration(orderLine), + BookingOrderProperties.OrderLineBookingDuration)!.Value; + } +} diff --git a/plugins/Booking/src/EasyAbp.EShop.Plugins.Booking.Application.Contracts/EasyAbp/EShop/Orders/OrderLineExtensions.cs b/plugins/Booking/src/EasyAbp.EShop.Plugins.Booking.Application.Contracts/EasyAbp/EShop/Orders/OrderLineExtensions.cs index dfb07608..2b808e00 100644 --- a/plugins/Booking/src/EasyAbp.EShop.Plugins.Booking.Application.Contracts/EasyAbp/EShop/Orders/OrderLineExtensions.cs +++ b/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(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(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(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(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(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(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(BookingOrderProperties.OrderLineBookingDuration); } + + public static TimeSpan GetBookingDuration(this IOrderLine orderLine) + { + return Check.NotNull(FindBookingDuration(orderLine), + BookingOrderProperties.OrderLineBookingDuration)!.Value; + } } \ No newline at end of file