Skip to content

Instantly share code, notes, and snippets.

@mikeedwards83
Created February 3, 2013 17:45
Show Gist options
  • Select an option

  • Save mikeedwards83/4702772 to your computer and use it in GitHub Desktop.

Select an option

Save mikeedwards83/4702772 to your computer and use it in GitHub Desktop.
public class CommentController : SitecoreController
{
ISitecoreContext _context;
ISitecoreService _master;
/// <summary>
/// This constructor can be used with dependency injection or unit testing
/// </summary>
/// <param name="context"></param>
public CommentController(ISitecoreContext context, ISitecoreService master)
{
_context = context;
_master = master;
}
[HttpGet]
public override ActionResult Index()
{
var model = _context.CreateClass<CommentPage>(false, false, PageContext.Current.Item);
return View(model);
}
[HttpPost]
public ActionResult Index(Comment comment)
{
var webModel = _context.CreateClass<CommentPage>(false, false, PageContext.Current.Item);
if (ModelState.IsValid && comment != null)
{
var masterModel = _master.GetItem<CommentPage>(webModel.Id);
if (masterModel.CommentFolder == null)
{
CommentFolder folder = new CommentFolder();
folder.Name = "Comments";
using (new SecurityDisabler())
{
_context.Create(masterModel, folder);
}
masterModel.CommentFolder = folder;
}
using (new SecurityDisabler())
{
comment.Name = DateTime.Now.ToString("yyyyMMddhhmmss");
//create the comment in the master database
_master.Create(masterModel.CommentFolder, comment);
webModel.CommentAdded = true;
}
}
return View(webModel);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment