Simple CMS with Linq to SQL

Now we need to add a helper function that will return a ContentPage object from a FormCollection. This is so we can keep the Create ActionResult free of clutter.

private ContentPage GetPageFromCollection(int id, FormCollection collection)
{
	// Setup markdown
	Markdown m = new Markdown();

	// New page
	ContentPage content = new ContentPage();

	// Trigger date
	DateTime dt = DateTime.Now;

	// Basics
	content.ParentId = id;
	content.Title = Util.DefaultString(collection["title"], "No title");
	content.Description = Util.DefaultString(collection["description"], "");
	content.Author = Util.DefaultString(collection["author"], "Anonymous"); // Just for now
	content.AuthorId = 0; // Just for now

	// Page content
	content.Abstract = Util.DefaultString(collection["abstract"], "");
	content.BodyText = Util.DefaultString(collection["bodytext"], "");
	content.BodyHtml = m.Transform(content.BodyText);


	// Enable publishing
	content.Approved = Util.DefaultBool(collection["approved"], true);

	// Nested
	content.ViewCount = 0;
	content.CommentCount = 0;
	content.PageCount = 0;

	// Times
	content.CreatedDate = dt;
	content.LastModified = dt;
	content.PubDate = Util.DefaultDate(collection["pubdate"], dt);

	// Feedback
	content.EnableComments = Util.DefaultBool(collection["enablecomments"], true);
	content.AnonComments = Util.DefaultBool(collection["anoncomments"], true);
	content.Moderated = Util.DefaultBool(collection["moderated"], false);

	return content;
}

Let’s see how we can use it to create new pages.

Modify the [HttpPost] Create ActionResult to match the following…

[HttpPost]
public ActionResult Create(int? id, FormCollection collection)
{
	// Parent Id
	id = id ?? 0;

	using (SimpleCMSDataContext db = new SimpleCMSDataContext())
	{
		// Insert the new page
		db.ContentPages.InsertOnSubmit(GetPageFromCollection(id.Value, collection));

		if (id > 0)
		{
			// Update parent page count if this page has a parent
			ContentPage parent = (from p in db.ContentPages
				  where p.PageId == id
				  select p).Single();

			parent.PageCount = (from p in db.ContentPages
					where p.ParentId == id
					select p.PageId).Count();
		}

		db.SubmitChanges();
	}

	return RedirectToAction("Index", new { id = id.Value });
}

Well, everything that’s created will eventually be destroyed. So let’s edit the two ActionResults that will help us do just that…

public ActionResult Delete(int? id)
{
	if (id.HasValue)
	{
		ContentPage page;
		using (SimpleCMSDataContext db = new SimpleCMSDataContext())
		{
			page = (from p in db.ContentPages
					where p.PageId == id
					select p).Single();
		}
		ViewData.Model = page;
		return View();
	}
	return RedirectToAction("Index");
}


[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
	try
	{
		ContentPage page;
		using (SimpleCMSDataContext db = new SimpleCMSDataContext())
		{
			page = (from p in db.ContentPages
					where p.PageId == id
					select p).Single();

			db.ContentPages.DeleteOnSubmit(page);
			db.SubmitChanges();
		}
		return RedirectToAction("Index");
	}
	catch
	{
		return RedirectToAction("Index");
	}
}

You’re probably wondering, why I left out Editing. That’s because it involves some UI modifications… And we don’t even have a UI except for a Master Page. That will come a little later.

Adding pages is fine, but we also need a way to add new comments to our pages… The following two functions in our controller will help with that…

[HttpPost]
public ActionResult CreateComment(int? id, FormCollection collection)
{
	// Page Id
	id = id ?? 0;

	if (id > 0)
	{
		using (SimpleCMSDataContext db = new SimpleCMSDataContext())
		{
			// Get our page
			ContentPage content = (from p in db.ContentPages
						   where p.PageId == id
						   select p).Single();

			// If comments are enabled
			if (Util.DefaultBool(content.EnableComments, true))
			{
				// Insert the new comment
				db.ContentComments.InsertOnSubmit(GetCommentFromCollection(id.Value, collection));

				// Update the comment count
				content.CommentCount = Util.DefaultInt(content.CommentCount, 0) + 1;

				db.SubmitChanges();
			}
		}
	}

	return RedirectToAction("Read", new { id = id });
}


// This is just like our GetPagesFromCollection function, except for comment forms
private ContentComment GetCommentFromCollection(int id, FormCollection collection)
{
	// Setup markdown
	Markdown m = new Markdown();

	// New comment
	ContentComment comment = new ContentComment();

	// Trigger date
	DateTime dt = DateTime.Now;

	// Basics
	comment.PageId = id;
	comment.Author = Util.DefaultString(collection["author"], "Anonymous");
	comment.AuthorId = 0; // Just for now
	comment.AuthorIP = Util.GetUserIP();
	comment.CreatedDate = dt;
	comment.LastModified = dt;

	// We don't have an approval yet
	comment.Approved = true;
	comment.AuthorEmail = Util.DefaultString(collection["authoremail"], "");

	// Content
	comment.BodyText = Util.DefaultString(collection["bodytext"], "");
	comment.BodyHtml = m.Transform(comment.BodyText);

	return comment;
}

And on to destroying them once created…

public ActionResult DeleteComment(int? id)
{
	int pageid = 0;
	if (id.HasValue)
	{
		ContentComment comment;
		using (SimpleCMSDataContext db = new SimpleCMSDataContext())
		{
			comment = (from c in db.ContentComments
					where c.PageId == id
					select c).Single();

			pageid = comment.PageId;
		}
		ViewData.Model = comment;
		return View();
	}
	return RedirectToAction("Read", new { id = pageid });
}

[HttpPost]
public ActionResult DeleteComment(int id, FormCollection collection)
{
	try
	{
		int pageid = 0;
		ContentComment comment;
		using (SimpleCMSDataContext db = new SimpleCMSDataContext())
		{
			comment = (from c in db.ContentComments
				   where c.PageId == id
				   select c).Single();

			pageid = comment.PageId;
			db.ContentComments.DeleteOnSubmit(comment);
			db.SubmitChanges();
		}
		return RedirectToAction("Read", new { id = pageid });
	}
	catch
	{
		return View();
	}
}

So now we have the functionality to create, read and delete new content from out CMS. But we don’t have the ability to edit them yet. As mentioned above, editing involves making changes to the UI, which we will be looking at in our next installment on making a simple CMS.

Advertisement

8 thoughts on “Simple CMS with Linq to SQL

  1. Pingback: Simple CMS with Linq to SQL part II « This page intentionally left ugly

  2. Pingback: Forum Tables « This page intentionally left ugly

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s