Ejemplo n.º 1
0
        public IActionResult DeleteProduct(int id)
        {
            ProductDBO dto = db.Products.Find(id);

            db.Products.Remove(dto);
            db.SaveChanges();


            TempData["SM"] = "Продукт удален!";

            return(RedirectToAction("Products"));
        }
Ejemplo n.º 2
0
        public FileContentResult GetPreview(int id)
        {
            ProductDBO img = db.Products.Find(id);

            if (img != null)
            {
                return(File(img.Img, img.ImgType));
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 3
0
        public IActionResult EditProduct(int id, int?secId)
        {
            List <CategoryVM> categoriesList;

            ProductVM model;


            ProductDBO dto = db.Products.Find(id);

            if (dto == null)
            {
                return(Content("Продукт не найден!"));
            }
            categoriesList = db.Categories.ToArray()
                             .Where(x => (secId == null || secId == 0 || x.SectionId == secId))
                             .Select(x => new CategoryVM(x))
                             .ToList();

            model = new ProductVM(dto);

            model.Categories = new SelectList(categoriesList, "Id", "Name");
            model.Materials  = new SelectList(db.Materials.ToList(), "Id", "Name");
            model.Sections   = new SelectList(db.Sections.ToList(), "Id", "Name");

            ViewBag.SelectedSec = secId.ToString();

            //GALLRY///////////////////////////////////////
            List <GalleryVM> gallery;
            List <int>       ids = new List <int>();

            gallery = db.ProductGallery
                      .ToArray()
                      .Where(x => x.ProductId == id)
                      .Select(x => new GalleryVM(x))
                      .ToList();

            foreach (var item in gallery)
            {
                ids.Add(item.Id);
            }
            ViewBag.Ids = ids;


            return(View(model));
        }
Ejemplo n.º 4
0
 public ProductVM(ProductDBO row)
 {
     Id           = row.Id;
     Name         = row.Name;
     Slug         = row.Slug;
     Description  = row.Description;
     Price        = row.Price;
     CategoryName = row.CategoryName;
     CategoryId   = row.CategoryId;
     MaterialName = row.MaterialName;
     MaterialId   = row.MaterialId;
     Img          = row.Img;
     ProductCode  = row.ProductCode;
     ImgType      = row.ImgType;
     Discount     = row.Discount;
     SectionId    = row.SectionId;
     SectionName  = row.SectionName;
 }
Ejemplo n.º 5
0
        public IActionResult GetProduct(string pc)
        {
            CartLong();

            int id = Convert.ToInt32(pc.Substring(4));

            ProductVM product;

            ProductDBO dto = db.Products.Find(id);

            product = new ProductVM(dto);


            List <GalleryVM> gallery;
            List <int>       ids = new List <int>();

            gallery = db.ProductGallery
                      .ToArray()
                      .Where(x => x.ProductId == id)
                      .Select(x => new GalleryVM(x))
                      .ToList();


            foreach (var item in gallery)
            {
                ids.Add(item.Id);
            }
            ViewBag.Ids = ids;

            if (Request.Headers["Referer"].ToString() == null)
            {
                ViewBag.Ref = "glorious.com.ua";
            }
            else
            {
                ViewBag.Ref = Request.Headers["Referer"].ToString();
            }

            return(View(product));
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> AddProduct(ProductVM model, IFormFile file = null)
        {
            if (!ModelState.IsValid)
            {
                model.Categories = new SelectList(db.Categories.ToList(), "Id", "Name");
                model.Materials  = new SelectList(db.Materials.ToList(), "Id", "Name");


                return(View(model));
            }

            if (file != null && file.Length > 0)
            {
                string ext = file.ContentType.ToLower();

                if (ext != "image/jpg" &&
                    ext != "image/jpeg" &&
                    ext != "image/pjpeg" &&
                    ext != "image/gif" &&
                    ext != "image/x-png" &&
                    ext != "image/png")
                {
                    model.Categories = new SelectList(db.Categories.ToList(), "Id", "Name");
                    model.Materials  = new SelectList(db.Materials.ToList(), "Id", "Name");


                    ModelState.AddModelError("", "Формат файла неподдерживается. Доступные форматы: jpg,jpeg,pjpeg,gif,x-png,png");
                    return(View(model));
                }
            }

            if (db.Products.Any(x => x.Name == model.Name))
            {
                model.Categories = new SelectList(db.Categories.ToList(), "Id", "Name");
                model.Materials  = new SelectList(db.Materials.ToList(), "Id", "Name");

                ModelState.AddModelError("", "Имя уже существует!");
                return(View(model));
            }

            int id;

            ProductDBO dto = new ProductDBO
            {
                Name        = model.Name,
                Slug        = model.Name.Replace(" ", "-").ToLower(),
                Description = model.Description,
                Price       = model.Price,
                Discount    = model.Discount,
                ProductCode = "1",
                CategoryId  = model.CategoryId,
                MaterialId  = model.MaterialId,
                SectionId   = model.SectionId,
                SectionName = model.SectionName
            };

            if (file != null && file.Length > 0)
            {
                using (var stream = new MemoryStream())
                {
                    await file.CopyToAsync(stream);

                    dto.Img     = stream.ToArray();
                    dto.ImgType = file.ContentType;
                }
            }

            CategoryDBO catDTo = db.Categories.FirstOrDefault(x => x.Id == model.CategoryId);

            dto.CategoryName = catDTo.Name;

            MaterialDBO matDTO = db.Materials.FirstOrDefault(x => x.Id == model.MaterialId);

            dto.MaterialName = matDTO.Name;

            SectionDBO secDTO = db.Sections.FirstOrDefault(x => x.Id == model.SectionId);

            dto.SectionName = secDTO.Name;

            db.Products.Add(dto);
            db.SaveChanges();

            id = db.Products.Max(i => i.Id);
            string     prodCode = model.SectionId.ToString("00") + model.CategoryId.ToString("00") + id.ToString("0000");
            ProductDBO dto_     = db.Products.Find(id);

            dto_.ProductCode = prodCode;
            db.SaveChanges();



            TempData["SM"] = "Продукт добавлен!";

            return(RedirectToAction("AddProduct"));
        }
Ejemplo n.º 7
0
        public async Task <IActionResult> EditProduct(ProductVM model, IFormFile file = null)
        {
            int id = model.Id;

            model.Categories = new SelectList(db.Categories.ToList(), "Id", "Name");
            model.Materials  = new SelectList(db.Materials.ToList(), "Id", "Name");

            if (file != null && file.Length > 0)
            {
                string ext = file.ContentType.ToLower();

                if (ext != "image/jpg" &&
                    ext != "image/jpeg" &&
                    ext != "image/pjpeg" &&
                    ext != "image/gif" &&
                    ext != "image/x-png" &&
                    ext != "image/png")
                {
                    ModelState.AddModelError("", "Формат файла неподдерживается. Доступные форматы: jpg,jpeg,pjpeg,gif,x-png,png");
                    return(View(model));
                }
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            if (db.Products.Where(x => x.Id != id).Any(x => x.Name == model.Name))
            {
                ModelState.AddModelError("", "Имя уже существует");
            }

            ProductDBO dto = db.Products.Find(id);

            dto.Name        = model.Name;
            dto.Description = model.Description;
            dto.Price       = model.Price;
            dto.Discount    = model.Discount;
            dto.CategoryId  = model.CategoryId;
            dto.MaterialId  = model.MaterialId;
            dto.SectionId   = model.SectionId;
            dto.SectionName = model.SectionName;
            if (file != null && file.Length > 0)
            {
                using (var stream = new MemoryStream())
                {
                    await file.CopyToAsync(stream);

                    dto.Img     = stream.ToArray();
                    dto.ImgType = file.ContentType;
                }
            }
            CategoryDBO catDTo = db.Categories.FirstOrDefault(x => x.Id == model.CategoryId);

            dto.CategoryName = catDTo.Name;

            MaterialDBO matDTO = db.Materials.FirstOrDefault(x => x.Id == model.MaterialId);

            dto.MaterialName = matDTO.Name;

            SectionDBO secDTO = db.Sections.FirstOrDefault(x => x.Id == model.SectionId);

            dto.SectionName = secDTO.Name;

            db.SaveChanges();



            TempData["SM"] = "Продукт изменен!";

            return(RedirectToAction("Products"));
        }