public ActionResult ShowCategory(int subCatId)
        {
            var dataModel = new SubCategoryViewModel();

            try
            {
                var subCategory = Db.SubCategories.FirstOrDefault();
                var Products    = Db.Products.Where(x => x.SubCategoryId == subCatId).OrderBy(x => x.Name).ToList();
                var list        = new List <SubCategoryItems>();
                foreach (var items in Products)
                {
                    var image = GetProductImageAndColors(items.Id);
                    var data  = new SubCategoryItems
                    {
                        DateAdded        = items.DateAdded.GetValueOrDefault(),
                        SubCategoryId    = items.Id,
                        Name             = items.Name,
                        ImagePath        = image.Count() > 0 ? image.FirstOrDefault()?.ImagePath : "//placehold.it/150x60",
                        ProductUniqueKey = items.UniqueId.GetValueOrDefault()
                    };
                    list.Add(data);
                }
                dataModel.Title = Globals.Category.Women_Ethnic_Wear.DisplayName();
                dataModel.SubcategoryItemList = list;
            }
            catch (Exception e)
            {
                Log.Debug(e);
            }
            return(View(dataModel));
        }
Ejemplo n.º 2
0
        public ActionResult DeleteCategory(int categoryId)
        {
            Category getCategoryToDelete = ApplicationDbContext.Categories.First(i => i.CategoryId == categoryId);

            List <Item> getItemsToDelete = ApplicationDbContext.Items.Where(i => i.CategoryId == categoryId).ToList();

            List <SubCategory> getSubcategoriesToDelete = ApplicationDbContext.SubCategories.Where(i => i.CategoryId == categoryId).ToList();

            //Remove Category Items from junction table subcategoryItems
            if (getItemsToDelete.Count > 0)
            {
                try
                {
                    foreach (var item in getItemsToDelete)
                    {
                        SubCategoryItems subcatItem = ApplicationDbContext.SubCategoryItems.First(i => i.ItemId == item.ItemId);

                        ApplicationDbContext.SubCategoryItems.Remove(subcatItem);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }
            try
            {
                ApplicationDbContext.Categories.Remove(getCategoryToDelete);

                if (getSubcategoriesToDelete.Count > 0)
                {
                    foreach (var subcat in getSubcategoriesToDelete)
                    {
                        ApplicationDbContext.SubCategories.Remove(subcat);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

            ApplicationDbContext.SaveChanges();

            return(RedirectToAction("SelectCategoryToDelete"));
        }
        public ActionResult ProductGallery(int catId)
        {
            //var catIdValue= Convert.ToInt32(EncryptDecrypt.Decrypt(key,""));
            var dataModel = new SubCategoryViewModel();

            try
            {
                var products    = Db.Products.Where(x => x.CategoryId == catId).OrderBy(x => x.Name).ToList();
                var subCategory = Db.SubCategories.FirstOrDefault(x => x.CategoryId == catId);
                dataModel.Title = !ReferenceEquals(subCategory, null) ? Db.Categories.FirstOrDefault(x => x.CategoryId == subCategory.CategoryId).CategoryName : "";
                if (products.Count() > 0)
                {
                    var distinctProducts = products.Select(x => new { x.Id, x.Type }).Distinct().ToList();
                    var list             = new List <SubCategoryItems>();
                    foreach (var item in distinctProducts)
                    {
                        var product = Db.Products.FirstOrDefault(x => x.Id == item.Id);

                        if (!ReferenceEquals(product, null))
                        {
                            var image = GetProductImageAndColors(product.Id);
                            var data  = new SubCategoryItems
                            {
                                DateAdded        = product.DateAdded.GetValueOrDefault(),
                                SubCategoryId    = product.SubCategoryId,
                                ProductId        = product.Id,
                                Name             = product.Name,
                                ProductUniqueKey = product.UniqueId.GetValueOrDefault(),
                                ImagePath        = image.Count() > 0? image.FirstOrDefault()?.ImagePath :"//placehold.it/150x60"
                            };
                            list.Add(data);
                        }
                    }
                    dataModel.SubcategoryItemList = list;
                }
            }
            catch (Exception e)
            {
                Log.Debug(e);
            }
            return(View(dataModel));
        }
Ejemplo n.º 4
0
        public void OnGet(string productid)
        {
            product = _context.Products.Include(a => a.Category).Where(x => x.Id == new Guid(productid)).FirstOrDefault();

            _p = product;


            foreach (var mainCat in MainCategories)
            {
                MainCategoryItems.Add(new SelectListItem(mainCat.Name, mainCat.Id.ToString()));   // id visas inte
            }

            foreach (var mainCat in SubCategories)
            {
                SubCategoryItems.Add(new SelectListItem(mainCat.Name, mainCat.Id.ToString()));          // men Pc har ingen console
            }

            subSelectedCategoryItemId  = product.Category.SubCategory.Id.ToString();
            mainSelectedCategoryItemId = product.Category.MainCategory.Id.ToString();

            subId  = subSelectedCategoryItemId;
            mainId = mainSelectedCategoryItemId;
        }
Ejemplo n.º 5
0
        public ActionResult CreateItem(CreateItemViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View("CreateItem", viewModel));
            }

            //gets the CategoryId
            int getCatId = ApplicationDbContext.Categories
                           .Where(i => i.CategoryName == viewModel.CategoryName)
                           .Select(i => i.CategoryId).FirstOrDefault();

            //First builds the Item object and adds it to the db. afterward the new items id is available to add to the junction table.
            Item newItem = new Item
            {
                Name           = viewModel.Name,
                Description    = viewModel.Description,
                Price          = viewModel.Price,
                CategoryId     = getCatId,
                ImageReference = viewModel.ImageReference,
                DateAdded      = DateTime.Now
            };

            ApplicationDbContext.Items.Add(newItem);
            ApplicationDbContext.SaveChanges();



            //Junction table for SubcategoryItems
            if (viewModel.SubCategories != null)
            {
                foreach (string sub in viewModel.SubCategories)
                {
                    int getSubId = ApplicationDbContext.SubCategories
                                   .Where(i => i.SubCategoryName == sub)
                                   .Select(i => i.SubCategoryId)
                                   .FirstOrDefault();

                    int getItemId = ApplicationDbContext.Items
                                    .Where(i => i.Name == viewModel.Name)
                                    .Select(i => i.ItemId).FirstOrDefault();

                    Item getItem = ApplicationDbContext.Items
                                   .FirstOrDefault(i => i.ItemId == getItemId);

                    if (getItem != null)
                    {
                        SubCategoryItems subCatItems = new SubCategoryItems
                        {
                            SubcategoryId = getSubId,
                            ItemId        = getItemId
                        };

                        ApplicationDbContext.SubCategoryItems.Add(subCatItems);
                        ApplicationDbContext.SaveChanges();
                    }
                }
            }

            ModelState.Clear();

            //Pass subcategories to view
            ViewBag.subcategories = ApplicationDbContext.SubCategories.Select(i => i.SubCategoryName);

            return(View());
        }
Ejemplo n.º 6
0
        public ActionResult UpdateItem(UpdateItemViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(viewModel));
            }

            Item itemToUpdate = ApplicationDbContext.Items.First(i => i.ItemId == viewModel.ItemId);

            itemToUpdate.Name           = viewModel.Name;
            itemToUpdate.Description    = viewModel.Description;
            itemToUpdate.Price          = viewModel.Price;
            itemToUpdate.ImageReference = viewModel.ImageReference;

            List <int> itemSubcategory = ApplicationDbContext.SubCategoryItems.Where(i => i.ItemId == viewModel.ItemId).Select(i => i.SubcategoryId).ToList();

            //Check for new subcategory to add to junction table. If sub count is 0 then all check will be added. if itemSubcategory does not contain the subcategoryId (sub) then it will be added
            //Add subcategories
            if (viewModel.Subcategories != null)
            {
                foreach (int sub in viewModel.Subcategories)
                {
                    if (itemSubcategory.Count < 1 || !itemSubcategory.Contains(sub))
                    {
                        SubCategoryItems newSubcatItem = new SubCategoryItems
                        {
                            SubcategoryId = sub,
                            ItemId        = viewModel.ItemId
                        };
                        ApplicationDbContext.SubCategoryItems.Add(newSubcatItem);
                    }
                }
            }

            //Check to see if subcategory was uncheck and will be removed.
            //remove subcategories
            if (viewModel.Subcategories == null)
            {
                var removeAll = ApplicationDbContext.SubCategoryItems
                                .Where(i => i.ItemId == viewModel.ItemId);
                ApplicationDbContext.SubCategoryItems
                .RemoveRange(removeAll);
            }
            else
            {
                foreach (var itemSub in itemSubcategory)
                {
                    if (!viewModel.Subcategories.Contains(itemSub))
                    {
                        //remove unchecked subcategory from item
                        SubCategoryItems removeSingle = ApplicationDbContext.SubCategoryItems
                                                        .Where(i => i.ItemId == viewModel.ItemId)
                                                        .First(i => i.SubcategoryId == itemSub);

                        ApplicationDbContext.SubCategoryItems
                        .Remove(removeSingle);

                        ApplicationDbContext.SaveChanges();
                    }
                }
            }

            ApplicationDbContext.SaveChanges();

            List <Category> categoryList = ApplicationDbContext.Categories.ToList();

            viewModel.Categories = categoryList;

            return(View("UpdateItem", viewModel));
        }