From eea8cf9aac714c9b9f25968db284cb8368342289 Mon Sep 17 00:00:00 2001 From: Dimitar Byalkov Date: Thu, 4 May 2023 21:10:00 +0200 Subject: [PATCH] Web and Desktop - comment threads --- .gitignore | 1 + .../HouseData/Managers/AnnouncementManager.cs | 4 +- .../HouseData/Managers/CommentManager.cs | 42 +++- .../HouseData/Models/Comment.cs | 4 + .../Repositories/AnnouncementRepository.cs | 6 +- .../Repositories/CommentRepository.cs | 118 +++++++++- .../WebApp/Pages/Announcements.cshtml | 2 + .../WinForms/AnnouncementForm.Designer.cs | 78 ++++++- .../WinForms/AnnouncementForm.cs | 79 ++++++- .../WinForms/CommentForm.Designer.cs | 215 ++++++++++++++++++ StudentHouseDashboard/WinForms/CommentForm.cs | 160 +++++++++++++ .../WinForms/CommentForm.resx | 60 +++++ StudentHouseDashboard/WinForms/Dashboard.cs | 45 +++- 13 files changed, 786 insertions(+), 28 deletions(-) create mode 100644 StudentHouseDashboard/WinForms/CommentForm.Designer.cs create mode 100644 StudentHouseDashboard/WinForms/CommentForm.cs create mode 100644 StudentHouseDashboard/WinForms/CommentForm.resx diff --git a/.gitignore b/.gitignore index 8a30d25..de3da87 100644 --- a/.gitignore +++ b/.gitignore @@ -396,3 +396,4 @@ FodyWeavers.xsd # JetBrains Rider *.sln.iml +/StudentHouseDashboard/.idea/.idea.StudentHouseDashboard/.idea diff --git a/StudentHouseDashboard/HouseData/Managers/AnnouncementManager.cs b/StudentHouseDashboard/HouseData/Managers/AnnouncementManager.cs index 3acab31..8bb1ac1 100644 --- a/StudentHouseDashboard/HouseData/Managers/AnnouncementManager.cs +++ b/StudentHouseDashboard/HouseData/Managers/AnnouncementManager.cs @@ -28,9 +28,9 @@ namespace StudentHouseDashboard.Managers { return announcementRepository.CreateAnnouncement(title, description, author, publishDate, isImportant, isSticky); } - public bool UpdateAnnouncement(int id, string title, string description, User author, DateTime publishDate, bool isImportant, bool isSticky) + public bool UpdateAnnouncement(int id, string title, string description, bool isImportant, bool isSticky) { - return announcementRepository.UpdateAnnouncement(id, title, description, author, publishDate, isImportant, isSticky); + return announcementRepository.UpdateAnnouncement(id, title, description, isImportant, isSticky); } public bool DeleteAnnouncement(int id) { diff --git a/StudentHouseDashboard/HouseData/Managers/CommentManager.cs b/StudentHouseDashboard/HouseData/Managers/CommentManager.cs index 5429680..59fe596 100644 --- a/StudentHouseDashboard/HouseData/Managers/CommentManager.cs +++ b/StudentHouseDashboard/HouseData/Managers/CommentManager.cs @@ -1,6 +1,40 @@ -namespace StudentHouseDashboard.Managers; +using StudentHouseDashboard.Models; +using StudentHouseDashboard.Repositories; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; -public class CommentManager +namespace StudentHouseDashboard.Managers { - //TODO: CRUD -} \ No newline at end of file + public class CommentManager + { + private CommentRepository commentRepository; + public CommentManager() + { + commentRepository = new CommentRepository(); + } + + public void CreateCommentToAnnouncement(User author, string description, string title, DateTime publishDate, int announcementId) + { + commentRepository.CreateCommentOnAnnouncement(author, description, title, publishDate, announcementId); + } + public void CreateResponseToComment(User author, string description, string title, DateTime publishDate, int commentId) + { + commentRepository.CreateResponseOnComment(author, description, title, publishDate, commentId); + } + public void UpdateComment(int id, string description) + { + commentRepository.UpdateComment(id, description); + } + public void DeleteCommentOnAnnouncement(int commentId, int announcementId) + { + commentRepository.DeleteCommentOnAnnouncement(commentId, announcementId); + } + public void DeleteResponseOnComment(int responseId, int commentId) + { + commentRepository.DeleteResponseOnComment(responseId, commentId); + } + } +} diff --git a/StudentHouseDashboard/HouseData/Models/Comment.cs b/StudentHouseDashboard/HouseData/Models/Comment.cs index 1116add..bec9a88 100644 --- a/StudentHouseDashboard/HouseData/Models/Comment.cs +++ b/StudentHouseDashboard/HouseData/Models/Comment.cs @@ -22,5 +22,9 @@ namespace StudentHouseDashboard.Models { throw new NotImplementedException(); } + public override string ToString() + { + return $"{Author.Name} ({PublishDate.ToString("g")}) - {Description.PadRight(100).Trim()}"; + } } } \ No newline at end of file diff --git a/StudentHouseDashboard/HouseData/Repositories/AnnouncementRepository.cs b/StudentHouseDashboard/HouseData/Repositories/AnnouncementRepository.cs index 80468f1..ebcf7ee 100644 --- a/StudentHouseDashboard/HouseData/Repositories/AnnouncementRepository.cs +++ b/StudentHouseDashboard/HouseData/Repositories/AnnouncementRepository.cs @@ -90,18 +90,16 @@ namespace StudentHouseDashboard.Repositories else return false; } } - public bool UpdateAnnouncement(int id, string title, string description, User author, DateTime publishDate, bool isImportant, bool isSticky) + public bool UpdateAnnouncement(int id, string title, string description, bool isImportant, bool isSticky) { using (SqlConnection conn = SqlConnectionHelper.CreateConnection()) { string sql = "UPDATE Announcements " + - "SET Author = @author, Description = @desc, Title = @title, PublishDate = @date, IsImportant = @important, IsSticky = @sticky " + + "SET Description = @desc, Title = @title, IsImportant = @important, IsSticky = @sticky " + "WHERE Id = @id;"; SqlCommand cmd = new SqlCommand(sql, conn); - cmd.Parameters.AddWithValue("@author", author.ID); cmd.Parameters.AddWithValue("@desc", description); cmd.Parameters.AddWithValue("@title", title); - cmd.Parameters.AddWithValue("@date", publishDate); cmd.Parameters.AddWithValue("@important", isImportant); cmd.Parameters.AddWithValue("@sticky", isSticky); cmd.Parameters.AddWithValue("@id", id); diff --git a/StudentHouseDashboard/HouseData/Repositories/CommentRepository.cs b/StudentHouseDashboard/HouseData/Repositories/CommentRepository.cs index 5b954d2..e7739ea 100644 --- a/StudentHouseDashboard/HouseData/Repositories/CommentRepository.cs +++ b/StudentHouseDashboard/HouseData/Repositories/CommentRepository.cs @@ -1,3 +1,4 @@ +using System.ComponentModel.Design; using System.Data.SqlClient; using StudentHouseDashboard.Models; @@ -7,7 +8,7 @@ public class CommentRepository { public CommentRepository() { - + } public List GetAllCommentsOnAnnouncement(int announcementId) @@ -65,4 +66,119 @@ public class CommentRepository return responses; } + + public Comment GetCommentById(int id) + { + 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 Comments c " + + "INNER JOIN Users u ON u.ID = c.Author " + + "WHERE c.ID = @commentID;"; + SqlCommand sqlCommand = new SqlCommand(sql, connection); + sqlCommand.Parameters.AddWithValue("@commentID", id); + var reader = sqlCommand.ExecuteReader(); + 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); + return newComment; + } + } + + public void UpdateComment(int id, string description) + { + using (SqlConnection connection = SqlConnectionHelper.CreateConnection()) + { + string sql = "UPDATE Comments " + + "SET Description = @desc " + + "WHERE Id = @id;"; + SqlCommand cmd = new SqlCommand(sql, connection); + cmd.Parameters.AddWithValue("@desc", description); + cmd.Parameters.AddWithValue("@id", id); + int writer = cmd.ExecuteNonQuery(); + } + } + + private Comment CreateComment(User author, string description, string title, DateTime publishDate) + { + using (SqlConnection connection = SqlConnectionHelper.CreateConnection()) + { + string sql = "INSERT INTO Comments (Author, Description, Title, PublishDate) " + + "VALUES (@author, @desc, @title, @date); " + + "SELECT SCOPE_IDENTITY();"; + SqlCommand cmd = new SqlCommand(sql, connection); + cmd.Parameters.AddWithValue("@author", author.ID); + cmd.Parameters.AddWithValue("@desc", description); + cmd.Parameters.AddWithValue("@title", title); + cmd.Parameters.AddWithValue("@date", publishDate); + return GetCommentById(Convert.ToInt32(cmd.ExecuteScalar())); + } + } + public void CreateCommentOnAnnouncement(User author, string description, string title, DateTime publishDate, int announcementId) + { + Comment comment = CreateComment(author, description, title, publishDate); + using (SqlConnection connection = SqlConnectionHelper.CreateConnection()) + { + string sql = "INSERT INTO AnnouncementsComments (AnnouncementID, CommentID) VALUES (@announcementID, @commentID);"; + SqlCommand cmd = new SqlCommand(sql, connection); + cmd.Parameters.AddWithValue("@announcementID", announcementId); + cmd.Parameters.AddWithValue("@commentID", comment.ID); + int writer = cmd.ExecuteNonQuery(); + } + } + public void CreateResponseOnComment(User author, string description, string title, DateTime publishDate, int commentId) + { + Comment response = CreateComment(author, description, title, publishDate); + using (SqlConnection connection = SqlConnectionHelper.CreateConnection()) + { + string sql = "INSERT INTO CommentsResponses (CommentID, ResponseID) VALUES (@commentID, @responseID);"; + SqlCommand cmd = new SqlCommand(sql, connection); + cmd.Parameters.AddWithValue("@commentID", commentId); + cmd.Parameters.AddWithValue("@responseID", response.ID); + int writer = cmd.ExecuteNonQuery(); + } + } + + private void DeleteComment(int id) + { + using (SqlConnection connection = SqlConnectionHelper.CreateConnection()) + { + string sql = "DELETE FROM Comments " + + "WHERE Id = @id;"; + SqlCommand cmd = new SqlCommand(sql, connection); + cmd.Parameters.AddWithValue("@id", id); + int writer = cmd.ExecuteNonQuery(); + } + } + + public void DeleteCommentOnAnnouncement(int commentId, int announcementId) + { + using (SqlConnection connection = SqlConnectionHelper.CreateConnection()) + { + string sql = "DELETE FROM AnnouncementsComments " + + "WHERE CommentID = @commentId AND AnnouncementID = @announcementId"; + SqlCommand cmd = new SqlCommand(sql, connection); + cmd.Parameters.AddWithValue("@commentId", commentId); + cmd.Parameters.AddWithValue("@announcementId", announcementId); + int writer = cmd.ExecuteNonQuery(); + } + DeleteComment(commentId); + } + public void DeleteResponseOnComment(int responseId, int commentId) + { + using (SqlConnection connection = SqlConnectionHelper.CreateConnection()) + { + string sql = "DELETE FROM AnnouncementsComments " + + "WHERE CommentID = @commentId AND ResponseID = @responseId"; + SqlCommand cmd = new SqlCommand(sql, connection); + cmd.Parameters.AddWithValue("@commentId", commentId); + cmd.Parameters.AddWithValue("@responseId", responseId); + int writer = cmd.ExecuteNonQuery(); + } + DeleteComment(commentId); + } } \ No newline at end of file diff --git a/StudentHouseDashboard/WebApp/Pages/Announcements.cshtml b/StudentHouseDashboard/WebApp/Pages/Announcements.cshtml index 8f32e68..b610f09 100644 --- a/StudentHouseDashboard/WebApp/Pages/Announcements.cshtml +++ b/StudentHouseDashboard/WebApp/Pages/Announcements.cshtml @@ -8,6 +8,8 @@ int currentPage = @Convert.ToInt32(ViewData["page"]); } + + @foreach (Announcement announcement in announcements) {
diff --git a/StudentHouseDashboard/WinForms/AnnouncementForm.Designer.cs b/StudentHouseDashboard/WinForms/AnnouncementForm.Designer.cs index 0f69d1b..6a4463b 100644 --- a/StudentHouseDashboard/WinForms/AnnouncementForm.Designer.cs +++ b/StudentHouseDashboard/WinForms/AnnouncementForm.Designer.cs @@ -38,6 +38,12 @@ ckbSticky = new CheckBox(); dtpPublishDate = new DateTimePicker(); lblAuthor = new Label(); + lblComments = new Label(); + listBox1 = new ListBox(); + btnViewComment = new Button(); + btnEditComment = new Button(); + btnDeleteComment = new Button(); + btnCreateComment = new Button(); SuspendLayout(); // // lblTitle @@ -128,11 +134,75 @@ lblAuthor.TabIndex = 10; lblAuthor.Text = "Created by: "; // + // lblComments + // + lblComments.AutoSize = true; + lblComments.Location = new Point(12, 209); + lblComments.Name = "lblComments"; + lblComments.Size = new Size(66, 15); + lblComments.TabIndex = 11; + lblComments.Text = "Comments"; + // + // listBox1 + // + listBox1.FormattingEnabled = true; + listBox1.ItemHeight = 15; + listBox1.Location = new Point(12, 227); + listBox1.Name = "listBox1"; + listBox1.Size = new Size(399, 154); + listBox1.TabIndex = 12; + // + // btnViewComment + // + btnViewComment.Location = new Point(424, 271); + btnViewComment.Name = "btnViewComment"; + btnViewComment.Size = new Size(75, 23); + btnViewComment.TabIndex = 13; + btnViewComment.Text = "View"; + btnViewComment.UseVisualStyleBackColor = true; + btnViewComment.Click += btnViewComment_Click; + // + // btnEditComment + // + btnEditComment.Location = new Point(424, 300); + btnEditComment.Name = "btnEditComment"; + btnEditComment.Size = new Size(75, 23); + btnEditComment.TabIndex = 14; + btnEditComment.Text = "Edit"; + btnEditComment.UseVisualStyleBackColor = true; + btnEditComment.Click += btnEditComment_Click; + // + // btnDeleteComment + // + btnDeleteComment.Location = new Point(424, 329); + btnDeleteComment.Name = "btnDeleteComment"; + btnDeleteComment.Size = new Size(75, 23); + btnDeleteComment.TabIndex = 15; + btnDeleteComment.Text = "Delete"; + btnDeleteComment.UseVisualStyleBackColor = true; + btnDeleteComment.Click += btnDeleteComment_Click; + // + // btnCreateComment + // + btnCreateComment.Location = new Point(424, 358); + btnCreateComment.Name = "btnCreateComment"; + btnCreateComment.Size = new Size(75, 23); + btnCreateComment.TabIndex = 16; + btnCreateComment.Text = "New"; + btnCreateComment.UseVisualStyleBackColor = true; + btnCreateComment.Click += btnCreateComment_Click; + // // AnnouncementForm // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(511, 216); + ClientSize = new Size(511, 400); + Controls.Add(btnCreateComment); + Controls.Add(btnDeleteComment); + Controls.Add(btnEditComment); + Controls.Add(btnViewComment); + Controls.Add(listBox1); + Controls.Add(lblComments); Controls.Add(lblAuthor); Controls.Add(dtpPublishDate); Controls.Add(ckbSticky); @@ -161,5 +231,11 @@ private CheckBox ckbSticky; private DateTimePicker dtpPublishDate; private Label lblAuthor; + private Label lblComments; + private ListBox listBox1; + private Button btnViewComment; + private Button btnEditComment; + private Button btnDeleteComment; + private Button btnCreateComment; } } \ No newline at end of file diff --git a/StudentHouseDashboard/WinForms/AnnouncementForm.cs b/StudentHouseDashboard/WinForms/AnnouncementForm.cs index 3c8d4df..db610e0 100644 --- a/StudentHouseDashboard/WinForms/AnnouncementForm.cs +++ b/StudentHouseDashboard/WinForms/AnnouncementForm.cs @@ -16,17 +16,18 @@ namespace WinForms { Announcement announcement; User currentUser; - public AnnouncementForm(Announcement? announcement, bool readOnly, User? currentUser) + public AnnouncementForm(Announcement? announcement, bool readOnly, User currentUser) { InitializeComponent(); this.announcement = announcement; this.currentUser = currentUser; + + dtpPublishDate.Enabled = false; if (readOnly) { btnSave.Enabled = false; tbTitle.Enabled = false; tbDescription.Enabled = false; - dtpPublishDate.Enabled = false; ckbImportant.Enabled = false; ckbSticky.Enabled = false; } @@ -38,12 +39,19 @@ namespace WinForms dtpPublishDate.Value = announcement.PublishDate; ckbImportant.Checked = announcement.IsImportant; ckbSticky.Checked = announcement.IsSticky; + RefreshComments(); + } + else + { + btnCreateComment.Enabled = false; + btnDeleteComment.Enabled = false; + btnEditComment.Enabled = false; + btnViewComment.Enabled = false; } if (currentUser != null) { lblAuthor.Text = $"Created by: {currentUser.Name}"; } - } private void btnSave_Click(object sender, EventArgs e) @@ -61,9 +69,72 @@ namespace WinForms } else { - announcementManager.UpdateAnnouncement(announcement.ID, tbTitle.Text, tbDescription.Text, currentUser, dtpPublishDate.Value, ckbImportant.Checked, ckbSticky.Checked); + announcementManager.UpdateAnnouncement(announcement.ID, tbTitle.Text, tbDescription.Text, ckbImportant.Checked, ckbSticky.Checked); } this.DialogResult = DialogResult.OK; } + + private void btnViewComment_Click(object sender, EventArgs e) + { + if (listBox1.SelectedIndex == -1) + { + MessageBox.Show("Please select an item from the list"); + } + else + { + CommentForm form = new CommentForm((Comment)listBox1.SelectedItem, true, currentUser); + form.ShowDialog(); + RefreshComments(); + } + } + + private void btnEditComment_Click(object sender, EventArgs e) + { + if (listBox1.SelectedIndex == -1) + { + MessageBox.Show("Please select an item from the list"); + } + else + { + CommentForm form = new CommentForm((Comment)listBox1.SelectedItem, false, currentUser); + form.ShowDialog(); + RefreshComments(); + } + } + + private void btnDeleteComment_Click(object sender, EventArgs e) + { + if (listBox1.SelectedIndex == -1) + { + MessageBox.Show("Please select an item from the list"); + } + else + { + Comment currentComment = (Comment)listBox1.SelectedItem; + if (MessageBox.Show($"Are you sure you want to delete\n{currentComment.Title}\nCreated at {currentComment.PublishDate.ToString("g")} by {currentComment.Author.Name}", + "Delete announcement", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + CommentManager commentManager = new CommentManager(); + commentManager.DeleteCommentOnAnnouncement(currentComment.ID, announcement.ID); + } + RefreshComments(); + } + } + + private void btnCreateComment_Click(object sender, EventArgs e) + { + CommentForm form = new CommentForm(null, false, currentUser, true, announcement.ID); + form.ShowDialog(); + RefreshComments(); + } + + private void RefreshComments() + { + listBox1.Items.Clear(); + foreach (var item in announcement.Comments) + { + listBox1.Items.Add(item); + } + } } } diff --git a/StudentHouseDashboard/WinForms/CommentForm.Designer.cs b/StudentHouseDashboard/WinForms/CommentForm.Designer.cs new file mode 100644 index 0000000..824264a --- /dev/null +++ b/StudentHouseDashboard/WinForms/CommentForm.Designer.cs @@ -0,0 +1,215 @@ +namespace WinForms +{ + partial class CommentForm + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + btnCreateComment = new Button(); + btnDeleteComment = new Button(); + btnEditComment = new Button(); + btnViewComment = new Button(); + listBox1 = new ListBox(); + lblComments = new Label(); + lblAuthor = new Label(); + dtpPublishDate = new DateTimePicker(); + btnSave = new Button(); + lblPublishDate = new Label(); + tbDescription = new TextBox(); + lblDescription = new Label(); + tbTitle = new TextBox(); + lblTitle = new Label(); + SuspendLayout(); + // + // btnCreateComment + // + btnCreateComment.Location = new Point(424, 358); + btnCreateComment.Name = "btnCreateComment"; + btnCreateComment.Size = new Size(75, 23); + btnCreateComment.TabIndex = 30; + btnCreateComment.Text = "New"; + btnCreateComment.UseVisualStyleBackColor = true; + btnCreateComment.Click += btnCreateComment_Click; + // + // btnDeleteComment + // + btnDeleteComment.Location = new Point(424, 329); + btnDeleteComment.Name = "btnDeleteComment"; + btnDeleteComment.Size = new Size(75, 23); + btnDeleteComment.TabIndex = 29; + btnDeleteComment.Text = "Delete"; + btnDeleteComment.UseVisualStyleBackColor = true; + btnDeleteComment.Click += btnDeleteComment_Click; + // + // btnEditComment + // + btnEditComment.Location = new Point(424, 300); + btnEditComment.Name = "btnEditComment"; + btnEditComment.Size = new Size(75, 23); + btnEditComment.TabIndex = 28; + btnEditComment.Text = "Edit"; + btnEditComment.UseVisualStyleBackColor = true; + btnEditComment.Click += btnEditComment_Click; + // + // btnViewComment + // + btnViewComment.Location = new Point(424, 271); + btnViewComment.Name = "btnViewComment"; + btnViewComment.Size = new Size(75, 23); + btnViewComment.TabIndex = 27; + btnViewComment.Text = "View"; + btnViewComment.UseVisualStyleBackColor = true; + btnViewComment.Click += btnViewComment_Click; + // + // listBox1 + // + listBox1.FormattingEnabled = true; + listBox1.ItemHeight = 15; + listBox1.Location = new Point(12, 227); + listBox1.Name = "listBox1"; + listBox1.Size = new Size(399, 154); + listBox1.TabIndex = 26; + // + // lblComments + // + lblComments.AutoSize = true; + lblComments.Location = new Point(12, 209); + lblComments.Name = "lblComments"; + lblComments.Size = new Size(66, 15); + lblComments.TabIndex = 25; + lblComments.Text = "Comments"; + // + // lblAuthor + // + lblAuthor.AutoSize = true; + lblAuthor.Location = new Point(12, 184); + lblAuthor.Name = "lblAuthor"; + lblAuthor.Size = new Size(70, 15); + lblAuthor.TabIndex = 24; + lblAuthor.Text = "Created by: "; + // + // dtpPublishDate + // + dtpPublishDate.Location = new Point(94, 153); + dtpPublishDate.Name = "dtpPublishDate"; + dtpPublishDate.Size = new Size(200, 23); + dtpPublishDate.TabIndex = 23; + // + // btnSave + // + btnSave.Location = new Point(424, 180); + btnSave.Name = "btnSave"; + btnSave.Size = new Size(75, 23); + btnSave.TabIndex = 22; + btnSave.Text = "Save changes"; + btnSave.UseVisualStyleBackColor = true; + btnSave.Click += btnSave_Click; + // + // lblPublishDate + // + lblPublishDate.AutoSize = true; + lblPublishDate.Location = new Point(12, 159); + lblPublishDate.Name = "lblPublishDate"; + lblPublishDate.Size = new Size(76, 15); + lblPublishDate.TabIndex = 21; + lblPublishDate.Text = "Publish Date:"; + // + // tbDescription + // + tbDescription.Location = new Point(94, 35); + tbDescription.Multiline = true; + tbDescription.Name = "tbDescription"; + tbDescription.Size = new Size(405, 112); + tbDescription.TabIndex = 20; + // + // lblDescription + // + lblDescription.AutoSize = true; + lblDescription.Location = new Point(12, 38); + lblDescription.Name = "lblDescription"; + lblDescription.Size = new Size(70, 15); + lblDescription.TabIndex = 19; + lblDescription.Text = "Description:"; + // + // tbTitle + // + tbTitle.Location = new Point(94, 6); + tbTitle.Name = "tbTitle"; + tbTitle.Size = new Size(405, 23); + tbTitle.TabIndex = 18; + // + // lblTitle + // + lblTitle.AutoSize = true; + lblTitle.Location = new Point(12, 9); + lblTitle.Name = "lblTitle"; + lblTitle.Size = new Size(32, 15); + lblTitle.TabIndex = 17; + lblTitle.Text = "Title:"; + // + // CommentForm + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(517, 395); + Controls.Add(btnCreateComment); + Controls.Add(btnDeleteComment); + Controls.Add(btnEditComment); + Controls.Add(btnViewComment); + Controls.Add(listBox1); + Controls.Add(lblComments); + Controls.Add(lblAuthor); + Controls.Add(dtpPublishDate); + Controls.Add(btnSave); + Controls.Add(lblPublishDate); + Controls.Add(tbDescription); + Controls.Add(lblDescription); + Controls.Add(tbTitle); + Controls.Add(lblTitle); + Name = "CommentForm"; + Text = "Comment"; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Button btnCreateComment; + private Button btnDeleteComment; + private Button btnEditComment; + private Button btnViewComment; + private ListBox listBox1; + private Label lblComments; + private Label lblAuthor; + private DateTimePicker dtpPublishDate; + private Button btnSave; + private Label lblPublishDate; + private TextBox tbDescription; + private Label lblDescription; + private TextBox tbTitle; + private Label lblTitle; + } +} \ No newline at end of file diff --git a/StudentHouseDashboard/WinForms/CommentForm.cs b/StudentHouseDashboard/WinForms/CommentForm.cs new file mode 100644 index 0000000..97e289c --- /dev/null +++ b/StudentHouseDashboard/WinForms/CommentForm.cs @@ -0,0 +1,160 @@ +using StudentHouseDashboard.Managers; +using StudentHouseDashboard.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace WinForms +{ + public partial class CommentForm : Form + { + Comment? comment; + User currentUser; + bool announcementResponse; + int parentId; + public CommentForm(Comment? comment, bool readOnly, User currentUser) + { + InitializeComponent(); + this.comment = comment; + this.currentUser = currentUser; + + // restriction: no need to create events in the past + dtpPublishDate.Enabled = false; + + if (comment != null) + { + tbTitle.Text = comment.Title; + lblAuthor.Text = $"Created by: {comment.Author.Name}"; + tbDescription.Text = comment.Description; + dtpPublishDate.Value = comment.PublishDate; + RefreshComments(); + } + else + { + btnCreateComment.Enabled = false; + btnDeleteComment.Enabled = false; + btnEditComment.Enabled = false; + btnViewComment.Enabled = false; + } + + if (comment != null && currentUser != null) + { + if (currentUser.ID != comment.Author.ID) + { + // restriction: only edit personal comments + readOnly = true; + } + } + + if (readOnly) + { + btnSave.Enabled = false; + tbTitle.Enabled = false; + tbDescription.Enabled = false; + } + + if (currentUser != null) + { + lblAuthor.Text = $"Created by: {currentUser.Name}"; + } + } + public CommentForm(Comment? comment, bool readOnly, User? currentUser, bool announcementResponse, int parentId) : this(comment, readOnly, currentUser) + { + this.announcementResponse = announcementResponse; + this.parentId = parentId; + } + private void btnSave_Click(object sender, EventArgs e) + { + CommentManager commentManager = new CommentManager(); + if (string.IsNullOrEmpty(tbTitle.Text) || string.IsNullOrEmpty(tbDescription.Text)) + { + MessageBox.Show("Please enter a title and comment text"); + return; + } + if (this.comment == null) + { + if (announcementResponse) + { + commentManager.CreateCommentToAnnouncement(currentUser, tbDescription.Text, tbTitle.Text, dtpPublishDate.Value, parentId); + } + else + { + commentManager.CreateResponseToComment(currentUser, tbDescription.Text, tbTitle.Text, dtpPublishDate.Value, parentId); + } + } + else + { + commentManager.UpdateComment(comment.ID, tbDescription.Text); + } + } + + private void btnViewComment_Click(object sender, EventArgs e) + { + if (listBox1.SelectedIndex == -1) + { + MessageBox.Show("Please select an item from the list"); + } + else + { + CommentForm form = new CommentForm((Comment)listBox1.SelectedItem, true, currentUser); + form.ShowDialog(); + RefreshComments(); + } + + } + + private void btnEditComment_Click(object sender, EventArgs e) + { + if (listBox1.SelectedIndex == -1) + { + MessageBox.Show("Please select an item from the list"); + } + else + { + CommentForm form = new CommentForm((Comment)listBox1.SelectedItem, false, currentUser); + form.ShowDialog(); + RefreshComments(); + } + } + + private void btnDeleteComment_Click(object sender, EventArgs e) + { + if (listBox1.SelectedIndex == -1) + { + MessageBox.Show("Please select an item from the list"); + } + else + { + Comment currentResponse = (Comment)listBox1.SelectedItem; + if (MessageBox.Show($"Are you sure you want to delete\n{currentResponse.Title}\nCreated at {currentResponse.PublishDate.ToString("g")} by {currentResponse.Author.Name}", + "Delete announcement", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + CommentManager commentManager = new CommentManager(); + commentManager.DeleteResponseOnComment(currentResponse.ID, comment.ID); + } + RefreshComments(); + } + } + + private void btnCreateComment_Click(object sender, EventArgs e) + { + CommentForm form = new CommentForm(null, false, currentUser, false, comment.ID); + form.ShowDialog(); + RefreshComments(); + } + private void RefreshComments() + { + listBox1.Items.Clear(); + foreach (var item in comment.Responses) + { + listBox1.Items.Add(item); + } + } + } +} diff --git a/StudentHouseDashboard/WinForms/CommentForm.resx b/StudentHouseDashboard/WinForms/CommentForm.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/StudentHouseDashboard/WinForms/CommentForm.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/StudentHouseDashboard/WinForms/Dashboard.cs b/StudentHouseDashboard/WinForms/Dashboard.cs index b5a88ad..49361d3 100644 --- a/StudentHouseDashboard/WinForms/Dashboard.cs +++ b/StudentHouseDashboard/WinForms/Dashboard.cs @@ -114,28 +114,49 @@ namespace WinForms private void btnDeleteAnnouncement_Click(object sender, EventArgs e) { - Announcement currentAnnouncement = (Announcement)lbAnnouncements.SelectedItem; - if (MessageBox.Show($"Are you sure you want to delete\n{currentAnnouncement.Title}\nCreated at {currentAnnouncement.PublishDate.ToString("g")} by {currentAnnouncement.Author.Name}", - "Delete announcement", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + if (lbAnnouncements.SelectedIndex == -1) { - AnnouncementManager announcementManager = new AnnouncementManager(); - announcementManager.DeleteAnnouncement(currentAnnouncement.ID); + MessageBox.Show("Please select an item from the list"); + } + else + { + Announcement currentAnnouncement = (Announcement)lbAnnouncements.SelectedItem; + if (MessageBox.Show($"Are you sure you want to delete\n{currentAnnouncement.Title}\nCreated at {currentAnnouncement.PublishDate.ToString("g")} by {currentAnnouncement.Author.Name}", + "Delete announcement", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + AnnouncementManager announcementManager = new AnnouncementManager(); + announcementManager.DeleteAnnouncement(currentAnnouncement.ID); + } + RefreshLists(); } - RefreshLists(); } private void btnEditAnnouncement_Click(object sender, EventArgs e) { - AnnouncementForm announcementForm = new AnnouncementForm((Announcement)lbAnnouncements.SelectedItem, false, null); - announcementForm.ShowDialog(); - RefreshLists(); + if (lbAnnouncements.SelectedIndex == -1) + { + MessageBox.Show("Please select an item from the list"); + } + else + { + AnnouncementForm announcementForm = new AnnouncementForm((Announcement)lbAnnouncements.SelectedItem, false, user); + announcementForm.ShowDialog(); + RefreshLists(); + } } private void btnViewAnnouncement_Click(object sender, EventArgs e) { - AnnouncementForm announcementForm = new AnnouncementForm((Announcement)lbAnnouncements.SelectedItem, true, null); - announcementForm.ShowDialog(); - RefreshLists(); + if (lbAnnouncements.SelectedIndex == -1) + { + MessageBox.Show("Please select an item from the list"); + } + else + { + AnnouncementForm announcementForm = new AnnouncementForm((Announcement)lbAnnouncements.SelectedItem, true, user); + announcementForm.ShowDialog(); + RefreshLists(); + } } } }