Esempio n. 1
0
        public async Task <IActionResult> Create(ProductAndCategoryViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            string imageName = "noimage.png";

            if (model.Product.ImageUpload != null)
            {
                string uploadsDir = Path.Combine(_webHostEnvironment.WebRootPath, "images/products");
                imageName = Guid.NewGuid().ToString() + "_" + model.Product.ImageUpload.FileName;
                string     filePath = Path.Combine(uploadsDir, imageName);
                FileStream fs       = new FileStream(filePath, FileMode.Create);
                await model.Product.ImageUpload.CopyToAsync(fs);

                fs.Close();
            }

            model.Product.Image = imageName;

            _db.Add(model.Product);
            await _db.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
Esempio n. 2
0
        //[HttpGet] for search
        //public async Task<IActionResult> Index(string productsearch)
        //{
        //    ViewData["GetProducts"] = productsearch;
        //    var query = from x in _db.ProductsModel select x;

        //    if (!String.IsNullOrEmpty(productsearch))
        //    {
        //        query = query.Where(x => x.Name.Contains(productsearch));
        //    }
        //    return View(await query.AsNoTracking().ToListAsync());
        //}

        public async Task <IActionResult> AddProducts()
        {
            ProductAndCategoryViewModel PAC = new ProductAndCategoryViewModel()
            {
                CategoryList = await _db.categoriesModels.ToListAsync(),
                Products     = new Models.ProductsModel()
            };

            return(View(PAC));
        }
Esempio n. 3
0
        public async Task <IActionResult> Create()
        {
            ProductAndCategoryViewModel model = new ProductAndCategoryViewModel()
            {
                Product      = new Models.Product(),
                CategoryList = await _db.Category.ToListAsync()
            };

            return(View(model));
        }
Esempio n. 4
0
        // GET
        public async Task <IActionResult> Index(int?category, string name, int page = 1,
                                                SortState sortOrder = SortState.NameAsc)
        {
            int pageSize = 4;

            //фильтрация
            IQueryable <Product> products = _context.Products.Include(x => x.CategoryProduct);

            if (category != null && category != 0)
            {
                products = products.Where(p => p.CategoryProductId == category);
            }
            if (!String.IsNullOrEmpty(name))
            {
                products = products.Where(p => p.Name.Contains(name));
            }

            // сортировка
            switch (sortOrder)
            {
            case SortState.NameDesc:
                products = products.OrderByDescending(s => s.Name);
                break;

            case SortState.AgeAsc:
            case SortState.CategoryAsc:
                products = products.OrderBy(s => s.CategoryProduct.Name);
                break;

            case SortState.CategoryDesc:
                products = products.OrderByDescending(s => s.CategoryProduct.Name);
                break;

            default:
                products = products.OrderBy(s => s.Name);
                break;
            }

            // пагинация
            var count = await products.CountAsync();

            var items = await products.Skip((page - 1) *pageSize).Take(pageSize).ToListAsync();

            // формируем модель представления
            ProductAndCategoryViewModel viewModel = new ProductAndCategoryViewModel
            {
                PageViewModel    = new PageViewModel(count, page, pageSize),
                SortViewModel    = new SortViewModel(sortOrder),
                FilterViewModel  = new FilterViewModel(_context.CategoryProducts.ToList(), category, name),
                Products         = items,
                CategoryProducts = await _context.CategoryProducts.ToListAsync()
            };

            return(View(viewModel));
        }
Esempio n. 5
0
        public async Task <IActionResult> AddProducts(ProductAndCategoryViewModel PAC)
        {
            var menuItemFromDb = await _db.ProductsModel.FindAsync(PAC.Products.Id);


            if (ModelState.IsValid)
            {
                var duplicate = _db.ProductsModel.Include(s => s.CategoriesModel).Where(s => s.Name == PAC.Products.Name && s.CategoriesModel.Id == PAC.Products.CategoryId);
                if (duplicate.Count() > 0)
                {
                    StatusMessage = "Error : Product Already exists under " + duplicate.First().CategoriesModel.Name + " Category, Please use Another Name";
                }
                else
                {
                    var files = HttpContext.Request.Form.Files;
                    if (files.Count > 0)
                    {
                        byte[] p1 = null;
                        using (var fs1 = files[0].OpenReadStream())
                        {
                            using (var ms1 = new MemoryStream())
                            {
                                fs1.CopyTo(ms1);
                                p1 = ms1.ToArray();
                            }
                        }
                        PAC.Products.ProductPhoto = p1;
                    }

                    _db.ProductsModel.Add(PAC.Products);
                    await _db.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
            }
            ProductAndCategoryViewModel mv = new ProductAndCategoryViewModel()
            {
                CategoryList  = await _db.categoriesModels.ToListAsync(),
                Products      = new Models.ProductsModel(),
                StatusMessage = StatusMessage
            };

            return(View(mv));
        }
Esempio n. 6
0
        public async Task <IActionResult> Edit(int id, ProductAndCategoryViewModel PAC)
        {
            if (ModelState.IsValid)
            {
                var product = await _db.ProductsModel.FindAsync(id);

                if (product == null)
                {
                    return(NotFound());
                }
                var files = HttpContext.Request.Form.Files;
                if (files.Count > 0)
                {
                    byte[] p1 = null;
                    using (var fs1 = files[0].OpenReadStream())
                    {
                        using (var ms1 = new MemoryStream())
                        {
                            fs1.CopyTo(ms1);
                            p1 = ms1.ToArray();
                        }
                    }
                    product.ProductPhoto = p1;
                }
                product.Name       = PAC.Products.Name;
                product.About      = PAC.Products.About;
                product.CategoryId = PAC.Products.CategoryId;
                product.Price      = PAC.Products.Price;

                await _db.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ProductAndCategoryViewModel mv = new ProductAndCategoryViewModel()
            {
                CategoryList  = await _db.categoriesModels.ToListAsync(),
                Products      = new Models.ProductsModel(),
                StatusMessage = StatusMessage
            };

            return(View(mv));
        }
Esempio n. 7
0
        public async Task <IActionResult> View(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            var products = await _db.ProductsModel.SingleOrDefaultAsync(m => m.Id == id);

            if (products == null)
            {
                return(NotFound());
            }
            ProductAndCategoryViewModel PAC = new ProductAndCategoryViewModel()
            {
                CategoryList = await _db.categoriesModels.ToListAsync(),
                Products     = products
            };

            return(View(PAC));
        }
Esempio n. 8
0
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var product = await _db.Product.FindAsync(id);

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

            ProductAndCategoryViewModel model = new ProductAndCategoryViewModel()
            {
                CategoryList = await _db.Category.ToListAsync(),
                Product      = product
            };

            return(View(model));
        }
Esempio n. 9
0
        public async Task <IActionResult> Edit(int id, ProductAndCategoryViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (model.Product.ImageUpload != null)
                {
                    string uploadsDir = Path.Combine(_webHostEnvironment.WebRootPath, "images/products");

                    if (!string.Equals(model.Product.Image, "noimage.png"))
                    {
                        // delete old image
                        string oldImagePath = Path.Combine(uploadsDir, model.Product.Image);
                        if (System.IO.File.Exists(oldImagePath))
                        {
                            System.IO.File.Delete(oldImagePath);
                        }
                    }

                    string     imageName = Guid.NewGuid().ToString() + "_" + model.Product.ImageUpload.FileName;
                    string     filePath  = Path.Combine(uploadsDir, imageName);
                    FileStream fs        = new FileStream(filePath, FileMode.Create);
                    await model.Product.ImageUpload.CopyToAsync(fs);

                    fs.Close();
                    model.Product.Image = imageName;
                }


                _db.Product.Update(model.Product);
                await _db.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            return(View(model));
        }