public ActionResult Create(Category model) { _repository.LangId = CurrentLangId; try { model.Id = 0; Category parent = null; int categoryLevel = 0; if (model.CategoryId != null) { parent = _repository.GetCategory((int)model.CategoryId); categoryLevel = parent.CategoryLevel + 1; } var category = new Category { Name = SiteHelper.UpdatePageWebName(model.Name), SortOrder = model.SortOrder, Parent = parent, CategoryLevel = categoryLevel, IsActive = model.IsActive, Title = model.Title, SeoDescription = model.SeoDescription, SeoKeywords = model.SeoKeywords, SeoText = model.SeoText }; var file = Request.Files[0]; if (file != null && !string.IsNullOrEmpty(file.FileName)) { string fileName = IOHelper.GetUniqueFileName("~/Content/Images", file.FileName); string filePath = Server.MapPath("~/Content/Images"); filePath = Path.Combine(filePath, fileName); GraphicsHelper.SaveOriginalImage(filePath, fileName, file, 1500); category.ImageSource = fileName; } else { category.ImageSource = category.ImageSource ?? ""; } _repository.AddCategory(category); } catch (Exception ex) { TempData["errorMessage"] = ex.Message; return View(model); } return RedirectToAction("Index"); }
public int AddCategory(Category category) { if (_store.Categories.Any(c => c.Name == category.Name)) { throw new Exception(string.Format("Category {0} already exists", category.Name)); } _store.Categories.Add(category); CreateOrChangeEntityLanguage(category); _store.SaveChanges(); return category.Id; }
private void Visit(Category node) { _result.Add(node); if (node.Children == null || node.Children.Count == 0) { return; } foreach (var child in node.Children.OrderBy(c => c.SortOrder)) { Visit(child); } }
private void CreateOrChangeEntityLanguage(Category cache) { var categoryLang = _store.CategoryLangs.FirstOrDefault(r => r.CategoryId == cache.Id && r.LanguageId == LangId); if (categoryLang == null) { var entityLang = new CategoryLang { CategoryId = cache.Id, LanguageId = LangId, Title = cache.Title, SeoDescription = cache.SeoDescription, SeoKeywords = cache.SeoKeywords, SeoText = cache.SeoText }; _store.CategoryLangs.Add(entityLang); } else { categoryLang.Title = cache.Title; categoryLang.SeoDescription = cache.SeoDescription; categoryLang.SeoKeywords = cache.SeoKeywords; categoryLang.SeoText = cache.SeoText; } }
public void SaveCategory(Category category) { var cache = _store.Categories.Single(c => c.Id == category.Id); //if (cache.Name != category.Name) //{ // if (_store.Categories.Any(c => c.Name == category.Name)) // { // throw new Exception(string.Format("Category {0} already exists", category.Name)); // } //} //cache.Name = category.Name; //cache.SortOrder = category.SortOrder; //cache.Title = category.Title; //cache.SeoDescription = category.SeoDescription; //cache.SeoKeywords = category.SeoKeywords; //cache.SeoText = category.SeoText; CreateOrChangeEntityLanguage(cache); _store.SaveChanges(); }
public ActionResult Edit(Category model) { _repository.LangId = CurrentLangId; try { var category = _repository.GetCategory(model.Id); category.Name = SiteHelper.UpdatePageWebName(model.Name); TryUpdateModel(category, new[] { "SortOrder","IsActive", "CategoryLevel", "Title", "SeoDescription", "SeoKeywords", "SeoText" }); var file = Request.Files[0]; if (file != null && !string.IsNullOrEmpty(file.FileName)) { if (!string.IsNullOrEmpty(category.ImageSource)) { ImageHelper.DeleteImage(category.ImageSource); } string fileName = IOHelper.GetUniqueFileName("~/Content/Images", file.FileName); string filePath = Server.MapPath("~/Content/Images"); filePath = Path.Combine(filePath, fileName); GraphicsHelper.SaveOriginalImage(filePath, fileName, file, 1500); category.ImageSource = fileName; } else { category.ImageSource = category.ImageSource ?? ""; } _repository.SaveCategory(category); } catch (Exception ex) { TempData["errorMessage"] = ex.Message; return View(model); } return RedirectToAction("Index"); }