public SectionVM(SectionDBO row) { Id = row.Id; Name = row.Name; Slug = row.Slug; Sorting = row.Sorting; }
public IActionResult DeleteSection(int id) { SectionDBO dto = db.Sections.Find(id); db.Sections.Remove(dto); db.SaveChanges(); return(RedirectToAction("Sections")); }
public string AddSection(string secName) { string id; if (db.Sections.Any(x => x.Name == secName)) { return("titletaken"); } SectionDBO dto = new SectionDBO(); dto.Name = secName; dto.Slug = secName.Replace(" ", "-").ToLower(); dto.Sorting = 100; db.Sections.Add(dto); db.SaveChanges(); id = dto.Id.ToString(); return(id); }
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")); }
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")); }