public ActionResult Create(CreateEditTopicViewModel viewModel) { using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork()) { var cats = _categoryService.GetAllowedEditCategories(UsersRole); if (cats.Count > 0) { if (ModelState.IsValid) { if (CheckCats(viewModel.Category, cats)) { var topic = new Topic(); var post = new Post(); topic.Name = viewModel.Name; topic.Category_Id = viewModel.Category; topic.IsLocked = viewModel.IsLocked; topic.IsSticky = viewModel.IsSticky; topic.MembershipUser_Id = LoggedOnReadOnlyUser.Id; topic.Post_Id = post.Id; post.PostContent = viewModel.Content; post.MembershipUser_Id = LoggedOnReadOnlyUser.Id; post.Topic_Id = topic.Id; post.IsTopicStarter = true; topic.ShotContent = string.Concat(StringUtils.ReturnAmountWordsFromString(StringUtils.StripHtmlFromString(post.PostContent), 50), "...."); topic.isAutoShotContent = true; // Sort image out first if (viewModel.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.Instance.UploadFolderPath, topic.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 = viewModel.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(viewModel)); } // Save avatar to user topic.Image = uploadResult.UploadedFileName; //viewModel.Image = topic.Image; } } try { _topicServic.Add(topic); _postSevice.Add(post); unitOfWork.Commit(); return(RedirectToAction("Edit", new { Id = topic.Id })); } catch (Exception ex) { LoggingService.Error(ex.Message); unitOfWork.Rollback(); } } else { //viewModel.Category = null; //No permission to create a Poll so show a message but create the topic //TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel //{ // Message = LocalizationService.GetResourceString("Errors.NoPermissionCatergory"), // MessageType = GenericMessages.info //}; ModelState.AddModelError(string.Empty, LocalizationService.GetResourceString("Errors.CatergoryMessage")); } } viewModel.Categories = _categoryService.GetBaseSelectListCategories(cats); return(View(viewModel)); } return(ErrorToHomePage(LocalizationService.GetResourceString("Errors.NoPermission"))); } }
public ActionResult Create(CreateEditTopicViewModel viewModel) { using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork()) { var cats = _categoryService.GetAllowedEditCategories(UsersRole); if (cats.Count > 0) { if (ModelState.IsValid) { if (CheckCats(viewModel.Category, cats)) { var topic = new Topic(); var post = new Post(); topic.Name = viewModel.Name; topic.Category_Id = viewModel.Category; topic.IsLocked = viewModel.IsLocked; topic.IsSticky = viewModel.IsSticky; topic.MembershipUser_Id = LoggedOnReadOnlyUser.Id; topic.Id = post.Id; post.PostContent = viewModel.Content; post.MembershipUser_Id = LoggedOnReadOnlyUser.Id; post.Topic_Id = topic.Id; post.IsTopicStarter = true; try { _topicServic.Add(topic); _postSevice.Add(post); unitOfWork.Commit(); } catch (Exception ex) { LoggingService.Error(ex.Message); unitOfWork.Rollback(); } } else { //viewModel.Category = null; //No permission to create a Poll so show a message but create the topic //TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel //{ // Message = LocalizationService.GetResourceString("Errors.NoPermissionCatergory"), // MessageType = GenericMessages.info //}; ModelState.AddModelError(string.Empty, LocalizationService.GetResourceString("Errors.CatergoryMessage")); } } viewModel.Categories = _categoryService.GetBaseSelectListCategories(cats); return(View(viewModel)); } return(ErrorToHomePage(LocalizationService.GetResourceString("Errors.NoPermission"))); } }