private async Task <PostViewModel> GetPost(string slug) { var post = await _postManager.FindBySlugAsync(slug); if (post != null) { var model = post.ToModel(); await PreparePostViewModel(model, post); return(model); } return(null); }
private async Task <bool> AddPost(BlogMlExtendedPost extPost) { var p = new Post(); p.Title = extPost.BlogPost.Title; p.CreationTime = extPost.BlogPost.DateCreated; p.PublishedTime = extPost.BlogPost.DateCreated; p.LastModificationTime = extPost.BlogPost.DateModified; p.Content = extPost.BlogPost.Content.UncodedText; p.Description = extPost.BlogPost.Excerpt.UncodedText; p.IsDraft = !extPost.BlogPost.Approved; p.ViewsCount = GetValue(extPost.BlogPost.Views); if (extPost.BlogPost.HasExcerpt) { p.Description = extPost.BlogPost.Excerpt.UncodedText; } if (!string.IsNullOrEmpty(extPost.PostUrl)) { // looking for a Slug with patterns such as: // /some-slug.aspx // /some-slug.html // /some-slug // Match slugMatch = Regex.Match(extPost.PostUrl, @"/([^/\.]+)(?:$|\.[\w]{1,10}$)", RegexOptions.IgnoreCase); if (slugMatch.Success) { p.Slug = slugMatch.Groups[1].Value.Trim(); } } if (string.IsNullOrEmpty(p.Slug)) { p.Slug = p.GetSeName(); } // skip if exists if (await _postManager.FindBySlugAsync(p.Slug) != null) { return(false); } if (extPost.BlogPost.Authors != null && extPost.BlogPost.Authors.Count > 0) { // p.UserId = extPost.BlogPost.Authors[0].Ref; p.UserId = _author.Id; } if (extPost.Categories != null && extPost.Categories.Count > 0) { foreach (var item in extPost.Categories) { p.Categories.Add(new PostCategory() { CategoryId = item.Id, PostId = item.Id }); } } if (extPost.Tags != null && extPost.Tags.Count > 0) { foreach (var item in extPost.Tags) { var tag = await _tagsManager.CreateOrUpdateAsync(item); p.Tags.Add(new PostTags() { PostId = p.Id, TagsId = tag.Id }); } } await _postManager.CreateAsync(p); if (extPost.Comments != null && extPost.Comments.Count > 0) { foreach (var comment in extPost.Comments) { comment.PostId = p.Id; try { await _commentManager.CreateAsync(comment); } catch (Exception) { } } p.CommentsCount = extPost.Comments.Count; await _postManager.UpdateAsync(p); } return(true); }