From 9d32aa27c00978f15074fa44abcbf9f9bdc5fb30 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Wed, 22 Jul 2026 19:30:16 +0330 Subject: [PATCH] #495 RateCommentAsync --- RMuseum/DbContext/RMuseumDbContext.cs | 6 ++++- .../Models/Ganjoor/GanjoorCommentReaction.cs | 1 + .../GanjoorService-CommentRating.cs | 24 +++++++++++-------- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/RMuseum/DbContext/RMuseumDbContext.cs b/RMuseum/DbContext/RMuseumDbContext.cs index 27f82054..e5c28c25 100644 --- a/RMuseum/DbContext/RMuseumDbContext.cs +++ b/RMuseum/DbContext/RMuseumDbContext.cs @@ -249,7 +249,11 @@ namespace RMuseum.DbContext builder.Entity() .Property(e => e.PoemSummary) .HasMaxLength(3000); - + + builder.Entity() + .HasIndex(i => new { i.GanjoorCommentId, i.UserId }) + .IsUnique(); + } diff --git a/RMuseum/Models/Ganjoor/GanjoorCommentReaction.cs b/RMuseum/Models/Ganjoor/GanjoorCommentReaction.cs index af7df590..5b8e199f 100644 --- a/RMuseum/Models/Ganjoor/GanjoorCommentReaction.cs +++ b/RMuseum/Models/Ganjoor/GanjoorCommentReaction.cs @@ -10,6 +10,7 @@ namespace RMuseum.Models.Ganjoor public int GanjoorCommentId { get; set; } public virtual GanjoorComment GanjoorComment { get; set; } + public int PoemId { get; set; } public Guid UserId { get; set; } public virtual RAppUser User { get; set; } diff --git a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-CommentRating.cs b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-CommentRating.cs index 456bfa34..78c00ce1 100644 --- a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-CommentRating.cs +++ b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-CommentRating.cs @@ -28,6 +28,12 @@ namespace RMuseum.Services.Implementation } try { + var comment = await _context.GanjoorComments.Where(c => c.Id == commentId).SingleOrDefaultAsync(); + if (comment == null) + { + return new RServiceResult(false, "comment == null"); + } + var oldRating = await _context.GanjoorCommentReactions.Where(c => c.GanjoorCommentId == commentId && c.UserId == userId).SingleOrDefaultAsync(); if(value == 0 && oldRating == null) { @@ -51,20 +57,21 @@ namespace RMuseum.Services.Implementation } await _context.SaveChangesAsync(); - return await ReCalculateCommentSortKeyAsync(commentId); + return await ReCalculateCommentSortKeyAsync(comment); } else { var rating = new GanjoorCommentReaction() { GanjoorCommentId = commentId, + PoemId = comment.PoemId, UserId = userId, Value = value, ReactionDate = DateTime.Now, }; _context.Add(rating); await _context.SaveChangesAsync(); - return await ReCalculateCommentSortKeyAsync(commentId); + return await ReCalculateCommentSortKeyAsync(comment); } } @@ -74,17 +81,14 @@ namespace RMuseum.Services.Implementation } } - private async Task> ReCalculateCommentSortKeyAsync(int commentId) + private async Task> ReCalculateCommentSortKeyAsync(GanjoorComment comment) { try { - var likes = await _context.GanjoorCommentReactions.AsNoTracking().Where(r => r.GanjoorCommentId == commentId && r.Value == 1).SumAsync(r => r.Value); - var dislikes = await _context.GanjoorCommentReactions.AsNoTracking().Where(r => r.GanjoorCommentId == commentId && r.Value == -1).SumAsync(r => r.Value); - var comment = await _context.GanjoorComments.Where(c => c.Id == commentId).SingleOrDefaultAsync(); - if(comment == null) - { - return new RServiceResult(false, "comment == null"); - } + var likes = await _context.GanjoorCommentReactions.AsNoTracking().Where(r => r.GanjoorCommentId == comment.Id && r.Value == 1).SumAsync(r => r.Value); + var dislikes = await _context.GanjoorCommentReactions.AsNoTracking().Where(r => r.GanjoorCommentId == comment.Id && r.Value == -1).SumAsync(r => r.Value); + comment.LikeCount = likes; + comment.DislikeCount = dislikes; comment.SortKey = GanjoorCommentRankingScoreCalculator.ComputeRankingScore(likes, dislikes); _context.Update(comment); await _context.SaveChangesAsync();