コード例 #1
0
 public CategoryVM(CategoryDBO row)
 {
     Id          = row.Id;
     Name        = row.Name;
     Slug        = row.Slug;
     Sorting     = row.Sorting;
     SectionId   = row.SectionId;
     SectionName = row.SectionName;
 }
コード例 #2
0
        public IActionResult DeleteCategory(int id)
        {
            CategoryDBO dto = db.Categories.Find(id);

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


            return(RedirectToAction("Categories"));
        }
コード例 #3
0
        public string СhangeSection(int catId, int secId)
        {
            try
            {
                CategoryDBO dbo = db.Categories.Find(catId);
                dbo.SectionId   = secId;
                dbo.SectionName = db.Sections.Find(secId).Name;

                db.SaveChanges();
                return("ok");
            }
            catch (Exception)
            {
                return("something is wrong");
            }
        }
コード例 #4
0
        public string AddCategory(string catName, int secId)
        {
            string id;

            if (db.Categories.Any(x => x.Name == catName))
            {
                return("titletaken");
            }
            CategoryDBO dto = new CategoryDBO
            {
                Name        = catName,
                Slug        = catName.Replace(" ", "-").ToLower(),
                Sorting     = 100,
                SectionId   = secId,
                SectionName = db.Sections.Find(secId).Name
            };

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

            id = dto.Id.ToString();

            return(id);
        }
コード例 #5
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"));
        }
コード例 #6
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"));
        }