public IPagedList <Product> Search(ProductAdminSearchQuery query) { Product productAlias = null; var queryOver = _session.QueryOver(() => productAlias); switch (query.PublishStatus) { case PublishStatus.Unpublished: queryOver = queryOver.Where(product => product.PublishOn == null || product.PublishOn > CurrentRequestData.Now); break; case PublishStatus.Published: queryOver = queryOver.Where(product => product.PublishOn != null && product.PublishOn <= CurrentRequestData.Now); break; } if (!string.IsNullOrWhiteSpace(query.Brand)) { Brand brandAlias = null; queryOver = queryOver.JoinAlias(product => product.BrandPage, () => brandAlias) .Where(() => brandAlias.Name.IsInsensitiveLike(query.Brand, MatchMode.Anywhere)); } if (!string.IsNullOrWhiteSpace(query.CategoryName)) { Product categoryProductAlias = null; queryOver = queryOver.WithSubquery.WhereExists(QueryOver.Of <Category>() .JoinAlias(category => category.Products, () => categoryProductAlias) .Where(x => x.Name.IsInsensitiveLike(query.CategoryName, MatchMode.Anywhere) && categoryProductAlias.Id == productAlias.Id) .Select(x => x.Id)); } if (!string.IsNullOrWhiteSpace(query.SKU)) { queryOver = queryOver.WithSubquery.WhereExists(QueryOver.Of <ProductVariant>() .Where(x => x.Product.Id == productAlias.Id && x.SKU.IsInsensitiveLike(query.SKU, MatchMode.Anywhere)) .Select(x => x.Id)); } if (!string.IsNullOrWhiteSpace(query.Name)) { queryOver = queryOver.Where(x => x.Name.IsInsensitiveLike(query.Name, MatchMode.Anywhere)); } var min = query.PriceFrom ?? 0; var max = query.PriceTo ?? int.MaxValue; queryOver = queryOver.WithSubquery.WhereExists(QueryOver.Of <ProductVariant>() .Where(x => x.Product.Id == productAlias.Id && x.BasePrice >= min && x.BasePrice <= max) .Select(x => x.Id)); return(queryOver.OrderBy(product => product.Name).Asc.Paged(query.Page)); }
private void SetBrand(ProductImportDataTransferObject dataTransferObject, Product product) { //Brand if (!String.IsNullOrWhiteSpace(dataTransferObject.Brand)) { string dtoBrand = dataTransferObject.Brand.Trim(); Brand brand = _session.QueryOver <Brand>() .Where(b => b.Name.IsInsensitiveLike(dtoBrand, MatchMode.Exact)) .Take(1) .SingleOrDefault(); if (brand == null) { brand = _getNewBrandPage.Get(dtoBrand); _session.Transact(session => session.Save(brand)); } product.BrandPage = brand; } }
public string ProcessBrands(NopCommerceDataReader dataReader, NopImportContext nopImportContext) { var brandDatas = dataReader.GetBrands(); foreach (BrandData brandData in brandDatas) { string name = brandData.Name.Trim(); Brand brand = _session.QueryOver <Brand>() .Where(b => b.Name.IsInsensitiveLike(name, MatchMode.Exact)) .List().FirstOrDefault(); if (brand == null) { brand = _getNewBrandPage.Get(name); _session.Transact(session => session.Save(brand)); } nopImportContext.AddEntry(brandData.Id, brand); } return(string.Format("{0} brands processed", brandDatas.Count)); }
public void ImportProductsService_ImportProducts_ShouldSetProductBrandIfItAlreadyExists() { var product = new ProductImportDataTransferObject { UrlSegment = "test-url", Brand = "Test Brand" }; var brand = new Brand {Name = "Test BrandPage"}; Product importProduct = _importProductsService.ImportProduct(product); importProduct.BrandPage.Should().Be(brand); }