public IActionResult CreatePost(PostViewModel post) { if (!ModelState.IsValid) { return(View(post)); } try { Entry entry = _mapper.Map <Entry>(post); entry.Initialize(); entry.Author = "admin"; //TODO: Need to integrate with context security entry.Language = "en-us"; //TODO: We inject this fron http context? entry.Latitude = null; entry.Longitude = null; EntrySaveState sts = _blogManager.CreateEntry(entry); if (sts != EntrySaveState.Added) { ModelState.AddModelError("", "Failed to create blog post"); return(View(post)); } } catch (Exception e) { RedirectToAction("Error"); } return(View("Views/BlogPost/EditPost.cshtml", post)); }
public IActionResult CreatePost(PostViewModel post, string submit) { NBR.Entry entry = null; modelViewCreator.AddAllLanguages(post); if (submit == Constants.BlogPostAddCategoryAction) { return(HandleNewCategory(post)); } if (submit == Constants.UploadImageAction) { return(HandleImageUpload(post)); } ValidatePostName(post); if (!ModelState.IsValid) { return(View(post)); } if (!string.IsNullOrWhiteSpace(post.NewCategory)) { ModelState.AddModelError(nameof(post.NewCategory), $"Please click 'Add' to add the category, \"{post.NewCategory}\" or clear the text before continuing"); return(View(post)); } try { entry = mapper.Map <NBR.Entry>(post); entry.Initialize(); entry.Author = httpContextAccessor.HttpContext.User.Identity.Name; entry.Language = post.Language; entry.Latitude = null; entry.Longitude = null; var sts = blogManager.CreateEntry(entry); if (sts != NBR.EntrySaveState.Added) { post.EntryId = entry.EntryId; ModelState.AddModelError("", "Failed to create blog post. Please check Logs for more details."); return(View(post)); } } catch (Exception ex) { logger.LogError(new EventDataItem(EventCodes.Error, null, "Blog post create failed: {0}", ex.Message)); ModelState.AddModelError("", "Failed to edit blog post. Please check Logs for more details."); } if (entry != null) { logger.LogInformation(new EventDataItem(EventCodes.EntryAdded, null, "Blog post created: {0}", entry.Title)); } BreakSiteCache(); return(View("views/blogpost/editPost.cshtml", post)); }
public IActionResult CreatePost(PostViewModel post, string submit) { post.Languages = GetAlllanguages(); if (submit == Constants.BlogPostAddCategoryAction) { return(HandleNewCategory(post)); } if (submit == Constants.UploadImageAction) { return(HandleImageUpload(post)); } if (!ModelState.IsValid) { return(View(post)); } if (!string.IsNullOrWhiteSpace(post.NewCategory)) { ModelState.AddModelError(nameof(post.NewCategory) , $"Please click 'Add' to add the category, \"{post.NewCategory}\" or clear the text before continuing"); return(View(post)); } try { Entry entry = _mapper.Map <Entry>(post); entry.Initialize(); entry.Author = _httpContextAccessor.HttpContext.User.Identity.Name; entry.Language = post.Language; entry.Latitude = null; entry.Longitude = null; EntrySaveState sts = _blogManager.CreateEntry(entry); if (sts != EntrySaveState.Added) { ModelState.AddModelError("", "Failed to create blog post"); return(View(post)); } } catch (Exception e) { RedirectToAction("Error"); } return(View("views/blogpost/editPost.cshtml", post)); }