Modifying blogs

To modify a blog, you must perform the following:

  1. Get the blog.

    Get an instance of the blog that you want to modify.

  2. Modify the blog.

    Set the properties of the blog to the new values.

  3. Save the blog.

    Save the changes that have been made to the blog.

The example below shows you how to modify a blog by its ID.

Modifying a blog by its ID

The following code modifies a blog by its ID.

Native API

private void ModifyBlogNativeAPI(Guid blogId, string newTitle)
{
    BlogsManager blogsManager = BlogsManager.GetManager();
  
    //Get the blog.
    Blog blog = blogsManager.GetBlogs().Where(b => b.Id == blogId).FirstOrDefault();
  
    if (blog != null)
    {
  
        //Modify the blog.
        blog.Title = newTitle;
        blog.LastModified = DateTime.UtcNow;
        blog.Urls.Clear();
        blog.UrlName = Regex.Replace(newTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
  
        //Save the changes.
        blogsManager.SaveChanges();
    }
}

First, you get an instance of the BlogsManager class. Then, you get the blog with the specified ID. You update the values of the properties of the blog. In this example you modify the title of the blog and its URL. Finally, you call SaveChanges to persist the changes.

Fluent API

private void ModifyBlogFluentAPI(Guid blogId, string newTitle)
{
    var count = 0;
  
    App.WorkWith().Blogs().Where(b => b.Id == blogId).Count(out count);
  
    if (count > 0)
    {
        //Get the blog.
        App.WorkWith().Blog(blogId).Do(blog =>
        {
            //Modify the blog.
            blog.Title = newTitle;
            blog.LastModified = DateTime.UtcNow;
            blog.Urls.Clear();
            blog.UrlName = Regex.Replace(newTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
        }).SaveChanges();
    }
}

First, you check whether a blog with the specified ID exists. Then, you get the singular facade of the blog with the specified ID. To modify the blog, you call the Do method of the facade. In this example you update the title of the blog and its URL. Finally, to save the changes, you call the SaveChanges method of the facade.

See Also

Related topics:

Feedback

How useful is this article?

Tell us more

Submit
Your message was successfully sent.

We appreciate your feedback.

Your message could not be sent.

OK