public ProductSpecificationAttributeValidator( IEnumerable <IValidatorConsumer <ProductSpecificationAttributeDto> > validators, ILocalizationService localizationService, ISpecificationAttributeService specificationAttributeService) : base(validators) { RuleFor(x => x).MustAsync(async(x, y, context) => { var specification = await specificationAttributeService.GetSpecificationAttributeById(x.SpecificationAttributeId); if (specification == null) { return(false); } return(true); }).WithMessage(localizationService.GetResource("Api.Catalog.ProductSpecificationAttribute.Fields.SpecificationAttributeId.NotExists")); RuleFor(x => x).MustAsync(async(x, y, context) => { if (!string.IsNullOrEmpty(x.SpecificationAttributeOptionId)) { var sa = await specificationAttributeService.GetSpecificationAttributeByOptionId(x.SpecificationAttributeOptionId); if (sa == null) { return(false); } } return(true); }).WithMessage(localizationService.GetResource("Api.Catalog.ProductSpecificationAttribute.Fields.SpecificationAttributeOptionId.NotExists")); }
public ProductSpecificationAttributeValidator(ILocalizationService localizationService, ISpecificationAttributeService specificationAttributeService) { RuleFor(x => x).Must((x, context) => { var specification = specificationAttributeService.GetSpecificationAttributeById(x.SpecificationAttributeId); if (specification == null) { return(false); } return(true); }).WithMessage(localizationService.GetResource("Api.Catalog.ProductSpecificationAttribute.Fields.SpecificationAttributeId.NotExists")); RuleFor(x => x).Must((x, context) => { if (!string.IsNullOrEmpty(x.SpecificationAttributeOptionId)) { var sa = specificationAttributeService.GetSpecificationAttributeByOptionId(x.SpecificationAttributeOptionId); if (sa == null) { return(false); } } return(true); }).WithMessage(localizationService.GetResource("Api.Catalog.ProductSpecificationAttribute.Fields.SpecificationAttributeOptionId.NotExists")); }
public async Task <IActionResult> OptionEditPopup(string id) { var sao = (await _specificationAttributeService.GetSpecificationAttributeByOptionId(id)).SpecificationAttributeOptions.Where(x => x.Id == id).FirstOrDefault(); if (sao == null) { //No specification attribute option found with the specified id return(RedirectToAction("List")); } var model = sao.ToModel(); model.EnableColorSquaresRgb = !String.IsNullOrEmpty(sao.ColorSquaresRgb); //locales await AddLocales(_languageService, model.Locales, (locale, languageId) => { locale.Name = sao.GetLocalized(x => x.Name, languageId, false, false); }); return(View(model)); }
public virtual async Task PrepareSpecsFilters(IList <string> alreadyFilteredSpecOptionIds, IList <string> filterableSpecificationAttributeOptionIds, ISpecificationAttributeService specificationAttributeService, IWebHelper webHelper, IWorkContext workContext, ICacheManager cacheManager) { Enabled = false; var optionIds = filterableSpecificationAttributeOptionIds != null ? string.Join(",", filterableSpecificationAttributeOptionIds.Union(alreadyFilteredSpecOptionIds)) : string.Empty; var cacheKey = string.Format(ModelCacheEventConst.SPECS_FILTER_MODEL_KEY, optionIds, workContext.WorkingLanguage.Id); var allFilters = await cacheManager.GetAsync(cacheKey, async() => { var _allFilters = new List <SpecificationAttributeOptionFilter>(); foreach (var sao in filterableSpecificationAttributeOptionIds.Union(alreadyFilteredSpecOptionIds)) { var sa = await specificationAttributeService.GetSpecificationAttributeByOptionId(sao); if (sa != null) { _allFilters.Add(new SpecificationAttributeOptionFilter { SpecificationAttributeId = sa.Id, SpecificationAttributeName = sa.GetLocalized(x => x.Name, workContext.WorkingLanguage.Id), SpecificationAttributeDisplayOrder = sa.DisplayOrder, SpecificationAttributeOptionId = sao, SpecificationAttributeOptionName = sa.SpecificationAttributeOptions.FirstOrDefault(x => x.Id == sao).GetLocalized(x => x.Name, workContext.WorkingLanguage.Id), SpecificationAttributeOptionDisplayOrder = sa.SpecificationAttributeOptions.FirstOrDefault(x => x.Id == sao).DisplayOrder, SpecificationAttributeOptionColorRgb = sa.SpecificationAttributeOptions.FirstOrDefault(x => x.Id == sao).ColorSquaresRgb, }); } } return(_allFilters.ToList()); }); if (!allFilters.Any()) { return; } //sort loaded options allFilters = allFilters.OrderBy(saof => saof.SpecificationAttributeDisplayOrder) .ThenBy(saof => saof.SpecificationAttributeName) .ThenBy(saof => saof.SpecificationAttributeOptionDisplayOrder) .ThenBy(saof => saof.SpecificationAttributeOptionName).ToList(); //prepare the model properties Enabled = true; var removeFilterUrl = webHelper.ModifyQueryString(webHelper.GetThisPageUrl(true), QUERYSTRINGPARAM, null); RemoveFilterUrl = ExcludeQueryStringParams(removeFilterUrl, webHelper); //get already filtered specification options var alreadyFilteredOptions = allFilters.Where(x => alreadyFilteredSpecOptionIds.Contains(x.SpecificationAttributeOptionId)); AlreadyFilteredItems = alreadyFilteredOptions.Select(x => { var filterUrl = webHelper.ModifyQueryString(webHelper.GetThisPageUrl(true), QUERYSTRINGPARAM, GenerateFilteredSpecQueryParam(alreadyFilteredOptions.Where(y => y.SpecificationAttributeOptionId != x.SpecificationAttributeOptionId).Select(z => z.SpecificationAttributeOptionId).ToList())); return(new SpecificationFilterItem { SpecificationAttributeName = x.SpecificationAttributeName, SpecificationAttributeOptionName = x.SpecificationAttributeOptionName, SpecificationAttributeOptionColorRgb = x.SpecificationAttributeOptionColorRgb, FilterUrl = ExcludeQueryStringParams(filterUrl, webHelper) }); }).ToList(); //get not filtered specification options NotFilteredItems = allFilters.Except(alreadyFilteredOptions).Select(x => { //filter URL var alreadyFiltered = alreadyFilteredSpecOptionIds.Concat(new List <string> { x.SpecificationAttributeOptionId }); var filterUrl = webHelper.ModifyQueryString(webHelper.GetThisPageUrl(true), QUERYSTRINGPARAM, GenerateFilteredSpecQueryParam(alreadyFiltered.ToList())); return(new SpecificationFilterItem() { SpecificationAttributeName = x.SpecificationAttributeName, SpecificationAttributeOptionName = x.SpecificationAttributeOptionName, SpecificationAttributeOptionColorRgb = x.SpecificationAttributeOptionColorRgb, FilterUrl = ExcludeQueryStringParams(filterUrl, webHelper) }); }).ToList(); }
public virtual async Task PrepareSpecsFilters(IList <string> alreadyFilteredSpecOptionIds, IList <string> filterableSpecificationAttributeOptionIds, ISpecificationAttributeService specificationAttributeService, IHttpContextAccessor httpContextAccessor, ICacheBase cacheBase, string langId) { Enabled = false; var allFilters = new List <SpecificationAttributeOptionFilter>(); foreach (var sao in filterableSpecificationAttributeOptionIds.Union(alreadyFilteredSpecOptionIds)) { var sa = await specificationAttributeService.GetSpecificationAttributeByOptionId(sao); if (sa != null) { allFilters.Add(new SpecificationAttributeOptionFilter { SpecificationAttributeId = sa.Id, SpecificationAttributeName = sa.GetTranslation(x => x.Name, langId), SpecificationAttributeSeName = sa.SeName, SpecificationAttributeDisplayOrder = sa.DisplayOrder, SpecificationAttributeOptionId = sao, SpecificationAttributeOptionName = sa.SpecificationAttributeOptions.FirstOrDefault(x => x.Id == sao).GetTranslation(x => x.Name, langId), SpecificationAttributeOptionSeName = sa.SpecificationAttributeOptions.FirstOrDefault(x => x.Id == sao).SeName, SpecificationAttributeOptionDisplayOrder = sa.SpecificationAttributeOptions.FirstOrDefault(x => x.Id == sao).DisplayOrder, SpecificationAttributeOptionColorRgb = sa.SpecificationAttributeOptions.FirstOrDefault(x => x.Id == sao).ColorSquaresRgb, }); } } if (!allFilters.Any()) { return; } //sort loaded options allFilters = allFilters.OrderBy(saof => saof.SpecificationAttributeDisplayOrder) .ThenBy(saof => saof.SpecificationAttributeName) .ThenBy(saof => saof.SpecificationAttributeOptionDisplayOrder) .ThenBy(saof => saof.SpecificationAttributeOptionName).ToList(); //prepare the model properties Enabled = true; string removeFilterUrl = httpContextAccessor.HttpContext.Request.GetDisplayUrl(); foreach (var item in allFilters.GroupBy(x => x.SpecificationAttributeSeName)) { removeFilterUrl = CommonExtensions.ModifyQueryString(removeFilterUrl, item.Key, null); } RemoveFilterUrl = ExcludeQueryStringParams(removeFilterUrl); //get already filtered specification options var alreadyFilteredOptions = allFilters.Where(x => alreadyFilteredSpecOptionIds.Contains(x.SpecificationAttributeOptionId)); AlreadyFilteredItems = alreadyFilteredOptions.Select(x => { var alreadyFiltered = alreadyFilteredOptions.Where(y => y.SpecificationAttributeId == x.SpecificationAttributeId).Select(z => z.SpecificationAttributeOptionSeName) .Except(new List <string> { x.SpecificationAttributeOptionSeName }).ToList(); var filterUrl = CommonExtensions.ModifyQueryString(httpContextAccessor.HttpContext.Request.GetDisplayUrl(), x.SpecificationAttributeSeName, GenerateFilteredSpecQueryParam(alreadyFiltered)); return(new SpecificationFilterItem { SpecificationAttributeName = x.SpecificationAttributeName, SpecificationAttributeSeName = x.SpecificationAttributeSeName, SpecificationAttributeOptionName = x.SpecificationAttributeOptionName, SpecificationAttributeOptionSeName = x.SpecificationAttributeOptionSeName, SpecificationAttributeOptionColorRgb = x.SpecificationAttributeOptionColorRgb, FilterUrl = ExcludeQueryStringParams(filterUrl) }); }).ToList(); //get not filtered specification options NotFilteredItems = allFilters.Except(alreadyFilteredOptions).Select(x => { //filter URL var alreadyFiltered = alreadyFilteredOptions.Where(y => y.SpecificationAttributeId == x.SpecificationAttributeId).Select(x => x.SpecificationAttributeOptionSeName) .Concat(new List <string> { x.SpecificationAttributeOptionSeName }); var filterUrl = CommonExtensions.ModifyQueryString(httpContextAccessor.HttpContext.Request.GetDisplayUrl(), x.SpecificationAttributeSeName, GenerateFilteredSpecQueryParam(alreadyFiltered.ToList())); return(new SpecificationFilterItem() { SpecificationAttributeName = x.SpecificationAttributeName, SpecificationAttributeSeName = x.SpecificationAttributeSeName, SpecificationAttributeOptionName = x.SpecificationAttributeOptionName, SpecificationAttributeOptionSeName = x.SpecificationAttributeOptionSeName, SpecificationAttributeOptionColorRgb = x.SpecificationAttributeOptionColorRgb, FilterUrl = ExcludeQueryStringParams(filterUrl) }); }).ToList(); }