Support for nested comments
This commit is contained in:
@@ -0,0 +1,6 @@
|
|||||||
|
namespace StudentHouseDashboard.Managers;
|
||||||
|
|
||||||
|
public class CommentManager
|
||||||
|
{
|
||||||
|
//TODO: CRUD
|
||||||
|
}
|
@@ -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)
|
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()
|
public void DownVote()
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
|
@@ -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>();
|
List<Comment> comments = new List<Comment>();
|
||||||
using (SqlConnection connection = SqlConnectionHelper.CreateConnection())
|
using (SqlConnection connection = SqlConnectionHelper.CreateConnection())
|
||||||
@@ -21,18 +21,48 @@ public class CommentRepository
|
|||||||
"INNER JOIN Users u ON u.ID = c.Author " +
|
"INNER JOIN Users u ON u.ID = c.Author " +
|
||||||
"WHERE ac.AnnouncementID = @announcementID";
|
"WHERE ac.AnnouncementID = @announcementID";
|
||||||
SqlCommand sqlCommand = new SqlCommand(sql, connection);
|
SqlCommand sqlCommand = new SqlCommand(sql, connection);
|
||||||
sqlCommand.Parameters.AddWithValue("@announcementID", announcementID);
|
sqlCommand.Parameters.AddWithValue("@announcementID", announcementId);
|
||||||
var reader = sqlCommand.ExecuteReader();
|
var reader = sqlCommand.ExecuteReader();
|
||||||
while (reader.Read())
|
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(),
|
new User((int)reader["UserID"], reader["UserName"].ToString(),
|
||||||
reader["Password"].ToString(), (UserRole)reader["Role"]),
|
reader["Password"].ToString(), (UserRole)reader["Role"]),
|
||||||
reader["Description"].ToString(), reader["Title"].ToString(),
|
reader["Description"].ToString(), reader["Title"].ToString(),
|
||||||
(DateTime)reader["PublishDate"]));
|
(DateTime)reader["PublishDate"]);
|
||||||
|
newComment.Responses = GetAllCommentResponses(newComment.ID);
|
||||||
|
comments.Add(newComment);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return comments;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
@@ -1,14 +1,20 @@
|
|||||||
@page
|
@page
|
||||||
@using StudentHouseDashboard.Models;
|
@using StudentHouseDashboard.Models;
|
||||||
|
@using System.Globalization
|
||||||
@model WebApp.Pages.AnnouncementModel
|
@model WebApp.Pages.AnnouncementModel
|
||||||
@{
|
@{
|
||||||
Announcement announcement = (Announcement)ViewData["announcement"];
|
Announcement announcement = (Announcement)ViewData["announcement"];
|
||||||
ViewData["Title"] = $"{announcement.Title}";
|
ViewData["Title"] = $"{announcement.Title}";
|
||||||
}
|
}
|
||||||
<h1>@announcement.Title</h1>
|
<h1>@announcement.Title</h1>
|
||||||
<p>Published @announcement.PublishDate.ToString("g") by @announcement.Author.Name
|
<p>
|
||||||
@(announcement.IsImportant ? "Important" : "") @(announcement.IsSticky ? "Sticky" : "")</p>
|
Published @announcement.PublishDate.ToString("g") by @announcement.Author.Name
|
||||||
<hr />
|
@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>
|
<p>@Html.Raw(announcement.Description.Replace(Environment.NewLine, "<br />"))</p>
|
||||||
<br/>
|
<br/>
|
||||||
<h3>Comments</h3>
|
<h3>Comments</h3>
|
||||||
@@ -20,11 +26,34 @@ else
|
|||||||
{
|
{
|
||||||
foreach (Comment comment in announcement.Comments)
|
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">
|
<div class="card-body">
|
||||||
<h5 class="card-title">@comment.Author.Name</h5>
|
<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>
|
||||||
<p class="card-text">@Html.Raw(comment.Description.Replace(Environment.NewLine, "<br />"))</p>
|
<h6>@comment.PublishDate.ToString("g")</h6>
|
||||||
<a href="#">Respond</a>
|
<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>
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user