public SingleBlogPostDTO GetBlogPostBySlug(string slug) { SingleBlogPostDTO singleBlogPostDTO = new SingleBlogPostDTO(); List <string> list = new List <string>(); var blogPost = _blogPostRepository.GetBlogPostBySlug(slug); if (blogPost != null) { BlogPostDTO blogPostDTO = new BlogPostDTO(); blogPostDTO.Title = blogPost.Title; blogPostDTO.Description = blogPost.Description; blogPostDTO.Body = blogPost.Body; blogPostDTO.Slug = blogPost.Slug; blogPostDTO.CreatedAt = blogPost.CreatedAt; blogPostDTO.UpdatedAt = blogPost.UpdatedAt; foreach (var singleTag in blogPost.Tags) { list.Add(singleTag.Title); } string[] tags = list.ToArray(); blogPostDTO.TagList = tags; singleBlogPostDTO.BlogPost = blogPostDTO; } return(singleBlogPostDTO); }
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); }
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); }