示例#1
0
        public ActionResult Category(String name, string filterText = "", string sortOrder = "", int page = 1, int pageSize = 36)
        {
            var model = new CategoryViewModel();

            try
            {
                model.FilterText = filterText.Trim();
                model.SortOrder  = sortOrder;
                using (var db = new DBContext())
                {
                    // Get category id
                    var categoryId = ParseId(name);

                    // Get category info
                    var category = db.Categories.Where(r => r.CategoryId == categoryId).FirstOrDefault();
                    if (category != null)
                    {
                        // Set meta data
                        ViewBag.MetaDescription = "Memory Audio - " + category.CategoryName;
                        ViewBag.MetaKeywords    = category.CategoryName + ",memory-audio,audiophile,sound,hifi,stereo,hi-end,hd,ultra-hd,dts,dts-hd";

                        // Query products
                        var query = from p in db.Products
                                    join c in db.Categories on p.CategoryId equals c.CategoryId into pc
                                    join b in db.Brands on p.BrandId equals b.BrandId into pb
                                    from j1 in pc.DefaultIfEmpty()
                                    from j2 in pb.DefaultIfEmpty()
                                    where p.Display > 1
                                    select new ProductInfo
                        {
                            ProductId     = p.ProductId,
                            ProductName   = p.ProductName,
                            CategoryId    = p.CategoryId,
                            CategoryName  = j1.CategoryName,
                            BrandId       = p.BrandId,
                            BrandName     = j2.BrandName,
                            Specification = p.Specification,
                            TotalInStock  = p.TotalInStock,
                            Price         = p.Price,
                            Discount      = p.Discount,
                            Image1        = p.Image1,
                            Image2        = p.Image2,
                            Image3        = p.Image3,
                            Image4        = p.Image4,
                            Image5        = p.Image5,
                            Image6        = p.Image6,
                            CreationDate  = p.CreationDate,
                            Display       = p.Display,
                            SortIdx       = p.SortIdx
                        };

                        // Get category nodes include its children and parent
                        var categoryNode = new CategoryTreeNode();
                        categoryNode.CategoryId   = category.CategoryId;
                        categoryNode.CategoryName = category.CategoryName;
                        categoryNode.Description  = category.Description;
                        categoryNode.Level        = 0;
                        categoryNode.Parent       = null;
                        categoryNode.Nodes        = new List <CategoryTreeNode>();
                        CategoryTree.AppendChildNodes(categoryNode);
                        CategoryTree.AppendParentNodes(categoryNode);

                        // Filter products by category which included its children
                        var childNodes       = categoryNode.GetChildNodes();
                        var childCategoryIds = new List <int>();
                        foreach (var node in childNodes)
                        {
                            childCategoryIds.Add(node.CategoryId);
                        }
                        if (childCategoryIds.Count > 0)
                        {
                            query = query.Where(r => childCategoryIds.Contains(r.CategoryId ?? 0));
                        }
                        if (!string.IsNullOrWhiteSpace(filterText))
                        {
                            query = query.Where(r => r.ProductName.Contains(filterText) || r.CategoryName.Contains(filterText));
                        }

                        // Get parent nodes
                        var parentNodes = categoryNode.GetParentNodes();
                        foreach (var parentNode in parentNodes)
                        {
                            var parentCategory = db.Categories.Where(r => r.CategoryId == parentNode.CategoryId).FirstOrDefault();
                            if (parentCategory != null && parentCategory.CategoryId != category.CategoryId)
                            {
                                model.ParentCategories.Add(parentCategory);
                            }
                        }

                        // Sorting
                        switch (sortOrder)
                        {
                        case "price":
                            query = query.OrderBy(p => p.Price);
                            break;

                        case "price_desc":
                            query = query.OrderByDescending(p => p.Price);
                            break;

                        case "name":
                            query = query.OrderBy(p => p.ProductName);
                            break;

                        case "name_desc":
                            query = query.OrderByDescending(p => p.ProductName);
                            break;

                        default:
                            query = query.OrderByDescending(p => p.SortIdx);
                            break;
                        }
                        var products  = query.ToList();
                        var pageCount = (products.Count / pageSize) + (products.Count % pageSize > 0 ? 1 : 0);
                        if (page > pageCount)
                        {
                            page = pageCount;
                        }
                        model.Products = query.ToPagedList <ProductInfo>(page == 0 ? 1 : page, pageSize);
                    }
                    else
                    {
                        throw new Exception("Category not found!");
                    }

                    model.Category  = category;
                    model.PageIndex = model.Products.PageNumber;
                    model.PageSize  = model.Products.PageSize;

                    return(View(model));
                }
            }
            catch (Exception ex)
            {
                // Write event logs
                EventLogs.Write("HomeController - Category: " + ex.ToString(), EventLogEntryType.Error);

                // Redirect to error page
                return(RedirectToAction("Index", "Error"));
            }
        }
示例#2
0
        public ActionResult Product(string name)
        {
            var model = new ProductViewModel();

            try
            {
                using (var db = new DBContext())
                {
                    // Get ProductId
                    var productId = ParseId(name);

                    // Get product info
                    var productInfo = (from p in db.Products
                                       join c in db.Categories on p.CategoryId equals c.CategoryId into pc
                                       join b in db.Brands on p.BrandId equals b.BrandId into pb
                                       from j1 in pc.DefaultIfEmpty()
                                       from j2 in pb.DefaultIfEmpty()
                                       where p.ProductId == productId && p.Display > 1
                                       select new ProductInfo
                    {
                        ProductId = p.ProductId,
                        ProductName = p.ProductName,
                        CategoryId = p.CategoryId,
                        CategoryName = j1.CategoryName,
                        BrandId = p.BrandId,
                        BrandName = j2.BrandName,
                        Specification = p.Specification,
                        TotalInStock = p.TotalInStock,
                        Price = p.Price,
                        Discount = p.Discount,
                        MSRP = p.MSRP,
                        Image1 = p.Image1,
                        Image2 = p.Image2,
                        Image3 = p.Image3,
                        Image4 = p.Image4,
                        Image5 = p.Image5,
                        Image6 = p.Image6,
                        CreationDate = p.CreationDate,
                        Display = p.Display,
                        SortIdx = p.SortIdx
                    }).FirstOrDefault();

                    // Product existed?
                    if (productInfo == null)
                    {
                        return(RedirectToAction("Index", "Error", new { @message = "Product not found!" }));
                    }

                    // Get related products
                    var relatedProducts = from p in db.Products
                                          join c in db.Categories on p.CategoryId equals c.CategoryId into pc
                                          join b in db.Brands on p.BrandId equals b.BrandId into pb
                                          from j1 in pc.DefaultIfEmpty()
                                          from j2 in pb.DefaultIfEmpty()
                                          where p.Display > 1 && p.ProductId != productInfo.ProductId && p.CategoryId == productInfo.CategoryId
                                          orderby p.Display descending, p.SortIdx ascending
                        select new ProductInfo
                    {
                        ProductId     = p.ProductId,
                        ProductName   = p.ProductName,
                        CategoryId    = p.CategoryId,
                        CategoryName  = j1.CategoryName,
                        BrandId       = p.BrandId,
                        BrandName     = j2.BrandName,
                        Specification = p.Specification,
                        TotalInStock  = p.TotalInStock,
                        Price         = p.Price,
                        Discount      = p.Discount,
                        Image1        = p.Image1,
                        Image2        = p.Image2,
                        Image3        = p.Image3,
                        Image4        = p.Image4,
                        Image5        = p.Image5,
                        Image6        = p.Image6,
                        CreationDate  = p.CreationDate,
                        Display       = p.Display,
                        SortIdx       = p.SortIdx
                    };

                    // Get category info and its parent
                    var category = db.Categories.Where(r => r.CategoryId == productInfo.CategoryId).FirstOrDefault();
                    if (category != null)
                    {
                        var categoryNode = new CategoryTreeNode();
                        categoryNode.CategoryId   = category.CategoryId;
                        categoryNode.CategoryName = category.CategoryName;
                        categoryNode.Description  = category.Description;
                        categoryNode.Level        = 0;
                        categoryNode.Parent       = null;
                        categoryNode.Nodes        = new List <CategoryTreeNode>();
                        CategoryTree.AppendParentNodes(categoryNode);

                        var parentNodes = categoryNode.GetParentNodes();
                        foreach (var parentNode in parentNodes)
                        {
                            var parentCategory = db.Categories.Where(r => r.CategoryId == parentNode.CategoryId).FirstOrDefault();
                            if (parentCategory != null && parentCategory.CategoryId != category.CategoryId)
                            {
                                model.ParentCategories.Add(parentCategory);
                            }
                        }
                        model.Category = category;
                    }
                    model.Product         = productInfo;
                    model.RelatedProducts = relatedProducts.Take(AppSettings.PAGE_SIZE).ToList();

                    // Set meta data
                    ViewBag.MetaDescription = "Memory Audio - " + productInfo.ProductName;
                    ViewBag.MetaKeywords    =
                        productInfo.CategoryName + "," +
                        productInfo.ProductName + "," +
                        productInfo.BrandName + ",memory audio,audiophile,sound,hifi,stereo,hi-end,hd,ultra-hd,dts,dts-hd";

                    return(View(model));
                }
            }
            catch (Exception ex)
            {
                // Write event logs
                EventLogs.Write("HomeController - Products: " + ex.ToString(), EventLogEntryType.Error);

                // Redirect to error page
                return(RedirectToAction("Index", "Error"));
            }
        }