public string AddPost(BlogMlExtendedPost extPost)
{
if (!Security.IsAdministrator)
{
throw new InvalidOperationException("BlogImporter.AddPost: Wrong credentials");
}
using (var p = new Post())
{
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 (extPost.Categories != null && extPost.Categories.Count > 0)
p.Categories.AddRange(extPost.Categories);
if(extPost.Tags != null && extPost.Tags.Count > 0)
p.Tags.AddRange(extPost.Tags);
// skip if post with this url already exists
var s = PostUrl(p.Slug, p.DateCreated);
var list = Post.Posts.FindAll(ps => ps.RelativeLink == s);
if (list.Count > 0)
{
return string.Empty;
}
if(extPost.Comments != null && extPost.Comments.Count > 0)
{
foreach (var comment in extPost.Comments)
{
p.ImportComment(comment);
}
}
p.Import();
return p.Id.ToString();
}
}