#495 RateCommentAsync
This commit is contained in:
parent
97e472af9a
commit
9d32aa27c0
@ -249,7 +249,11 @@ namespace RMuseum.DbContext
|
|||||||
builder.Entity<GanjoorPoem>()
|
builder.Entity<GanjoorPoem>()
|
||||||
.Property(e => e.PoemSummary)
|
.Property(e => e.PoemSummary)
|
||||||
.HasMaxLength(3000);
|
.HasMaxLength(3000);
|
||||||
|
|
||||||
|
builder.Entity<GanjoorCommentReaction>()
|
||||||
|
.HasIndex(i => new { i.GanjoorCommentId, i.UserId })
|
||||||
|
.IsUnique();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -10,6 +10,7 @@ namespace RMuseum.Models.Ganjoor
|
|||||||
public int GanjoorCommentId { get; set; }
|
public int GanjoorCommentId { get; set; }
|
||||||
public virtual GanjoorComment GanjoorComment { get; set; }
|
public virtual GanjoorComment GanjoorComment { get; set; }
|
||||||
|
|
||||||
|
public int PoemId { get; set; }
|
||||||
public Guid UserId { get; set; }
|
public Guid UserId { get; set; }
|
||||||
public virtual RAppUser User { get; set; }
|
public virtual RAppUser User { get; set; }
|
||||||
|
|
||||||
|
|||||||
@ -28,6 +28,12 @@ namespace RMuseum.Services.Implementation
|
|||||||
}
|
}
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
var comment = await _context.GanjoorComments.Where(c => c.Id == commentId).SingleOrDefaultAsync();
|
||||||
|
if (comment == null)
|
||||||
|
{
|
||||||
|
return new RServiceResult<bool>(false, "comment == null");
|
||||||
|
}
|
||||||
|
|
||||||
var oldRating = await _context.GanjoorCommentReactions.Where(c => c.GanjoorCommentId == commentId && c.UserId == userId).SingleOrDefaultAsync();
|
var oldRating = await _context.GanjoorCommentReactions.Where(c => c.GanjoorCommentId == commentId && c.UserId == userId).SingleOrDefaultAsync();
|
||||||
if(value == 0 && oldRating == null)
|
if(value == 0 && oldRating == null)
|
||||||
{
|
{
|
||||||
@ -51,20 +57,21 @@ namespace RMuseum.Services.Implementation
|
|||||||
}
|
}
|
||||||
await _context.SaveChangesAsync();
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
return await ReCalculateCommentSortKeyAsync(commentId);
|
return await ReCalculateCommentSortKeyAsync(comment);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var rating = new GanjoorCommentReaction()
|
var rating = new GanjoorCommentReaction()
|
||||||
{
|
{
|
||||||
GanjoorCommentId = commentId,
|
GanjoorCommentId = commentId,
|
||||||
|
PoemId = comment.PoemId,
|
||||||
UserId = userId,
|
UserId = userId,
|
||||||
Value = value,
|
Value = value,
|
||||||
ReactionDate = DateTime.Now,
|
ReactionDate = DateTime.Now,
|
||||||
};
|
};
|
||||||
_context.Add(rating);
|
_context.Add(rating);
|
||||||
await _context.SaveChangesAsync();
|
await _context.SaveChangesAsync();
|
||||||
return await ReCalculateCommentSortKeyAsync(commentId);
|
return await ReCalculateCommentSortKeyAsync(comment);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -74,17 +81,14 @@ namespace RMuseum.Services.Implementation
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<RServiceResult<bool>> ReCalculateCommentSortKeyAsync(int commentId)
|
private async Task<RServiceResult<bool>> ReCalculateCommentSortKeyAsync(GanjoorComment comment)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var likes = await _context.GanjoorCommentReactions.AsNoTracking().Where(r => r.GanjoorCommentId == commentId && r.Value == 1).SumAsync(r => r.Value);
|
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 == commentId && 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);
|
||||||
var comment = await _context.GanjoorComments.Where(c => c.Id == commentId).SingleOrDefaultAsync();
|
comment.LikeCount = likes;
|
||||||
if(comment == null)
|
comment.DislikeCount = dislikes;
|
||||||
{
|
|
||||||
return new RServiceResult<bool>(false, "comment == null");
|
|
||||||
}
|
|
||||||
comment.SortKey = GanjoorCommentRankingScoreCalculator.ComputeRankingScore(likes, dislikes);
|
comment.SortKey = GanjoorCommentRankingScoreCalculator.ComputeRankingScore(likes, dislikes);
|
||||||
_context.Update(comment);
|
_context.Update(comment);
|
||||||
await _context.SaveChangesAsync();
|
await _context.SaveChangesAsync();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user