public ActionResult Comment() { string conString = "Data Source=VKT-TAYLANA\\SQLEXPRESS;Initial Catalog=BlogWebSystem;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"; SqlConnection connection = new SqlConnection(conString); if (connection.State == ConnectionState.Closed) { connection.Open(); } string commentQuery = "Select CommentId,Comment.PostId,PostTitle,AudienceName,AudienceMail,AudienceMsg,CommentPublished From Comment INNER JOIN Post ON Comment.PostId = Post.PostId ORDER BY CommentPublished DESC"; SqlCommand list = new SqlCommand(commentQuery, connection); SqlDataReader reader = list.ExecuteReader(); List <Models.CommentModel> commentModel = new List <Models.CommentModel>(); Models.CommentModel item; while (reader.Read()) { item = new Models.CommentModel(); item.CommentId = Convert.ToInt32(reader["CommentId"]); item.PostId = Convert.ToInt32(reader["PostId"]); item.PostTitle = reader["PostTitle"].ToString(); item.AudienceName = reader["AudienceName"].ToString(); item.AudienceMail = reader["AudienceMail"].ToString(); item.AudienceMsg = reader["AudienceMsg"].ToString(); item.CommentPublished = reader["CommentPublished"].ToString(); commentModel.Add(item); } connection.Close(); reader.Close(); return(View(commentModel)); }
public Models.CommentsListModel GetCommentsForLaw(int lawID, string userID, Infrastructure.CommentOrder order) { using (var context = JavnaRasprava.WEB.DomainModels.ApplicationDbContext.Create()) { var results = new Models.CommentsListModel() { Comments = new List <Models.CommentModel>(), LawID = lawID }; var comments = context.LawComments .Where(x => x.LawID == lawID) .Include(x => x.ApplicationUser) .ToList(); var commentVotingResults = context.LawCommentLikes .Where(x => x.LawID == lawID) .GroupBy(x => new { x.LawCommentID, x.Vote }) .Select(g => new { CommentID = g.Key.LawCommentID, Vote = g.Key.Vote, Count = g.Count() }) .ToList(); var userVotes = context.LawCommentLikes .Where(x => x.LawID == lawID && x.ApplicationUserID == userID) .Select(x => x.LawCommentID) .ToList(); foreach (var comment in comments) { var result = new Models.CommentModel() { Comment = comment, UserVoted = userVotes.Any(x => x == comment.LawCommentID) }; var nullableResult = commentVotingResults.Where(x => x.CommentID == comment.LawCommentID && x.Vote == true).FirstOrDefault(); result.VotesUp = nullableResult == null ? 0 : nullableResult.Count; nullableResult = commentVotingResults.Where(x => x.CommentID == comment.LawCommentID && x.Vote == false).FirstOrDefault(); result.VotesDown = nullableResult == null ? 0 : nullableResult.Count; result.VotesDownPercentage = Infrastructure.Math.Percentage(result.VotesDown, result.VotesDown + result.VotesUp); result.VotesUpPercentage = Infrastructure.Math.Percentage(result.VotesUp, result.VotesDown + result.VotesUp); results.Comments.Add(result); } return(results); } }
public ActionResult AddComment(Models.CommentModel comment) { _comments.Add(comment); return(Content("Success :)")); }