private string editComment(int blogID, int commentID, string type)
{
string content = null, select = null;
string commentDisplayName = (loggedIn) ? displayName : null;
string commentText = null;
string userEmail = (showEmail) ? email : null;
DateTime commentDate = DateTime.Now;
SqlDataReader dr;
if (commentID == 0 && type == "postComment")
{
//replying to a blog, so spit out the blog as a reference
content += showBlogs(blogID);
}
else if(type == "postComment")
{
//replying to a comment, so spit out the comment as a reference
select = @"SELECT displayName, parentComment, name, commentText, commentDate,
CASE " +
"WHEN showEmail = 1 THEN ' (<a href="mailto: ' + email + '">' + email + '</a>)' " +
"ELSE '' " +
@"END AS 'email', a.userID
FROM comments a
LEFT JOIN users b
on a.userID = b.userID
WHERE commentID = " + commentID + @"
ORDER BY commentDate ASC";
dr = query(select);
if (dr.HasRows)
{
dr.Read();
content = @"<div style='width: 72%;'><div class='commentContainer'>
<div class='comment'>
<div class='header'>";
content += (Convert.ToInt16(dr["userID"]) > 0) ? dr["displayName"] : dr["name"];
content += dr["email"] + " @ " + Convert.ToDateTime(dr["commentDate"]).ToString("hh:mmtt ddMMMyyyy") +
@"</div>
<div class='text'>" + Convert.ToString(dr["commentText"]).Replace("n", "<br>") + @"</div>
</div>
</div></div>";
dr.Dispose();
}
else
{
dr.Dispose();
return "<div class='error'>Invalid Comment Specified</div>";
}
}
if (type == "editComment")
{
//edit existing comment
select = @"SELECT displayName, name, commentText, commentDate,
CASE " +
"WHEN showEmail = 1 THEN ' (<a href="mailto: ' + email + '">' + email + '</a>)' " +
"ELSE '' " +
@"END AS 'email', a.userID, blogID
FROM comments a
LEFT JOIN users b
on a.userID = b.userID
WHERE commentID=" + commentID + @"
ORDER BY commentDate ASC";
dr = query(select);
if (dr.HasRows)
{
dr.Read();
commentDisplayName = (Convert.ToInt16(dr["userID"]) == 0) ? Convert.ToString(dr["name"]) : Convert.ToString(dr["displayName"]);
commentText = Convert.ToString(dr["commentText"]);
commentDate = Convert.ToDateTime(dr["commentDate"]);
userEmail = Convert.ToString(dr["email"]);
blogID = Convert.ToInt16(dr["blogID"]);
dr.Dispose();
}
else
{
dr.Dispose();
return "<div class='error'>Error: Invalid Comment Specified</div>";
}
}
content += "<div style='width: 72%;'>";
content += @"<form method='POST' action='default.aspx' style='display: inline;'><div class='commentContainer'>
<div class='comment'>
<div class='header'>";
content += (loggedIn) ? commentDisplayName : "Name: <input name='name' value='" + commentDisplayName + "'>";
content += userEmail + " @ " + commentDate.ToString("hh:mmtt ddMMMyyyy");
content += @"</div>
<div class='text'><textarea name='commentText' style='width: 100%;' rows=5>" + commentText + @"</textarea></div>
<div class='reply'>
<input type='hidden' name='option' value='saveComment'>
<input type='hidden' name='blogID' value='" + blogID + @"'>";
if(commentID > 0){ content += "<input type='hidden' name='commentID' value='" + commentID + "'>";}
content += @"<input type='submit' value='Save'>
</form>
<form action='default.aspx' method='GET' style='display: inline;'>
<input type='hidden' name='option' value='comments'>
<input type='hidden' name='blogID' value='" + blogID + @"'>
<input type='submit' value='Cancel'>
</form>
</div>
</div>";
content += "</div>";
return content;
}
{
string content = null, select = null;
string commentDisplayName = (loggedIn) ? displayName : null;
string commentText = null;
string userEmail = (showEmail) ? email : null;
DateTime commentDate = DateTime.Now;
SqlDataReader dr;
if (commentID == 0 && type == "postComment")
{
//replying to a blog, so spit out the blog as a reference
content += showBlogs(blogID);
}
else if(type == "postComment")
{
//replying to a comment, so spit out the comment as a reference
select = @"SELECT displayName, parentComment, name, commentText, commentDate,
CASE " +
"WHEN showEmail = 1 THEN ' (<a href="mailto: ' + email + '">' + email + '</a>)' " +
"ELSE '' " +
@"END AS 'email', a.userID
FROM comments a
LEFT JOIN users b
on a.userID = b.userID
WHERE commentID = " + commentID + @"
ORDER BY commentDate ASC";
dr = query(select);
if (dr.HasRows)
{
dr.Read();
content = @"<div style='width: 72%;'><div class='commentContainer'>
<div class='comment'>
<div class='header'>";
content += (Convert.ToInt16(dr["userID"]) > 0) ? dr["displayName"] : dr["name"];
content += dr["email"] + " @ " + Convert.ToDateTime(dr["commentDate"]).ToString("hh:mmtt ddMMMyyyy") +
@"</div>
<div class='text'>" + Convert.ToString(dr["commentText"]).Replace("n", "<br>") + @"</div>
</div>
</div></div>";
dr.Dispose();
}
else
{
dr.Dispose();
return "<div class='error'>Invalid Comment Specified</div>";
}
}
if (type == "editComment")
{
//edit existing comment
select = @"SELECT displayName, name, commentText, commentDate,
CASE " +
"WHEN showEmail = 1 THEN ' (<a href="mailto: ' + email + '">' + email + '</a>)' " +
"ELSE '' " +
@"END AS 'email', a.userID, blogID
FROM comments a
LEFT JOIN users b
on a.userID = b.userID
WHERE commentID=" + commentID + @"
ORDER BY commentDate ASC";
dr = query(select);
if (dr.HasRows)
{
dr.Read();
commentDisplayName = (Convert.ToInt16(dr["userID"]) == 0) ? Convert.ToString(dr["name"]) : Convert.ToString(dr["displayName"]);
commentText = Convert.ToString(dr["commentText"]);
commentDate = Convert.ToDateTime(dr["commentDate"]);
userEmail = Convert.ToString(dr["email"]);
blogID = Convert.ToInt16(dr["blogID"]);
dr.Dispose();
}
else
{
dr.Dispose();
return "<div class='error'>Error: Invalid Comment Specified</div>";
}
}
content += "<div style='width: 72%;'>";
content += @"<form method='POST' action='default.aspx' style='display: inline;'><div class='commentContainer'>
<div class='comment'>
<div class='header'>";
content += (loggedIn) ? commentDisplayName : "Name: <input name='name' value='" + commentDisplayName + "'>";
content += userEmail + " @ " + commentDate.ToString("hh:mmtt ddMMMyyyy");
content += @"</div>
<div class='text'><textarea name='commentText' style='width: 100%;' rows=5>" + commentText + @"</textarea></div>
<div class='reply'>
<input type='hidden' name='option' value='saveComment'>
<input type='hidden' name='blogID' value='" + blogID + @"'>";
if(commentID > 0){ content += "<input type='hidden' name='commentID' value='" + commentID + "'>";}
content += @"<input type='submit' value='Save'>
</form>
<form action='default.aspx' method='GET' style='display: inline;'>
<input type='hidden' name='option' value='comments'>
<input type='hidden' name='blogID' value='" + blogID + @"'>
<input type='submit' value='Cancel'>
</form>
</div>
</div>";
content += "</div>";
return content;
}
First we just initialize all our variables and set some to their default values. We then check to see if the commentID is 0 and the type is postComment. This means we're responding to the root blog post. In that case, we just use our showBlogs() method to spit out the blog the user is responding to. This lets them re-read it as they're writing their comment.
Next we just see if it's a response. Since we handled the commentID=0 case in the first if branch, we know this is a response to a comment. So next we retrieve the comment and spit it out for the user to reference. If this were a real application, I'd write a method that spits out comments, then call it from both here and from the showComments() method. However, I don't want to waste a lot of tutorial space rewriting code we've already written, so let's leave it like this for now. Perhaps we'll address this in the future.
After we address any postComment options, we check to see if this is an editComment request and, if it is, we retrieve all the comment details. Finally we get to the actual comment form. This we pre-populate with either the default values or the edited value, depending on what's appopriate.