Support for nested comments

This commit is contained in:
Dimitar Byalkov
2023-05-01 06:04:47 +02:00
parent 55a2319405
commit 913bb39ba8
4 changed files with 82 additions and 15 deletions

View File

@@ -0,0 +1,6 @@
namespace StudentHouseDashboard.Managers;
public class CommentManager
{
//TODO: CRUD
}

View File

@@ -9,8 +9,10 @@ namespace StudentHouseDashboard.Models
{
public Comment(int id, User author, string description, string title, DateTime publishDate) : base(id, author, description, title, publishDate)
{
Responses = new List<Comment>();
}
public List<Comment> Responses { get; set; }
public void DownVote()
{
throw new NotImplementedException();

View File

@@ -10,7 +10,7 @@ public class CommentRepository
}
public List<Comment> GetAllCommentsOnAnnouncement(int announcementID)
public List<Comment> GetAllCommentsOnAnnouncement(int announcementId)
{
List<Comment> comments = new List<Comment>();
using (SqlConnection connection = SqlConnectionHelper.CreateConnection())
@@ -21,18 +21,48 @@ public class CommentRepository
"INNER JOIN Users u ON u.ID = c.Author " +
"WHERE ac.AnnouncementID = @announcementID";
SqlCommand sqlCommand = new SqlCommand(sql, connection);
sqlCommand.Parameters.AddWithValue("@announcementID", announcementID);
sqlCommand.Parameters.AddWithValue("@announcementID", announcementId);
var reader = sqlCommand.ExecuteReader();
while (reader.Read())
{
comments.Add(new Comment((int)reader["ID"],
Comment newComment = new Comment((int)reader["ID"],
new User((int)reader["UserID"], reader["UserName"].ToString(),
reader["Password"].ToString(), (UserRole)reader["Role"]),
reader["Description"].ToString(), reader["Title"].ToString(),
(DateTime)reader["PublishDate"]));
(DateTime)reader["PublishDate"]);
newComment.Responses = GetAllCommentResponses(newComment.ID);
comments.Add(newComment);
}
}
return comments;
}
public List<Comment> GetAllCommentResponses(int commentId)
{
List<Comment> responses = new List<Comment>();
using (SqlConnection connection = SqlConnectionHelper.CreateConnection())
{
string sql = "SELECT c.ID, c.Author, c.Description, c.Title, c.PublishDate, " +
"u.ID UserID, u.Name UserName, u.Password, u.Role FROM CommentsResponses cr " +
"INNER JOIN Comments c ON c.ID = cr.ResponseID " +
"INNER JOIN Users u ON u.ID = c.Author " +
"WHERE cr.CommentID = @commentID";
SqlCommand sqlCommand = new SqlCommand(sql, connection);
sqlCommand.Parameters.AddWithValue("@commentID", commentId);
var reader = sqlCommand.ExecuteReader();
while (reader.Read())
{
Comment newComment = new Comment((int)reader["ID"],
new User((int)reader["UserID"], reader["UserName"].ToString(),
reader["Password"].ToString(), (UserRole)reader["Role"]),
reader["Description"].ToString(), reader["Title"].ToString(),
(DateTime)reader["PublishDate"]);
newComment.Responses = GetAllCommentResponses(newComment.ID);
responses.Add(newComment);
}
}
return responses;
}
}

View File

@@ -1,14 +1,20 @@
@page
@using StudentHouseDashboard.Models;
@using System.Globalization
@model WebApp.Pages.AnnouncementModel
@{
Announcement announcement = (Announcement)ViewData["announcement"];
ViewData["Title"] = $"{announcement.Title}";
}
<h1>@announcement.Title</h1>
<p>Published @announcement.PublishDate.ToString("g") by @announcement.Author.Name
@(announcement.IsImportant ? "Important" : "") @(announcement.IsSticky ? "Sticky" : "")</p>
<hr />
<p>
Published @announcement.PublishDate.ToString("g") by @announcement.Author.Name
@Html.Raw((announcement.Author.Role == UserRole.ADMIN || announcement.Author.Role == UserRole.MANAGER)
? $"<b>({CultureInfo.CurrentCulture.TextInfo.ToTitleCase(announcement.Author.Role.ToString().ToLower())})</b>"
: "")
@(announcement.IsImportant ? "Important" : "") @(announcement.IsSticky ? "Sticky" : "")
</p>
<hr/>
<p>@Html.Raw(announcement.Description.Replace(Environment.NewLine, "<br />"))</p>
<br/>
<h3>Comments</h3>
@@ -20,11 +26,34 @@ else
{
foreach (Comment comment in announcement.Comments)
{
<div class="card">
DisplayComment(comment, 0);
}
}
@{
void DisplayComment(Comment comment, int level)
{
<div class="d-flex flex-row">
@for (int i = 0; i < level; i++)
{
<a class="me-3" href="#"></a>
}
<div class="card flex-fill">
<div class="card-body">
<h5 class="card-title">@comment.Author.Name</h5>
<p class="card-text">@Html.Raw(comment.Description.Replace(Environment.NewLine, "<br />"))</p>
<a href="#">Respond</a>
<h5 class="card-title">@comment.Author.Name @Html.Raw((comment.Author.Role == UserRole.ADMIN || comment.Author.Role == UserRole.MANAGER) ? $"<b>({CultureInfo.CurrentCulture.TextInfo.ToTitleCase(comment.Author.Role.ToString().ToLower())})</b>" : "")</h5>
<h6>@comment.PublishDate.ToString("g")</h6>
<p class="card-text">@Html.Raw(comment.Description.Replace(Environment.NewLine, "<br />"))</p>
<a class="btn btn-outline-success" href="#">Like</a>
<a class="btn btn-outline-danger" href="#">Dislike</a>
<a class="btn btn-outline-primary" href="#">Reply</a>
</div>
@if (comment.Responses.Count != 0)
{
foreach (var response in comment.Responses)
{
DisplayComment(response, level + 1);
}
}
</div>
</div>
}