#264 create a more brief version of this API:

/api/ganjoor/poem/{poemId}/recitations/upvotes
to only return an array of int (only upvoted ones)
This commit is contained in:
Hamid Reza Mohammadi 2021-12-22 18:26:17 +03:30
parent 32bd764bd0
commit a809c6b456
7 changed files with 20 additions and 55 deletions

View File

@ -837,7 +837,7 @@ namespace GanjooRazor.Pages
return new OkObjectResult(false);
}
public async Task<IActionResult> OnGetGetUserUpvotedRecitationsAsync(int poemId)
public async Task<IActionResult> OnGetUserUpvotedRecitationsAsync(int poemId)
{
using (HttpClient secureClient = new HttpClient())
{
@ -848,7 +848,7 @@ namespace GanjooRazor.Pages
{
return new BadRequestObjectResult(JsonConvert.DeserializeObject<string>(await response.Content.ReadAsStringAsync()));
}
var res = JsonConvert.DeserializeObject<RecitationUserUpVoteViewModel[]>(await response.Content.ReadAsStringAsync());
var res = JsonConvert.DeserializeObject<int[]>(await response.Content.ReadAsStringAsync());
return new OkObjectResult(res);
}
}

View File

@ -868,15 +868,13 @@ function MarkUserUpvotedRecitations(poemId) {
setTimeout(function () {
$.ajax({
type: "GET",
url: '?Handler=GetUserUpvotedRecitations&poemId=' + String(poemId),
url: '?Handler=UserUpvotedRecitations&poemId=' + String(poemId),
error: function (err) {
console.log(err);
},
success: function (result) {
for (var i = 0; i < result.length; i++) {
if (result[i].upVote) {
document.getElementById('recitaion-' + String(result[i].id)).classList.add('recitation-vote');
}
document.getElementById('recitaion-' + String(result[i])).classList.add('recitation-vote');
}
},
});

View File

@ -804,19 +804,19 @@ namespace RMuseum.Controllers
}
/// <summary>
/// get user up votes for the recitations of a poem
/// get user upvoted recitations of a poem
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet]
[Route("poem/{id}/recitations/upvotes")]
[Authorize]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(RecitationUserUpVoteViewModel[]))]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(int[]))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public async Task<IActionResult> GetUserPoemRecitationsUpVotes(int id)
{
var loggedOnUserId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value);
RServiceResult<RecitationUserUpVoteViewModel[]> res =
RServiceResult<int[]> res =
await _ganjoorService.GetUserPoemRecitationsUpVotes(id, loggedOnUserId);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);

View File

@ -1,18 +0,0 @@
namespace RMuseum.Models.GanjoorAudio.ViewModels
{
/// <summary>
/// recitation user up vote
/// </summary>
public class RecitationUserUpVoteViewModel
{
/// <summary>
/// reciation id
/// </summary>
public int Id { get; set; }
/// <summary>
/// up vote
/// </summary>
public bool UpVote { get; set; }
}
}

View File

@ -958,7 +958,7 @@
</member>
<member name="M:RMuseum.Controllers.GanjoorController.GetUserPoemRecitationsUpVotes(System.Int32)">
<summary>
get user up votes for the recitations of a poem
get user upvoted recitations of a poem
</summary>
<param name="id"></param>
<returns></returns>
@ -4115,21 +4115,6 @@
Finish Date
</summary>
</member>
<member name="T:RMuseum.Models.GanjoorAudio.ViewModels.RecitationUserUpVoteViewModel">
<summary>
recitation user up vote
</summary>
</member>
<member name="P:RMuseum.Models.GanjoorAudio.ViewModels.RecitationUserUpVoteViewModel.Id">
<summary>
reciation id
</summary>
</member>
<member name="P:RMuseum.Models.GanjoorAudio.ViewModels.RecitationUserUpVoteViewModel.UpVote">
<summary>
up vote
</summary>
</member>
<member name="T:RMuseum.Models.GanjoorAudio.ViewModels.RecitationVerseSync">
<summary>
Verse Sync Range

View File

@ -129,7 +129,7 @@ namespace RMuseum.Services
/// <param name="id"></param>
/// <param name="userId"></param>
/// <returns></returns>
Task<RServiceResult<RecitationUserUpVoteViewModel[]>> GetUserPoemRecitationsUpVotes(int id, Guid userId);
Task<RServiceResult<int[]>> GetUserPoemRecitationsUpVotes(int id, Guid userId);
/// <summary>
/// get poem images by id (some fields are intentionally field with blank or null),

View File

@ -556,7 +556,7 @@ namespace RMuseum.Services.Implementation
/// <param name="id"></param>
/// <param name="userId"></param>
/// <returns></returns>
public async Task<RServiceResult<RecitationUserUpVoteViewModel[]>> GetUserPoemRecitationsUpVotes(int id, Guid userId)
public async Task<RServiceResult<int[]>> GetUserPoemRecitationsUpVotes(int id, Guid userId)
{
var source =
from audio in _context.Recitations
@ -567,19 +567,19 @@ namespace RMuseum.Services.Implementation
&&
poem.Id == id
orderby audio.AudioOrder
select new RecitationUserUpVoteViewModel
{
Id = audio.Id
};
var recitationUpVotes = await source.ToArrayAsync();
foreach (var recitationUpVote in recitationUpVotes)
select audio.Id;
List<int> upVotedRecitations = new List<int>();
var recitationIds = await source.ToArrayAsync();
foreach (var recitationId in recitationIds)
{
recitationUpVote.UpVote =
await _context.RecitationUserUpVotes.Where(v => v.RecitationId == recitationUpVote.Id && v.UserId == userId).AnyAsync();
if (await _context.RecitationUserUpVotes.Where(v => v.RecitationId == recitationId && v.UserId == userId).AnyAsync())
{
upVotedRecitations.Add(recitationId);
}
}
return new RServiceResult<RecitationUserUpVoteViewModel[]>(recitationUpVotes);
return new RServiceResult<int[]>(upVotedRecitations.ToArray());
}
private async Task _FillPoemCoupletIndices(RMuseumDbContext context, int poemId)