public async Task SetSpecificationAttributesAsync(AbcMattressModel model, Product product) { var comfortSpecAttr = (await _specificationAttributeService.GetSpecificationAttributesAsync()) .Where(sa => sa.Name == "Comfort") .FirstOrDefault(); if (comfortSpecAttr == null) { throw new NopException("Unable to find 'Comfort' specification attribute."); } var options = await _specificationAttributeService.GetSpecificationAttributeOptionsBySpecificationAttributeAsync(comfortSpecAttr.Id); var option = options.Where(so => so.Name == model.Comfort) .FirstOrDefault(); if (option == null) { throw new NopException($"Unable to find '{model.Comfort}' " + "specification attribute option for the 'Comfort' " + "specification attribute."); } var productSpecificationAttributes = await _specificationAttributeService.GetProductSpecificationAttributesAsync(product.Id); var comfortProductSpecAttr = productSpecificationAttributes.Where(psa => psa.SpecificationAttributeOptionId == option.Id) .FirstOrDefault(); // Found the existing product spec. attribute - just skip processing if (comfortProductSpecAttr != null) { return; } // delete any currently existing Comfort specs var optionIds = options.Select(o => o.Id); var existingComfortProductSpecAttributes = productSpecificationAttributes.Where(psa => optionIds.Contains(psa.SpecificationAttributeOptionId)); foreach (var psa in existingComfortProductSpecAttributes) { await _specificationAttributeService.DeleteProductSpecificationAttributeAsync(psa); } // Add new product spec attribute var productSpecAttr = new ProductSpecificationAttribute() { ProductId = product.Id, AttributeType = SpecificationAttributeType.Option, SpecificationAttributeOptionId = option.Id, AllowFiltering = true }; await _specificationAttributeService.InsertProductSpecificationAttributeAsync( productSpecAttr ); }
public async Task <IActionResult> GetSpecificationAttributesCount(SpecificationAttributesCountParametersModel parameters) { var specificationAttributesCount = (await _specificationAttributeService.GetSpecificationAttributesAsync()).TotalCount; var specificationAttributesCountRootObject = new SpecificationAttributesCountRootObject { Count = specificationAttributesCount }; return(Ok(specificationAttributesCountRootObject)); }
public async Task ExecuteAsync() { var specificationAttributes = await _specificationAttribute.GetSpecificationAttributesAsync(); var availableOnWeekdayAttribute = specificationAttributes.FirstOrDefault(x => x.Name.Equals("Available On Weekday")); if (availableOnWeekdayAttribute == null) { await _logger.WarningAsync("WeekdayProductRotation: Can't get available on weekday attribute"); throw new Exception("Can't get available on weekday attribute"); } var availableOnWeekdayOptions = await _specificationAttribute.GetSpecificationAttributeOptionsBySpecificationAttributeAsync(availableOnWeekdayAttribute.Id); if (!availableOnWeekdayOptions.Any()) { await _logger.WarningAsync("WeekdayProductRotation: Can't get options"); throw new Exception("Can't get options"); } var storeDateTime = await _dateTimeHelper.ConvertToUserTimeAsync(DateTime.Now); var today3PMLocal = new DateTime(storeDateTime.Year, storeDateTime.Month, storeDateTime.Day, 15, 0, 0); var today3PMUtc = _dateTimeHelper.ConvertToUtcTime(today3PMLocal); //var lastSuccessfulRunUtc = // await _settingService.GetSettingByKeyAsync<DateTime>( // LAST_SUCCESSFUL_RUN_KEY, // loadSharedValueIfNotFound: true); //if(lastSuccessfulRunUtc <= today3PMUtc) //{ DayOfWeek targetWeekdayMenu; if (storeDateTime.Hour > 15) { targetWeekdayMenu = GetNextWeekDay(storeDateTime.DayOfWeek); } else { targetWeekdayMenu = storeDateTime.DayOfWeek; } var publishableSpecificationOptionIds = GetPublishableWeekdaySpecificationOptionIds(availableOnWeekdayOptions, targetWeekdayMenu); await _logger.InformationAsync( $"WeekdayProductRotation: Target weekday = {targetWeekdayMenu}, option ids = {string.Join(',', publishableSpecificationOptionIds)}"); var allProducts = await _productService.SearchProductsAsync(showHidden : true); await _logger.InformationAsync( $"WeekdayProductRotation: Found {allProducts.TotalCount} products, " + $"{allProducts.TotalPages} pages"); int unpublishedCount = 0, publishedCount = 0, untouchedCount = 0; foreach (var product in allProducts) { var productSpecificationAttributes = await _specificationAttribute.GetProductSpecificationAttributesAsync(productId : product.Id); // publish / unpublish product only when it have at least one "Available On Weekday" product specification if (productSpecificationAttributes.Any(x => availableOnWeekdayOptions.Any(y => y.Id == x.SpecificationAttributeOptionId))) { if (productSpecificationAttributes.Any(x => publishableSpecificationOptionIds.Contains(x.SpecificationAttributeOptionId))) { await _logger.InformationAsync( $"WeekdayProductRotation: Publishing product {product.Name}"); product.Published = true; publishedCount++; } else { await _logger.InformationAsync( $"WeekdayProductRotation: Unpublishing product {product.Name}"); product.Published = false; unpublishedCount++; } await _productService.UpdateProductAsync(product); } else { await _logger.InformationAsync( $"WeekdayProductRotation: No weekday specification attribute for product {product.Name}"); untouchedCount++; } } await _settingService.SetSettingAsync <DateTime>( LAST_SUCCESSFUL_RUN_KEY, storeDateTime, clearCache : true); await _logger.InformationAsync($"Published {publishedCount}, " + $"unpublished {unpublishedCount}, " + $"left untouched {untouchedCount} products"); //} //else //{ // await _logger.InformationAsync("WeekdayProductRotation: Skipping for now"); //} }