#264 returning computation scores for recitation orders

This commit is contained in:
Hamid Reza Mohammadi 2021-12-22 19:08:38 +03:30
parent a809c6b456
commit 7436c1c32c
4 changed files with 97 additions and 16 deletions

View File

@ -972,6 +972,24 @@ namespace RMuseum.Controllers
return Ok(res.Result.Items);
}
/// <summary>
/// compute poem recitations order (no update)
/// </summary>
/// <param name="poemId"></param>
/// <returns></returns>
[HttpGet("votes/{poemId}/scores")]
[AllowAnonymous]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable<RecitationOrderingViewModel>))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public async Task<IActionResult> ComputePoemRecitationsOrdersAsync(int poemId)
{
var res = await _audioService.ComputePoemRecitationsOrdersAsync(poemId, false);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
return Ok(res.Result);
}
/// <summary>
/// readonly mode
/// </summary>

View File

@ -0,0 +1,38 @@
namespace RMuseum.Models.GanjoorAudio.ViewModels
{
/// <summary>
/// Recitation Ordering View Model
/// </summary>
public class RecitationOrderingViewModel
{
/// <summary>
/// recitation
/// </summary>
public int RecitationId { get; set; }
/// <summary>
/// earlyness advatage
/// </summary>
public int EarlynessAdvantage { get; set; }
/// <summary>
/// upvotes from users other than the owner
/// </summary>
public int UpVotes { get; set; }
/// <summary>
/// approved mistaked
/// </summary>
public int Mistakes { get; set; }
/// <summary>
/// total scores
/// </summary>
public int TotalScores { get; set; }
/// <summary>
/// computed order
/// </summary>
public int ComputedOrder { get; set; }
}
}

View File

@ -232,11 +232,12 @@ namespace RMuseum.Services
Task<RServiceResult<bool>> AcceptReportedErrorAsync(int id);
/// <summary>
/// reorder poem recitations by user upvotes + being elder score
/// compute poem recitations order
/// </summary>
/// <param name="poemId"></param>
/// <param name="update"></param>
/// <returns></returns>
Task<RServiceResult<bool>> ReOrderPoemRecitationsAsync(int poemId);
Task<RServiceResult<RecitationOrderingViewModel[]>> ComputePoemRecitationsOrdersAsync(int poemId, bool update = true);
/// <summary>
/// up vote a recitation

View File

@ -1870,45 +1870,69 @@ namespace RMuseum.Services.Implementationa
}
/// <summary>
/// reorder poem recitations by user upvotes + being elder score
/// compute poem recitations order
/// </summary>
/// <param name="poemId"></param>
/// <param name="update"></param>
/// <returns></returns>
public async Task<RServiceResult<bool>> ReOrderPoemRecitationsAsync(int poemId)
public async Task<RServiceResult<RecitationOrderingViewModel[]>> ComputePoemRecitationsOrdersAsync(int poemId, bool update = true)
{
try
{
var recitations =
var recitations =
await _context.Recitations
.Where(r => r.ReviewStatus == AudioReviewStatus.Approved && r.GanjoorPostId == poemId)
.OrderByDescending(r => r.Id) //this causes the oldest recirations to become the first one
.ToListAsync();
for (var i = 0; i< recitations.Count; i++)
List<RecitationOrderingViewModel> scores = new List<RecitationOrderingViewModel>();
for (var i = 0; i < recitations.Count; i++)
{
var recitation = recitations[i];
RecitationOrderingViewModel score = new RecitationOrderingViewModel()
{
RecitationId = recitation.Id,
EarlynessAdvantage = recitations.Count - 1 - i,
UpVotes = await _context.RecitationUserUpVotes.AsNoTracking().Where(r => r.RecitationId == recitation.Id && r.UserId != recitation.OwnerId)
.CountAsync(),
Mistakes = 0
};
score.TotalScores = score.EarlynessAdvantage
+ score.UpVotes
- score.Mistakes;
//audio order is used as a temporary variable in the following line and soon is get replaced by computed value
recitation.AudioOrder =
recitations.Count - 1 - i +
await _context.RecitationUserUpVotes.AsNoTracking().Where(r => r.RecitationId == recitation.Id && r.UserId != recitation.OwnerId)
.CountAsync(); //this way oldest recitations have an advantage which could be beaten by user ranks over time
recitation.AudioOrder = score.TotalScores;
scores.Add(score);
}
recitations.Sort((a, b) => a.AudioOrder.CompareTo(b.AudioOrder));
for (var i = 0; i < recitations.Count; i++)
{
recitations[i].AudioOrder = i + 1;
_context.Update(recitations[i]);
scores.Where(s => s.RecitationId == recitations[i].Id).Single().ComputedOrder = i + 1;
if (update)
{
_context.Update(recitations[i]);
}
}
await _context.SaveChangesAsync();
if (update)
await _context.SaveChangesAsync();
return new RServiceResult<bool>(true);
return new RServiceResult<RecitationOrderingViewModel[]>(scores.ToArray());
}
catch (Exception exp)
{
return new RServiceResult<bool>(false, exp.ToString());
return new RServiceResult<RecitationOrderingViewModel[]>(null, exp.ToString());
}
}
@ -1947,7 +1971,7 @@ namespace RMuseum.Services.Implementationa
_context.RecitationUserUpVotes.Add(vote);
await _context.SaveChangesAsync();
await ReOrderPoemRecitationsAsync(recitation.GanjoorPostId);
await ComputePoemRecitationsOrdersAsync(recitation.GanjoorPostId);
return new RServiceResult<bool>(true);
@ -1977,7 +2001,7 @@ namespace RMuseum.Services.Implementationa
_context.Remove(vote);
await _context.SaveChangesAsync();
await ReOrderPoemRecitationsAsync(recitation.GanjoorPostId);
await ComputePoemRecitationsOrdersAsync(recitation.GanjoorPostId);
return new RServiceResult<bool>(true);