public ActionResult CreateCategory() { using (UnitOfWorkManager.NewUnitOfWork()) { var categoryViewModel = new CategoryViewModel { AllCategories = _categoryService.GetAll().ToList() }; return View(categoryViewModel); } }
public ActionResult CreateCategory(CategoryViewModel categoryViewModel) { if (ModelState.IsValid) { using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork()) { try { var category = new Category { Name = categoryViewModel.Name, Description = categoryViewModel.Description, IsLocked = categoryViewModel.IsLocked, ModeratePosts = categoryViewModel.ModeratePosts, ModerateTopics = categoryViewModel.ModerateTopics, SortOrder = categoryViewModel.SortOrder, PageTitle = categoryViewModel.PageTitle, MetaDescription = categoryViewModel.MetaDesc, Colour = categoryViewModel.CategoryColour }; // Sort image out first if (categoryViewModel.Files != null) { // Before we save anything, check the user already has an upload folder and if not create one var uploadFolderPath = HostingEnvironment.MapPath(string.Concat(SiteConstants.UploadFolderPath, category.Id)); if (!Directory.Exists(uploadFolderPath)) { Directory.CreateDirectory(uploadFolderPath); } // Loop through each file and get the file info and save to the users folder and Db var file = categoryViewModel.Files[0]; if (file != null) { // If successful then upload the file var uploadResult = AppHelpers.UploadFile(file, uploadFolderPath, LocalizationService, true); if (!uploadResult.UploadSuccessful) { TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel { Message = uploadResult.ErrorMessage, MessageType = GenericMessages.danger }; return View(categoryViewModel); } // Save avatar to user category.Image = uploadResult.UploadedFileName; } } if (categoryViewModel.ParentCategory != null) { var parentCategory = _categoryService.Get(categoryViewModel.ParentCategory.Value); category.ParentCategory = parentCategory; SortPath(category, parentCategory); } _categoryService.Add(category); // We use temp data because we are doing a redirect TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel { Message = "Category Created", MessageType = GenericMessages.success }; unitOfWork.Commit(); } catch (Exception) { unitOfWork.Rollback(); } } } else { ModelState.AddModelError("", "There was an error creating the category"); } return RedirectToAction("Index"); }
private CategoryViewModel CreateEditCategoryViewModel(Category category) { var categoryViewModel = new CategoryViewModel { Name = category.Name, Description = category.Description, IsLocked = category.IsLocked, ModeratePosts = category.ModeratePosts == true, ModerateTopics = category.ModerateTopics == true, SortOrder = category.SortOrder, Id = category.Id, PageTitle = category.PageTitle, MetaDesc = category.MetaDescription, Image = category.Image, CategoryColour = category.Colour, ParentCategory = category.ParentCategory == null ? Guid.Empty : category.ParentCategory.Id, AllCategories = _categoryService.GetAll() .Where(x => x.Id != category.Id) .ToList() }; return categoryViewModel; }
public ActionResult EditCategory(CategoryViewModel categoryViewModel) { if (ModelState.IsValid) { using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork()) { try { var category = _categoryService.Get(categoryViewModel.Id); // Sort image out first if (categoryViewModel.Files != null) { // Before we save anything, check the user already has an upload folder and if not create one var uploadFolderPath = HostingEnvironment.MapPath(string.Concat(SiteConstants.UploadFolderPath, categoryViewModel.Id)); if (!Directory.Exists(uploadFolderPath)) { Directory.CreateDirectory(uploadFolderPath); } // Loop through each file and get the file info and save to the users folder and Db var file = categoryViewModel.Files[0]; if (file != null) { // If successful then upload the file var uploadResult = AppHelpers.UploadFile(file, uploadFolderPath, LocalizationService, true); if (!uploadResult.UploadSuccessful) { TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel { Message = uploadResult.ErrorMessage, MessageType = GenericMessages.danger }; return View(categoryViewModel); } // Save avatar to user category.Image = uploadResult.UploadedFileName; } } category.Description = categoryViewModel.Description; category.IsLocked = categoryViewModel.IsLocked; category.ModeratePosts = categoryViewModel.ModeratePosts; category.ModerateTopics = categoryViewModel.ModerateTopics; category.Name = categoryViewModel.Name; category.SortOrder = categoryViewModel.SortOrder; category.PageTitle = categoryViewModel.PageTitle; category.MetaDescription = categoryViewModel.MetaDesc; category.Colour = categoryViewModel.CategoryColour; if (categoryViewModel.ParentCategory != null) { // Set the parent category var parentCategory = _categoryService.Get(categoryViewModel.ParentCategory.Value); category.ParentCategory = parentCategory; // Append the path from the parent category SortPath(category, parentCategory); } else { // Must access property (trigger lazy-loading) before we can set it to null (Entity Framework bug!!!) var triggerEfLoad = category.ParentCategory; category.ParentCategory = null; // Also clear the path category.Path = null; } _categoryService.UpdateSlugFromName(category); TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel { Message = "Category Updated", MessageType = GenericMessages.success }; categoryViewModel = CreateEditCategoryViewModel(category); unitOfWork.Commit(); } catch (Exception ex) { LoggingService.Error(ex); unitOfWork.Rollback(); TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel { Message = "Category Update Failed", MessageType = GenericMessages.danger }; } } } return View(categoryViewModel); }