Web and Desktop - comment threads
This commit is contained in:
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,5 +22,9 @@ namespace StudentHouseDashboard.Models
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{Author.Name} ({PublishDate.ToString("g")}) - {Description.PadRight(100).Trim()}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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<Comment> 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user