protected virtual async Task <bool> IsBookingOrderValidAsync(AuthorizationHandlerContext context, OrderDto order) { var bookingOrderLines = order.OrderLines.Where(x => x.FindBookingAssetId() is not null).ToList(); if (!bookingOrderLines.Any()) { return(true); } var models = new List <OccupyAssetInfoModel>(); var byCategoryModels = new List <OccupyAssetByCategoryInfoModel>(); foreach (var orderLine in bookingOrderLines) { if (!await IsPeriodInfoValidAsync(orderLine)) { return(false); } var assetId = orderLine.FindBookingAssetId(); var assetCategoryId = orderLine.FindBookingAssetCategoryId(); if (assetId is not null) { models.Add(CreateOccupyAssetInfoModel(assetId.Value, orderLine)); } else if (assetCategoryId is not null) { byCategoryModels.Add(CreateOccupyAssetByCategoryInfoModel(assetCategoryId.Value, orderLine)); } else { return(false); } } try { await _assetOccupancyAppService.CheckBulkCreateAsync(new BulkCreateAssetOccupancyDto { OccupierUserId = Check.NotNull(context.User.FindUserId(), "CurrentUserId"), Models = models, ByCategoryModels = byCategoryModels }); } catch { return(false); } return(true); }
protected override async Task HandleOrderCreationAsync(AuthorizationHandlerContext context, OrderOperationAuthorizationRequirement requirement, OrderCreationResource resource) { var productGroupNames = (await _definitionAppService.GetListAsync()).Items.Select(x => x.ProductGroupName); var bookingOrderLines = resource.Input.OrderLines.Where(x => productGroupNames.Contains(resource.ProductDictionary[x.ProductId].ProductGroupName)).ToList(); if (!bookingOrderLines.Any()) { return; } var models = new List <OccupyAssetInfoModel>(); var byCategoryModels = new List <OccupyAssetByCategoryInfoModel>(); foreach (var orderLine in bookingOrderLines) { if (!await IsPeriodInfoValidAsync(orderLine)) { context.Fail(); return; } var assetId = orderLine.FindBookingAssetId(); var assetCategoryId = orderLine.FindBookingAssetCategoryId(); if (assetId is not null) { if (!await IsAssetInfoValidAsync(orderLine, resource)) { context.Fail(); return; } models.Add(CreateOccupyAssetInfoModel(assetId.Value, orderLine)); } else if (assetCategoryId is not null) { if (!await IsAssetCategoryInfoValidAsync(orderLine, resource)) { context.Fail(); return; } byCategoryModels.Add(CreateOccupyAssetByCategoryInfoModel(assetCategoryId.Value, orderLine)); } else { context.Fail(); return; } } try { await _assetOccupancyAppService.CheckBulkCreateAsync(new BulkCreateAssetOccupancyDto { OccupierUserId = Check.NotNull(context.User.FindUserId(), "CurrentUserId"), Models = models, ByCategoryModels = byCategoryModels }); } catch { context.Fail(); return; } }