コード例 #1
0
        public SingleBlogPostDTO UpdateBlogPost(string slug, BlogPostUpdateBindingModel blogPostModel)
        {
            BlogPost blogPost = _blogPostRepository.GetBlogPostBySlug(slug);

            if (!string.IsNullOrEmpty(blogPostModel.Title))
            {
                blogPost.Title = blogPostModel.Title;

                string generatedSlug = SlugifyManager.Slugify(blogPostModel.Title);
                blogPost.Slug = generatedSlug;

                if (_blogPostRepository.SlugExist(generatedSlug))
                {
                    List <string> existingSlugs = _blogPostRepository.Slugs();
                    blogPost.Slug = SlugifyManager.IncrementSlug(generatedSlug, existingSlugs);
                }
            }
            if (!string.IsNullOrEmpty(blogPostModel.Description))
            {
                blogPost.Description = blogPostModel.Description;
            }
            if (!string.IsNullOrEmpty(blogPostModel.Body))
            {
                blogPost.Body = blogPostModel.Body;
            }

            blogPost.UpdatedAt = DateTime.Now;

            var updatedBlogPost = _blogPostRepository.UpdateBlogPost(blogPost);
            SingleBlogPostDTO singleBlogPostDTO = new SingleBlogPostDTO();

            singleBlogPostDTO = GetBlogPostBySlug(updatedBlogPost.Slug);

            return(singleBlogPostDTO);
        }
コード例 #2
0
        public SingleBlogPostDTO CreateBlogPost(BlogPostCreateBindingModel blogPostModel)
        {
            BlogPost blogPost = new BlogPost();
            //List<string> tags = new List<string>();
            List <Tag> tags = new List <Tag>();

            blogPost.Title       = blogPostModel.Title;
            blogPost.Description = blogPostModel.Description;
            blogPost.Body        = blogPostModel.Body;
            blogPost.CreatedAt   = DateTime.Now;

            string slug = SlugifyManager.Slugify(blogPostModel.Title);

            blogPost.Slug = slug;

            if (_blogPostRepository.SlugExist(slug))
            {
                List <string> existingSlugs = _blogPostRepository.Slugs();
                blogPost.Slug = SlugifyManager.IncrementSlug(slug, existingSlugs);
            }

            blogPost.Tags = tags;

            if (blogPostModel.TagList != null)
            {
                foreach (var singleTag in blogPostModel.TagList)
                {
                    Tag tag = new Tag();
                    tag = _tagRepository.GetTagByTitle(singleTag);
                    //tags.Add(tag);
                    //blogPost.Tags = tags;
                    blogPost.Tags.Add(tag);
                }
            }
            var createdBlogPost = _blogPostRepository.CreateBlogPost(blogPost);
            SingleBlogPostDTO singleBlogPostDTO = new SingleBlogPostDTO();

            singleBlogPostDTO = GetBlogPostBySlug(createdBlogPost.Slug);

            return(singleBlogPostDTO);
        }