public async Task <IActionResult> Create(ProductCategoriesViewModel productCategoriesViewModel, IFormFile file, string[] selectedCategories)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    productCategoriesViewModel.Image = file;
                    var product = productCategoriesViewModel.ToProductModel();

                    product.DateCreated = DateTime.Now;
                    product.UserCreated = User.Identity.Name;

                    await UploadImageToAzure(file);

                    AddCategoriesToProduct(product, selectedCategories);
                    _context.Add(product);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, message: "Greška prilikom stvaranja produkta");
                    return(View("Error"));
                }
            }
            return(View(productCategoriesViewModel));
        }
Esempio n. 2
0
        public PartialViewResult RenderBestSellingProductCategories()
        {
            var model = new ProductCategoriesViewModel();

            model.Categories = this.WorkerService.GetBestSellingProductCategories();
            return(PartialView("ProductCategories", model));
        }
Esempio n. 3
0
        public async Task <ProductCategoriesViewModel> GetProductCategories(Guid uid)
        {
            var categories = await _shopy.ListCategories();

            var model = new ProductCategoriesViewModel()
            {
                AvailableCategories = categories
                                      .Where(c => !c.Products.Any(p => p.Uid == uid))
                                      .Select(c => new CategoryListItemViewModel
                {
                    Uid  = c.Uid,
                    Name = c.Name
                }),

                AssignedCategories = categories
                                     .Where(c => c.Products.Any(p => p.Uid == uid))
                                     .Select(c => new CategoryListItemViewModel
                {
                    Uid  = c.Uid,
                    Name = c.Name
                })
            };

            return(model);
        }
        public async Task <IActionResult> Index(string ProductCategory, string searchString)
        {
            //Use LINQ to get list of genres.
            var categoryQuery = from m in _context.Category
                                orderby m.CategoryName
                                select m.CategoryName;

            var products = from m in _context.Product
                           select m;

            if (!string.IsNullOrEmpty(searchString))
            {
                products = products.Where(s => s.Text.Contains(searchString));
            }

            if (!string.IsNullOrEmpty(ProductCategory))
            {
                products = products.Where(p => p.Category.CategoryName == ProductCategory);
            }

            var productCategoryMV = new ProductCategoriesViewModel
            {
                Categories = new SelectList(await categoryQuery.Distinct().ToListAsync()),
                Products   = await products.ToListAsync()
            };

            return(View(productCategoryMV));
        }
        // GET: Products/Create
        public IActionResult Create()
        {
            var productCategoryViewModel = new ProductCategoriesViewModel {
                Categories = FetchCategories()
            };

            return(View(productCategoryViewModel));
        }
Esempio n. 6
0
        public ActionResult AddProduct()
        {
            var cateogry  = context.categoryDb.ToList();
            var viewModel = new ProductCategoriesViewModel
            {
                Category = cateogry
            };

            return(View(viewModel));
        }
        public async Task <IActionResult> Categories()
        {
            var lkpService        = _lookupServiceFactory.Create <ProductCategory>();
            var productCategories = await lkpService.GetAllAsync();

            var vm = new ProductCategoriesViewModel()
            {
                ProductCategories = Mapper.Map(productCategories).ToANew <IEnumerable <ProductCategoriesViewModel.ProductCategoryDTO> >()
            };

            return(View(vm));
        }
        public ActionResult Categories_Update([DataSourceRequest]DataSourceRequest request, ProductCategoriesViewModel category)
        {
            if (ModelState.IsValid)
            {
                var entity = new Category
                {
                    Id = category.Id,
                    Name = category.Name
                };

                this.categories.Update(entity);
            }

            return Json(new[] { category }.ToDataSourceResult(request, ModelState));
        }
        public async Task <IActionResult> Edit(int id, [Bind("ProductID,SKU,Name,ImagePath")] ProductCategoriesViewModel productCategoriesViewModel, IFormFile file, string[] selectedCategories)
        {
            if (id != productCategoriesViewModel.ProductID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var productToUpdate = await _context.Product.Include(x => x.Categories).SingleOrDefaultAsync(x => x.ID == id);

                    productToUpdate.Name = productCategoriesViewModel.Name;
                    productToUpdate.SKU  = productCategoriesViewModel.SKU;

                    if (file.FileName.CompareTo(productToUpdate.ImageName) != 0)
                    {
                        productToUpdate.ImageName = file.FileName;
                        await DeleteImageFromAzure(file);
                        await UploadImageToAzure(file);
                    }

                    productToUpdate.DateModified = DateTime.Now;
                    productToUpdate.UserModified = User.Identity.Name;

                    UpdateProductCategories(productToUpdate, selectedCategories);
                    PopulateAssignedCategories(productCategoriesViewModel, productToUpdate.Categories);

                    _context.Update(productToUpdate);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ProductExists(productCategoriesViewModel.ProductID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        _logger.LogError("Greška prilikom editiranja produkta.");
                        return(View("Error"));
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(productCategoriesViewModel));
        }
        private void PopulateAssignedCategories(ProductCategoriesViewModel productViewModel, ICollection <ProductCategory> productCategories)
        {
            var allCategories   = _context.Category;
            var categories      = productCategories.Select(x => x.CategoryId).ToList();
            var assignViewModel = new List <AssignedProductCategory>();

            foreach (var category in allCategories)
            {
                assignViewModel.Add(new AssignedProductCategory
                {
                    CategoryID   = category.ID,
                    CategoryName = category.Name,
                    Assigned     = categories.Contains(category.ID)
                });
            }

            productViewModel.Categories = assignViewModel;
        }
Esempio n. 11
0
        public ActionResult EditProduct(int id)
        {
            var category = context.categoryDb.ToList();
            var product  = context.productDb.SingleOrDefault(p => p.id == id);

            product.oldCategoryId = product.CategoryId;
            var viewModel = new ProductCategoriesViewModel
            {
                Category = category,
                Product  = product
            };

            if (product == null)
            {
                return(HttpNotFound());
            }

            return(View("EditProduct", viewModel));
        }
Esempio n. 12
0
        public ActionResult Index([FromUri] int?parentId)
        {
            object parentCategoryIdObject = TempData[Constants.TempDataKeys.PRODUCT_CATEGORIES_PARENT_ID];

            int?parentCategoryId = (int?)parentCategoryIdObject ?? parentId;

            List <ProductCategory> parentCategories = new List <ProductCategory>();

            if (parentCategoryId.HasValue)
            {
                parentCategories.AddRange(_productCategoriesBL.GetParentCategories(parentCategoryId.Value));
                parentCategories.Add(_productCategoriesBL.GetById(parentCategoryId.Value));
            }

            List <ProductCategoriesBreadCrumbItem> breadCrumbs = new List <ProductCategoriesBreadCrumbItem>(
                parentCategories.Select(x => new ProductCategoriesBreadCrumbItem
            {
                Id   = x.Id,
                Name = x.Name
            }));

            ProductCategoriesBreadCrumbItem rootCategoriesBreadCrumbItem = new ProductCategoriesBreadCrumbItem
            {
                Id   = null,
                Name = ".."
            };

            breadCrumbs.Insert(0, rootCategoriesBreadCrumbItem);

            ProductCategoriesViewModel model = new ProductCategoriesViewModel
            {
                BreadCrumbItems  = breadCrumbs,
                ParentCategoryId = parentCategoryId
            };

            return(View(model));
        }
 public MainViewModel()
 {
     Content = new ProductCategoriesViewModel();
 }
Esempio n. 14
0
        public ActionResult AddConfirmmed(Product product, HttpPostedFileBase imageFile)
        {
            if (product.id == 0)
            {
                if (imageFile != null)
                {
                    string fileName  = Path.GetFileNameWithoutExtension(imageFile.FileName);
                    string extension = Path.GetExtension(imageFile.FileName);
                    fileName      = fileName + DateTime.Now.ToString("yymmssfff") + extension;
                    product.image = "~/Image/" + fileName;
                    fileName      = Path.Combine(Server.MapPath("~/Image/"), fileName);
                    imageFile.SaveAs(fileName);
                }
                else
                {
                    var viewModel = new ProductCategoriesViewModel
                    {
                        Product  = product,
                        Category = context.categoryDb.ToList()
                    };
                    return(View("AddProduct", viewModel));
                }

                Category category = new Category();
                category = context.categoryDb.Find(product.CategoryId);
                category.number_of_products++;

                context.productDb.Add(product);
            }

            else
            {
                if (imageFile != null)
                {
                    string fileName  = Path.GetFileNameWithoutExtension(imageFile.FileName);
                    string extension = Path.GetExtension(imageFile.FileName);
                    fileName      = fileName + DateTime.Now.ToString("yymmssfff") + extension;
                    product.image = "~/Image/" + fileName;
                    fileName      = Path.Combine(Server.MapPath("~/Image/"), fileName);
                    imageFile.SaveAs(fileName);
                }
                else
                {
                    var viewModel = new ProductCategoriesViewModel
                    {
                        Product  = product,
                        Category = context.categoryDb.ToList()
                    };
                    var editAction = EditProduct(product.id);
                    return(editAction);
                }

                var productInDb = context.productDb.Single(p => p.id == product.id);
                productInDb.name        = product.name;
                productInDb.price       = product.price;
                productInDb.image       = product.image;
                productInDb.description = product.description;
                productInDb.CategoryId  = product.CategoryId;
                if (productInDb.CategoryId != product.oldCategoryId)
                {
                    Category category = new Category();
                    category = context.categoryDb.Find(productInDb.CategoryId);
                    category.number_of_products++;

                    category = context.categoryDb.Find(product.oldCategoryId);
                    category.number_of_products--;
                }
            }
            context.SaveChanges();
            return(RedirectToAction("ListProducts", "Products"));
        }