Exemplo n.º 1
0
        public IEnumerable<ProductDetails> Search(string zipCode, string searchText, SortBy orderBy, bool isAsc)
        {
            if (!Enum.IsDefined(typeof(MVCMobileDetect.Product.SortBy), orderBy))
                throw new MVCMobileDetect.Exceptions.MDException(string.Format("SortBy not found for the Product:: Enum value:{0} is not defined in MVCMobileDetect.Product.SortBy", orderBy));
            #if DEBUG
            MVCMobileDetect.Common.LogHelper lh = new MVCMobileDetect.Common.LogHelper();
            #endif
            IEnumerable<ProductDetails> sortedProducts = null;
            IQueryable<ProductDetails> products = null;
            if (SortBy.TopSeller == orderBy)
            {
                if (isAsc)
                {
                    products = (from productClass in DataContext.GCProductClasses
                                join product in DataContext.GCProducts on productClass.Class equals product.ClassId
                                join marketPrice in DataContext.GCMarketPrices on product.ProductId equals marketPrice.ProductId
                                join marketZip in DataContext.GCMarketZips on marketPrice.MarketId equals marketZip.MarketId
                                join topSeller in DataContext.GCProductTop50s on product.SKU equals topSeller.SKU

                                where
                                  ((marketZip.ZipCode == zipCode && product.IsActive == true && topSeller.MarketId == marketZip.MarketId && topSeller.ClassId == product.ClassId) &&
                                  (productClass.Description.Contains(searchText) ||
                                  product.Brand.Contains(searchText) ||
                                  product.ShortDescription.Contains(searchText) ||
                                  product.Keywords.Contains(searchText) ||
                                  product.Category.Contains(searchText) ||
                                  product.SKU.Contains(searchText))
                                  && product.Brand != null && product.ShortDescription != null)

                                orderby topSeller.Ranking ascending
                                select new MVCMobileDetect.Product.ProductDetails
                                {
                                    ProductID = product.ProductId.ToString(),
                                    Brand = (product.Brand == "NULL" ? null : product.Brand.Trim()),
                                    Name = product.ShortDescription.Trim(),
                                    RegularPrice = String.Format("{0:0.00}", marketPrice.MarketPrice),
                                    ThumbnailURL = product.SmallImage,
                                    Rating = GetRandom(product.ProductId, 1, 5),
                                    RatingCount = GetRandom(product.ProductId, 1, 100),
                                    Offer = string.Format("${0} each for 2 or more", String.Format("{0:0.00}", marketPrice.MarketPrice*0.75)),
                                    SKU = product.SKU,
                                    DateUpdated = (DateTime)product.UpdateDate
                                }).Distinct();
                }
                else
                {
                    products = (from productClass in DataContext.GCProductClasses
                                join product in DataContext.GCProducts on productClass.Class equals product.ClassId
                                join marketPrice in DataContext.GCMarketPrices on product.ProductId equals marketPrice.ProductId
                                join marketZip in DataContext.GCMarketZips on marketPrice.MarketId equals marketZip.MarketId
                                join topSeller in DataContext.GCProductTop50s on product.SKU equals topSeller.SKU

                                where
                                  ((marketZip.ZipCode == zipCode && product.IsActive == true && topSeller.MarketId == marketZip.MarketId && topSeller.ClassId == product.ClassId) &&
                                  (productClass.Description.Contains(searchText) ||
                                  product.Brand.Contains(searchText) ||
                                  product.ShortDescription.Contains(searchText) ||
                                  product.Keywords.Contains(searchText) ||
                                  product.Category.Contains(searchText) ||
                                  product.SKU.Contains(searchText))
                                  && product.Brand != null && product.ShortDescription != null)

                                orderby topSeller.Ranking descending
                                select new MVCMobileDetect.Product.ProductDetails
                                {
                                    ProductID = product.ProductId.ToString(),
                                    Brand = (product.Brand == "NULL" ? null : product.Brand.Trim()),
                                    Name = product.ShortDescription.Trim(),
                                    RegularPrice = String.Format("{0:0.00}", marketPrice.MarketPrice),
                                    ThumbnailURL = product.SmallImage,
                                    Rating = GetRandom(product.ProductId, 1, 5),
                                    RatingCount = GetRandom(product.ProductId, 1, 100),
                                    Offer = string.Format("${0} each for 2 or more", String.Format("{0:0.00}", marketPrice.MarketPrice*0.75)),
                                    SKU = product.SKU,
                                    DateUpdated = (DateTime)product.UpdateDate
                                }).Distinct();
                }
            }
            else
            {
                products = (from productClass in DataContext.GCProductClasses
                            join product in DataContext.GCProducts on productClass.Class equals product.ClassId
                            join marketPrice in DataContext.GCMarketPrices on product.ProductId equals marketPrice.ProductId
                            join marketZip in DataContext.GCMarketZips on marketPrice.MarketId equals marketZip.MarketId

                            where
                              ((marketZip.ZipCode == zipCode && product.IsActive == true) &&
                              (productClass.Description.Contains(searchText) ||
                              product.Brand.Contains(searchText) ||
                              product.ShortDescription.Contains(searchText) ||
                              product.Keywords.Contains(searchText) ||
                              product.Category.Contains(searchText) ||
                              product.SKU.Contains(searchText))
                              && product.Brand != null && product.ShortDescription != null)
                            select new MVCMobileDetect.Product.ProductDetails
                            {
                                ProductID = product.ProductId.ToString(),
                                Brand = (product.Brand == "NULL" ? null : product.Brand.Trim()),
                                Name = product.ShortDescription.Trim(),
                                RegularPrice = String.Format("{0:0.00}", marketPrice.MarketPrice),
                                ThumbnailURL = product.SmallImage,
                                Rating = GetRandom(product.ProductId, 1, 5),
                                RatingCount = GetRandom(product.ProductId, 1, 100),
                                Offer = string.Format("${0} each for 2 or more", String.Format("{0:0.00}", marketPrice.MarketPrice*0.75)),
                                SKU = product.SKU,
                                DateUpdated = (DateTime)product.UpdateDate
                            }).Distinct();
            }

            switch (orderBy)
            {
                case SortBy.Product:
                    sortedProducts = (isAsc == true ? products.ToList().OrderBy(a => a.Name) : products.ToList().OrderByDescending(a => a.Name));
                    break;

                case SortBy.Brand:
                    sortedProducts = (isAsc == true ? products.ToList().OrderBy(a => a.Brand) : products.ToList().OrderByDescending(a => a.Brand));
                    break;

                case SortBy.Price:
                    sortedProducts = (isAsc == true ? products.ToList().OrderBy(a => decimal.Parse(a.RegularPrice)) : products.ToList().OrderByDescending(a => decimal.Parse(a.RegularPrice)));
                    break;

                case SortBy.Rated:
                    sortedProducts = (isAsc == true ? products.ToList().OrderBy(a => a.RatingCount) : products.ToList().OrderByDescending(a => a.RatingCount));
                    break;

                case SortBy.Newest:
                    sortedProducts = (isAsc == true ? products.ToList().OrderBy(a => a.DateUpdated) : products.ToList().OrderByDescending(a => a.DateUpdated));
                    break;

                default:
                    sortedProducts = products.ToList().OrderBy(a => a.Name);
                    break;
            }
            #if DEBUG
            lh.StopWrite(_Logger, "Product.Search took>>>>> {0}.");
            #endif
            return sortedProducts;
        }
Exemplo n.º 2
0
        public IEnumerable<ProductsCategory> GetProductsByCategory(int catID, string zipCode, SortBy orderBy, bool isAsc)
        {
            #if DEBUG
            MVCMobileDetect.Common.LogHelper lh = new MVCMobileDetect.Common.LogHelper();
            #endif
            IEnumerable<ProductsCategory> sortedProducts=null;
            IQueryable<ProductsCategory> products = null;
            if (SortBy.TopSeller == orderBy)
            {
                products = from product in DataContext.GCProducts
                           join marketPrice in DataContext.GCMarketPrices on product.ProductId equals marketPrice.ProductId
                           join marketZip in DataContext.GCMarketZips on marketPrice.MarketId equals marketZip.MarketId
                           join topSeller in DataContext.GCProductTop50s on product.SKU equals topSeller.SKU
                           where (marketZip.ZipCode == zipCode && product.IsActive == true && product.ClassId == catID
                                && topSeller.MarketId == marketZip.MarketId && topSeller.ClassId == catID
                                && product.Brand != null && product.ShortDescription != null)

                           select new ProductsCategory
                           {
                               ProductID = product.ProductId,
                               Brand = (product.Brand == "NULL" ? null : product.Brand.Trim()),
                               Name = product.ShortDescription.Trim(),
                               RegularPrice = marketPrice.MarketPrice,
                               SalePrice = marketPrice.MarketPrice-1,
                               ThumbnailURL = product.SmallImage,
                               Ranking = topSeller.Ranking,
                               Rating = GetRandom(product.ProductId, 1, 5),
                               RatingCount = GetRandom(product.ProductId, 1, 100)
                           };

                sortedProducts = (isAsc == true ? products.ToList().OrderBy(a => a.Ranking) : products.ToList().OrderByDescending(a => a.Ranking));
            }
            else
            {
                products = from marketPrice in DataContext.GCMarketPrices
                           join product in DataContext.GCProducts on marketPrice.ProductId equals product.ProductId
                           join marketZip in DataContext.GCMarketZips on marketPrice.MarketId equals marketZip.MarketId
                           where (marketZip.ZipCode == zipCode && product.IsActive == true && product.ClassId == catID
                                    && product.Brand != null && product.ShortDescription != null)

                           select new ProductsCategory
                           {
                               ProductID = product.ProductId,
                               Brand = (product.Brand == "NULL" ? null : product.Brand.Trim()),
                               Name = product.ShortDescription.Trim(),
                               RegularPrice = marketPrice.MarketPrice,
                               SalePrice = marketPrice.MarketPrice - 1,
                               ThumbnailURL = product.SmallImage,
                               Rating = new Random(product.ProductId + (int)DateTime.Now.Ticks).Next(1, 5),
                               RatingCount =  new Random(product.ProductId + (int)DateTime.Now.Ticks).Next(1, 100),
                               DateCreated = product.CreateDate.Value.ToShortDateString()
                           };

                switch (orderBy)
                {
                    case SortBy.Product:
                        sortedProducts = (isAsc == true ? products.ToList().OrderBy(a => a.Name) : products.ToList().OrderByDescending(a => a.Name));
                        break;

                    case SortBy.Brand:
                        sortedProducts = (isAsc == true ? products.ToList().OrderBy(a => a.Brand) : products.ToList().OrderByDescending(a => a.Brand));
                        break;

                    case SortBy.Price:
                        sortedProducts = (isAsc == true ? products.ToList().OrderBy(a => a.RegularPrice) : products.ToList().OrderByDescending(a => a.RegularPrice));
                        break;

                    case SortBy.Newest:
                        sortedProducts = (isAsc == true ? products.ToList().OrderBy(a => a.DateCreated) : products.ToList().OrderByDescending(a => a.DateCreated));
                        break;

                    case SortBy.Rated:
                        sortedProducts = (isAsc == true ? products.ToList().OrderBy(a => a.RatingCount) : products.ToList().OrderByDescending(a => a.RatingCount));
                        break;

                    default:
                        sortedProducts = products.ToList().OrderBy(a => a.Name);
                        break;

                }
            }

            #if DEBUG
            lh.StopWrite(_Logger, "Product.GetProductsByCategory took>>>>> {0}.");
            #endif
            return sortedProducts;
            //return products.ToList();
        }