Apr 18, 2013

Nearing the end of my initial migration now in Part 6, I dove into Comment Listing, Adding New Comments and then emailing users a new comment was entered. (Other Posts: Part 1, Part 2, Part 3, Part 4 and Part 5).
In Part 5, I imported the Comments, but wasn't doing anything but showing the Comment Count in the Post Title. In this post I will begin with what I did to display the comments.
First off I added a new column to my PostComments to handle those Comments that were not approved or are pending approval (thinking about spam bots in particular). After adding that new column, I created a Stored Procedure to return the Comments for a given Post, some (maybe most) might find creating a Stored Procedure to simply return one Table is unnecessary, but I find it helps keep my C# much cleaner by adding that layer between my SQL Database and my C# code.
[sql]
CREATE PROCEDURE [dbo].[getPostCommentsSP]
(@PostID INT)
AS
SELECT
dbo.PostComments.Modified,
dbo.PostComments.Body,
dbo.PostComments.Name
FROM dbo.PostComments
WHERE dbo.PostComments.PostID = @PostID AND
dbo.PostComments.IsApproved = 1 AND
dbo.PostComments.Active = 1
[/sql]
I proceeded to add a new function in my PostFactory to return the converted List collection:

Since the main listings do not display the Comments, just the count, it was necessary to have a unique ActionResult.
That being said, I did reuse my PartialView I created the other night, only adding to it:

Because the Comments are otherwise null I can reuse the PartialView.
After adding in all of the CSS Styles:
[caption id="attachment_2039" align="aligncenter" width="300"]in MVC4 Site" width="300" height="165" class="size-medium wp-image-2039" /> Comments Listing in MVC4 Site[/caption]
Next on the list of things I wanted to accomplish is adding a form below the Comments Listing.
Adding a pretty basic form for web developers is pretty trivial, however here is the code I am using:

A feature of WordPress I realized I enjoyed was the fact it emailed me when a new comment was entered in the system. So I figured I would add the same functionality to my MVC app. One thing I should note, this is far from ideal code. Think of a larger site, with hundreds or thousands of comments from users. The user would have to wait until all of the emails were sent and then return the user to the post they added their comment to. A better approach would be to offload this potentially long running task to a Windows Service - a feature I will be adding shortly.

So what is next? Implementing the WCF Service previously mentioned and the Windows Service mentioned above. This will allow me to easily create Windows Phone 8, Windows 8 Store apps or heck even a command line version if there was demand. More to come...