From 913bb39ba8d77de5271218393cd3a83c4ac54db4 Mon Sep 17 00:00:00 2001 From: Dimitar Byalkov Date: Mon, 1 May 2023 06:04:47 +0200 Subject: [PATCH] Support for nested comments --- .../HouseData/Managers/CommentManager.cs | 6 +++ .../HouseData/Models/Comment.cs | 4 +- .../Repositories/CommentRepository.cs | 44 ++++++++++++++++--- .../WebApp/Pages/Announcement.cshtml | 43 +++++++++++++++--- 4 files changed, 82 insertions(+), 15 deletions(-) create mode 100644 StudentHouseDashboard/HouseData/Managers/CommentManager.cs diff --git a/StudentHouseDashboard/HouseData/Managers/CommentManager.cs b/StudentHouseDashboard/HouseData/Managers/CommentManager.cs new file mode 100644 index 0000000..5429680 --- /dev/null +++ b/StudentHouseDashboard/HouseData/Managers/CommentManager.cs @@ -0,0 +1,6 @@ +namespace StudentHouseDashboard.Managers; + +public class CommentManager +{ + //TODO: CRUD +} \ No newline at end of file diff --git a/StudentHouseDashboard/HouseData/Models/Comment.cs b/StudentHouseDashboard/HouseData/Models/Comment.cs index ca76440..1116add 100644 --- a/StudentHouseDashboard/HouseData/Models/Comment.cs +++ b/StudentHouseDashboard/HouseData/Models/Comment.cs @@ -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(); } - + + public List Responses { get; set; } public void DownVote() { throw new NotImplementedException(); diff --git a/StudentHouseDashboard/HouseData/Repositories/CommentRepository.cs b/StudentHouseDashboard/HouseData/Repositories/CommentRepository.cs index 68a7257..5b954d2 100644 --- a/StudentHouseDashboard/HouseData/Repositories/CommentRepository.cs +++ b/StudentHouseDashboard/HouseData/Repositories/CommentRepository.cs @@ -10,7 +10,7 @@ public class CommentRepository } - public List GetAllCommentsOnAnnouncement(int announcementID) + public List GetAllCommentsOnAnnouncement(int announcementId) { List comments = new List(); 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"], - new User((int)reader["UserID"], reader["UserName"].ToString(), - reader["Password"].ToString(), (UserRole)reader["Role"]), - reader["Description"].ToString(), reader["Title"].ToString(), - (DateTime)reader["PublishDate"])); + 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); + comments.Add(newComment); } } return comments; } + + public List GetAllCommentResponses(int commentId) + { + List responses = new List(); + 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; + } } \ No newline at end of file diff --git a/StudentHouseDashboard/WebApp/Pages/Announcement.cshtml b/StudentHouseDashboard/WebApp/Pages/Announcement.cshtml index 883ee11..254ad15 100644 --- a/StudentHouseDashboard/WebApp/Pages/Announcement.cshtml +++ b/StudentHouseDashboard/WebApp/Pages/Announcement.cshtml @@ -1,14 +1,20 @@ @page @using StudentHouseDashboard.Models; +@using System.Globalization @model WebApp.Pages.AnnouncementModel @{ Announcement announcement = (Announcement)ViewData["announcement"]; ViewData["Title"] = $"{announcement.Title}"; }

@announcement.Title

-

Published @announcement.PublishDate.ToString("g") by @announcement.Author.Name - @(announcement.IsImportant ? "Important" : "") @(announcement.IsSticky ? "Sticky" : "")

-
+

+ Published @announcement.PublishDate.ToString("g") by @announcement.Author.Name + @Html.Raw((announcement.Author.Role == UserRole.ADMIN || announcement.Author.Role == UserRole.MANAGER) + ? $"({CultureInfo.CurrentCulture.TextInfo.ToTitleCase(announcement.Author.Role.ToString().ToLower())})" + : "") + @(announcement.IsImportant ? "Important" : "") @(announcement.IsSticky ? "Sticky" : "") +

+

@Html.Raw(announcement.Description.Replace(Environment.NewLine, "
"))


Comments

@@ -20,11 +26,34 @@ else { foreach (Comment comment in announcement.Comments) { -
+ DisplayComment(comment, 0); + } +} + +@{ + void DisplayComment(Comment comment, int level) + { +
+ @for (int i = 0; i < level; i++) + { + + } +
-
@comment.Author.Name
-

@Html.Raw(comment.Description.Replace(Environment.NewLine, "
"))

- Respond +
@comment.Author.Name @Html.Raw((comment.Author.Role == UserRole.ADMIN || comment.Author.Role == UserRole.MANAGER) ? $"({CultureInfo.CurrentCulture.TextInfo.ToTitleCase(comment.Author.Role.ToString().ToLower())})" : "")
+
@comment.PublishDate.ToString("g")
+

@Html.Raw(comment.Description.Replace(Environment.NewLine, "
"))

+ Like + Dislike + Reply +
+ @if (comment.Responses.Count != 0) + { + foreach (var response in comment.Responses) + { + DisplayComment(response, level + 1); + } + }
}