/// <summary> /// Preview /// </summary> /// <param name="postIM"></param> /// <returns></returns> public async Task <JsonResult> OnPostPreviewAsync([FromBody] BlogPostIM postIM) { // prep blog post List <Tag> tags = new List <Tag>(); foreach (var title in postIM.Tags) // titles { tags.Add(await _tagSvc.GetByTitleAsync(title)); } var blogPost = new BlogPost { User = await _userManager.GetUserAsync(HttpContext.User), UserId = Convert.ToInt32(_userManager.GetUserId(HttpContext.User)), Category = await _catSvc.GetAsync(postIM.CategoryId), CreatedOn = BlogUtil.GetCreatedOn(postIM.PostDate), Tags = tags, Slug = postIM.Slug.IsNullOrEmpty() ? "untitled" : postIM.Slug, Excerpt = postIM.Excerpt, Title = postIM.Title.IsNullOrEmpty() ? "Untitled" : postIM.Title, Body = postIM.Body, }; // prep TempData var prevRelLink = BlogRoutes.GetPostPreviewRelativeLink(blogPost.CreatedOn, blogPost.Slug); TempData.Put(prevRelLink, blogPost); // return preview url return(new JsonResult($"{Request.Scheme}://{Request.Host}{prevRelLink}")); }
/// <summary> /// Ajax POST to publish a post. /// </summary> /// <returns> /// Absolute URL to the post. /// </returns> /// <remarks> /// The post could be new or previously published. /// </remarks> public async Task <JsonResult> OnPostPublishAsync([FromBody] BlogPostIM postIM) { var blogPost = new BlogPost { UserId = Convert.ToInt32(_userManager.GetUserId(HttpContext.User)), CategoryId = postIM.CategoryId, CreatedOn = BlogUtil.GetCreatedOn(postIM.PostDate), TagTitles = postIM.Tags, Slug = postIM.Slug, Excerpt = postIM.Excerpt, Title = postIM.Title, Body = postIM.Body, Status = EPostStatus.Published, }; if (postIM.Id <= 0) { blogPost = await _blogSvc.CreateAsync(blogPost); } else { blogPost.Id = postIM.Id; blogPost = await _blogSvc.UpdateAsync(blogPost); } return(new JsonResult(GetPostAbsoluteUrl(blogPost))); }
/// <summary> /// Updates an existing published post. /// </summary> /// <returns> /// Absolute URL to the post. /// </returns> public async Task <IActionResult> OnPostUpdateAsync([FromBody] PageIM pageIM) { try { var page = new Blog.Models.Page { Id = pageIM.Id, UserId = Convert.ToInt32(userManager.GetUserId(HttpContext.User)), ParentId = pageIM.ParentId, CreatedOn = BlogUtil.GetCreatedOn(pageIM.PostDate), Title = pageIM.Title, Body = pageIM.Body, BodyMark = pageIM.BodyMark, Excerpt = pageIM.Excerpt, Status = EPostStatus.Published, PageLayout = (byte)pageIM.PageLayout, }; page = await pageService.UpdateAsync(page); return(new JsonResult(GetPostAbsoluteUrl(page))); } catch (FanException ex) { return(BadRequest(ex.Message)); } }
/// <summary> /// Saves a page as draft. /// </summary> /// <returns> /// The updated <see cref="BlogPost"/>. /// </returns> /// <remarks> /// This is called by either Auto Save or user clicking on Save. /// </remarks> public async Task <IActionResult> OnPostSaveAsync([FromBody] PageIM pageIM) { try { // get page var page = new Blog.Models.Page { UserId = Convert.ToInt32(userManager.GetUserId(HttpContext.User)), ParentId = pageIM.ParentId, CreatedOn = BlogUtil.GetCreatedOn(pageIM.PostDate), Title = pageIM.Title, Body = pageIM.Body, BodyMark = pageIM.BodyMark, Excerpt = pageIM.Excerpt, Status = EPostStatus.Draft, PageLayout = (byte)pageIM.PageLayout, }; // create or update page if (pageIM.Id <= 0) { page = await pageService.CreateAsync(page); } else { page.Id = pageIM.Id; page = await pageService.UpdateAsync(page); } // return page var coreSettings = await settingService.GetSettingsAsync <CoreSettings>(); pageIM = new PageIM { Id = page.Id, Title = page.Title, BodyMark = page.BodyMark, Excerpt = page.Excerpt, PostDate = page.CreatedOn.ToString(DATE_FORMAT), ParentId = page.ParentId, Published = page.Status == EPostStatus.Published, IsDraft = page.Status == EPostStatus.Draft, DraftDate = page.UpdatedOn.HasValue ? page.UpdatedOn.Value.ToDisplayString(coreSettings.TimeZoneId) : "", PageLayout = (EPageLayout)page.PageLayout, }; return(new JsonResult(pageIM)); } catch (FanException ex) { return(BadRequest(ex.Message)); } }
/// <summary> /// Ajax POST to save a post as draft. /// </summary> /// <returns> /// The updated <see cref="BlogPost"/>. /// </returns> /// <remarks> /// This is called by either auto save or user clicking on Save. /// </remarks> public async Task <JsonResult> OnPostSaveAsync([FromBody] BlogPostIM postIM) { var blogPost = new BlogPost { UserId = Convert.ToInt32(_userManager.GetUserId(HttpContext.User)), CategoryId = postIM.CategoryId, CreatedOn = BlogUtil.GetCreatedOn(postIM.PostDate), TagTitles = postIM.Tags, Slug = postIM.Slug, Excerpt = postIM.Excerpt, Title = postIM.Title, Body = postIM.Body, Status = EPostStatus.Draft, }; if (postIM.Id <= 0) { blogPost = await _blogSvc.CreateAsync(blogPost); } else { blogPost.Id = postIM.Id; blogPost = await _blogSvc.UpdateAsync(blogPost); } var coreSettings = await _settingSvc.GetSettingsAsync <CoreSettings>(); var postVM = new BlogPostIM { Id = blogPost.Id, Title = blogPost.Title, Body = blogPost.Body, PostDate = blogPost.CreatedOn.ToString(DATE_FORMAT), Slug = blogPost.Slug, Excerpt = blogPost.Excerpt, CategoryId = blogPost.CategoryId ?? 1, Tags = blogPost.TagTitles, Published = blogPost.Status == EPostStatus.Published, IsDraft = blogPost.Status == EPostStatus.Draft, DraftDate = blogPost.UpdatedOn.HasValue ? blogPost.UpdatedOn.Value.ToDisplayString(coreSettings.TimeZoneId) : "", }; return(new JsonResult(postVM)); }