Пример #1
0
        /// <summary>
        /// Preview
        /// </summary>
        /// <param name="post"></param>
        /// <returns></returns>
        public async Task <JsonResult> OnPostPreviewAsync([FromBody] PostVM post)
        {
            // prep blog post
            List <Tag> tags = new List <Tag>();

            foreach (var title in post.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(post.CategoryId),
                CreatedOn = GetCreatedOn(post.PostDate),
                Tags      = tags,
                Slug      = post.Slug.IsNullOrEmpty() ? "untitled" : post.Slug,
                Excerpt   = post.Excerpt,
                Title     = post.Title.IsNullOrEmpty() ? "Untitled" : post.Title,
                Body      = post.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}"));
        }
Пример #2
0
        /// <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] PostVM post)
        {
            var blogPost = new BlogPost
            {
                UserId     = Convert.ToInt32(_userManager.GetUserId(HttpContext.User)),
                CategoryId = post.CategoryId,
                CreatedOn  = GetCreatedOn(post.PostDate),
                TagTitles  = post.Tags,
                Slug       = post.Slug,
                Excerpt    = post.Excerpt,
                Title      = post.Title,
                Body       = post.Body,
                Status     = EPostStatus.Published,
            };

            if (post.Id <= 0)
            {
                blogPost = await _blogSvc.CreateAsync(blogPost);
            }
            else
            {
                blogPost.Id = post.Id;
                blogPost    = await _blogSvc.UpdateAsync(blogPost);
            }

            return(new JsonResult(GetPostAbsoluteUrl(blogPost)));
        }
Пример #3
0
        /// <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] PostVM post)
        {
            var blogPost = new BlogPost
            {
                UserId     = Convert.ToInt32(_userManager.GetUserId(HttpContext.User)),
                CategoryId = post.CategoryId,
                CreatedOn  = GetCreatedOn(post.PostDate),
                TagTitles  = post.Tags,
                Slug       = post.Slug,
                Excerpt    = post.Excerpt,
                Title      = post.Title,
                Body       = post.Body,
                Status     = EPostStatus.Draft,
            };

            if (post.Id <= 0)
            {
                blogPost = await _blogSvc.CreateAsync(blogPost);
            }
            else
            {
                blogPost.Id = post.Id;
                blogPost    = await _blogSvc.UpdateAsync(blogPost);
            }

            var postVM = new PostVM
            {
                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.UpdatedOnDisplay : "",
            };

            return(new JsonResult(postVM));
        }
Пример #4
0
        // -------------------------------------------------------------------- public methods

        /// <summary>
        /// GET to return <see cref="ComposeVM"/> to initialize the page.
        /// </summary>
        /// <remarks>
        /// NOTE: the parameter cannot be named "page".
        /// </remarks>
        /// <param name="postId">0 for a new post or an existing post id</param>
        /// <returns></returns>
        public async Task OnGetAsync(int postId)
        {
            // theme
            var coreSettings = await _settingSvc.GetSettingsAsync <CoreSettings>();

            Theme = coreSettings.Theme;

            // post
            PostVM postVM;

            if (postId > 0) // existing post
            {
                var post = await _blogSvc.GetAsync(postId);

                postVM = new PostVM
                {
                    Id         = post.Id,
                    Title      = post.Title,
                    Body       = post.Body,
                    PostDate   = post.CreatedOn.ToString(DATE_FORMAT),
                    Slug       = post.Slug,
                    Excerpt    = post.Excerpt,
                    CategoryId = post.CategoryId ?? 1,
                    Tags       = post.TagTitles,
                    Published  = post.Status == EPostStatus.Published,
                    IsDraft    = post.Status == EPostStatus.Draft,
                    DraftDate  = post.UpdatedOn.HasValue ? post.UpdatedOn.Value.ToString(DATE_FORMAT) : "",
                };
            }
            else // new post
            {
                var date         = Util.ConvertTime(DateTimeOffset.UtcNow, coreSettings.TimeZoneId).ToString(DATE_FORMAT);
                var blogSettings = await _settingSvc.GetSettingsAsync <BlogSettings>();

                postVM = new PostVM
                {
                    Title      = "",
                    Body       = "",
                    PostDate   = date,
                    CategoryId = blogSettings.DefaultCategoryId,
                    Tags       = new List <string>(),
                    Published  = false,
                    IsDraft    = false,
                };
            }
            PostJson = JsonConvert.SerializeObject(postVM);

            // cats
            var categories = await _catSvc.GetAllAsync();

            var allCats = from c in categories
                          select new CatVM
            {
                Value = c.Id,
                Text  = c.Title,
            };

            CatsJson = JsonConvert.SerializeObject(allCats);

            // tags
            var tags = await _tagSvc.GetAllAsync();

            var allTags = tags.Select(t => t.Title).ToArray();

            TagsJson = JsonConvert.SerializeObject(allTags);
        }