#495 rating comments

This commit is contained in:
Hamid Reza Mohammadi 2026-08-04 14:59:58 +03:30
parent 9d32aa27c0
commit 1b613ba397
5 changed files with 92 additions and 11 deletions

View File

@ -1199,6 +1199,7 @@ namespace RMuseum.Controllers
/// </summary>
/// <param name="id"></param>
/// <param name="coupletIndex"></param>
/// <param name="sortByRanking">true: order by rating then date, false: order by date</param>
/// <returns></returns>
[HttpGet]
@ -1206,10 +1207,31 @@ namespace RMuseum.Controllers
[AllowAnonymous]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorCommentSummaryViewModel[]))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public async Task<IActionResult> GetPoemComments(int id, int? coupletIndex)
public async Task<IActionResult> GetPoemComments(int id, int? coupletIndex, bool sortByRanking = true)
{
RServiceResult<GanjoorCommentSummaryViewModel[]> res =
await _ganjoorService.GetPoemComments(id, Guid.Empty, coupletIndex);
await _ganjoorService.GetPoemComments(id, Guid.Empty, coupletIndex, sortByRanking);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
return Ok(res.Result);
}
/// <summary>
/// Rate a comment (like / dislike / clear rating)
/// </summary>
/// <param name="id">comment id</param>
/// <param name="value">+1: like, -1: dislike, 0: remove previous rating</param>
/// <returns></returns>
[HttpPost]
[Route("poem/comment/{id}/rate")]
[Authorize]
[ProducesResponseType((int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public async Task<IActionResult> RateComment(int id, short value)
{
Guid userId = new Guid(User.Claims.First(c => c.Type == "UserId").Value);
RServiceResult<bool> res =
await _ganjoorService.RateCommentAsync(userId, id, value);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
return Ok(res.Result);

View File

@ -254,6 +254,15 @@ namespace RMuseum.DbContext
.HasIndex(i => new { i.GanjoorCommentId, i.UserId })
.IsUnique();
builder.Entity<GanjoorCommentReaction>()
.HasIndex(i => new { i.PoemId, i.UserId });
builder.Entity<GanjoorComment>()
.HasIndex(i => new { i.PoemId, i.SortKey });
builder.Entity<GanjoorComment>()
.HasIndex(i => new { i.PoemId, i.CommentDate });
}

View File

@ -1358,7 +1358,7 @@
<param name="url"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.GetPoemById(System.Int32,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean)">
<member name="M:RMuseum.Controllers.GanjoorController.GetPoemById(System.Int32,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean)">
<summary>
get poem by id
</summary>
@ -1373,6 +1373,7 @@
<param name="verseDetails"></param>
<param name="navigation">next/previous</param>
<param name="relatedpoems"></param>
<param name="sortByRanking"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.GetPoemVerses(System.Int32,System.Int32)">
@ -1429,12 +1430,21 @@
<param name="trackType"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.GetPoemComments(System.Int32,System.Nullable{System.Int32})">
<member name="M:RMuseum.Controllers.GanjoorController.GetPoemComments(System.Int32,System.Nullable{System.Int32},System.Boolean)">
<summary>
Get Poem Comments
</summary>
<param name="id"></param>
<param name="coupletIndex"></param>
<param name="sortByRanking">true: order by rating then date, false: order by date</param>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.RateComment(System.Int32,System.Int16)">
<summary>
Rate a comment (like / dislike / clear rating)
</summary>
<param name="id">comment id</param>
<param name="value">+1: like, -1: dislike, 0: remove previous rating</param>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.GetRelatedSections(System.Int32,System.Int32,System.Int32,System.Int32)">
@ -16764,7 +16774,7 @@
<param name="id"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.GetPoemById(System.Int32,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean)">
<member name="M:RMuseum.Services.IGanjoorService.GetPoemById(System.Int32,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean)">
<summary>
Get Poem By Id
</summary>
@ -16780,6 +16790,7 @@
<param name="navigation"></param>
<param name="relatedpoems"></param>
<param name="sections"></param>
<param name="sortByRanking"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.GetPoemVersesAsync(System.Int32,System.Int32)">
@ -16845,13 +16856,14 @@
<param name="trackType"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.GetPoemComments(System.Int32,System.Guid,System.Nullable{System.Int32})">
<member name="M:RMuseum.Services.IGanjoorService.GetPoemComments(System.Int32,System.Guid,System.Nullable{System.Int32},System.Boolean)">
<summary>
get poem comments
</summary>
<param name="poemId"></param>
<param name="userId"></param>
<param name="coupletIndex"></param>
<param name="sortByRanking">true: order by rating SortKey then CommentDate, false: order by CommentDate</param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.GetCommentByIdAsync(System.Int32)">
@ -16861,6 +16873,15 @@
<param name="commentId"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.RateCommentAsync(System.Guid,System.Int32,System.Int16)">
<summary>
like / dislike / clear rating for a comment
</summary>
<param name="userId"></param>
<param name="commentId"></param>
<param name="value">+1: like, -1: dislike, 0: remove previous rating</param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.GetRelatedSections(System.Int32,System.Int32,System.Int32,System.Int32)">
<summary>
get a section related sections
@ -20119,6 +20140,9 @@
<summary>
IGanjoorService implementation
</summary>
<summary>
IGanjoorService implementation
</summary>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.SwitchCoupletBookmark(System.Guid,System.Int32,System.Int32)">
<summary>
@ -20321,6 +20345,15 @@
</summary>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.RateCommentAsync(System.Guid,System.Int32,System.Int16)">
<summary>
</summary>
<param name="userId"></param>
<param name="commentId"></param>
<param name="value">+1: like, -1: dislike, 0: remove previous rating</param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.GetDigitalSourceFromTagAsync(System.String)">
<summary>
get digital source from tag
@ -21342,13 +21375,14 @@
<param name="userId"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.GetPoemComments(System.Int32,System.Guid,System.Nullable{System.Int32})">
<member name="M:RMuseum.Services.Implementation.GanjoorService.GetPoemComments(System.Int32,System.Guid,System.Nullable{System.Int32},System.Boolean)">
<summary>
get poem comments
</summary>
<param name="poemId"></param>
<param name="userId"></param>
<param name="coupletIndex"></param>
<param name="sortByRanking"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.GetCommentByIdAsync(System.Int32)">
@ -21482,7 +21516,7 @@
<param name="coupletIndex"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.GetPoemById(System.Int32,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean)">
<member name="M:RMuseum.Services.Implementation.GanjoorService.GetPoemById(System.Int32,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean)">
<summary>
Get Poem By Id
</summary>
@ -21498,6 +21532,7 @@
<param name="navigation"></param>
<param name="relatedpoems"></param>
<param name="sections">sections</param>
<param name="sortByRanking"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.DeletePoemCorrections(System.Guid,System.Int32)">
@ -23552,6 +23587,11 @@
<param name="userId"></param>
<returns></returns>
</member>
<member name="T:RMuseum.Utils.GanjoorCommentRankingScoreCalculator">
<summary>
comment ranking score calculator
</summary>
</member>
<member name="T:RMuseum.Utils.GanjoorPoemTools">
<summary>
ganjoor poem tools

View File

@ -212,8 +212,9 @@ namespace RMuseum.Services
/// <param name="poemId"></param>
/// <param name="userId"></param>
/// <param name="coupletIndex"></param>
/// <param name="sortByRanking">true: order by rating SortKey then CommentDate, false: order by CommentDate</param>
/// <returns></returns>
Task<RServiceResult<GanjoorCommentSummaryViewModel[]>> GetPoemComments(int poemId, Guid userId, int? coupletIndex);
Task<RServiceResult<GanjoorCommentSummaryViewModel[]>> GetPoemComments(int poemId, Guid userId, int? coupletIndex, bool sortByRanking = false);
/// <summary>
/// get a single comment information (replies are not included)
@ -222,6 +223,15 @@ namespace RMuseum.Services
/// <returns></returns>
Task<RServiceResult<GanjoorCommentSummaryViewModel>> GetCommentByIdAsync(int commentId);
/// <summary>
/// like / dislike / clear rating for a comment
/// </summary>
/// <param name="userId"></param>
/// <param name="commentId"></param>
/// <param name="value">+1: like, -1: dislike, 0: remove previous rating</param>
/// <returns></returns>
Task<RServiceResult<bool>> RateCommentAsync(Guid userId, int commentId, short value);
/// <summary>
/// get a section related sections
/// </summary>

View File

@ -85,8 +85,8 @@ namespace RMuseum.Services.Implementation
{
try
{
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);
var likes = await _context.GanjoorCommentReactions.AsNoTracking().CountAsync(r => r.GanjoorCommentId == comment.Id && r.Value == 1);
var dislikes = await _context.GanjoorCommentReactions.AsNoTracking().CountAsync(r => r.GanjoorCommentId == comment.Id && r.Value == -1);
comment.LikeCount = likes;
comment.DislikeCount = dislikes;
comment.SortKey = GanjoorCommentRankingScoreCalculator.ComputeRankingScore(likes, dislikes);