From a809c6b4565d07d2a76cfdd3a6c23154935a9fcb Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Wed, 22 Dec 2021 18:26:17 +0330 Subject: [PATCH] #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) --- GanjooRazor/Pages/Index.cshtml.cs | 4 ++-- GanjooRazor/wwwroot/js/bk.js | 6 ++--- RMuseum/Controllers/GanjoorController.cs | 6 ++--- .../RecitationUserUpVoteViewModel.cs | 18 --------------- RMuseum/RMuseum.xml | 17 +------------- RMuseum/Services/IGanjoorService.cs | 2 +- .../Services/Implementation/GanjoorService.cs | 22 +++++++++---------- 7 files changed, 20 insertions(+), 55 deletions(-) delete mode 100644 RMuseum/Models/GanjoorAudio/ViewModels/RecitationUserUpVoteViewModel.cs diff --git a/GanjooRazor/Pages/Index.cshtml.cs b/GanjooRazor/Pages/Index.cshtml.cs index 5aaba368..6b1c8deb 100644 --- a/GanjooRazor/Pages/Index.cshtml.cs +++ b/GanjooRazor/Pages/Index.cshtml.cs @@ -837,7 +837,7 @@ namespace GanjooRazor.Pages return new OkObjectResult(false); } - public async Task OnGetGetUserUpvotedRecitationsAsync(int poemId) + public async Task OnGetUserUpvotedRecitationsAsync(int poemId) { using (HttpClient secureClient = new HttpClient()) { @@ -848,7 +848,7 @@ namespace GanjooRazor.Pages { return new BadRequestObjectResult(JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync())); } - var res = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); + var res = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); return new OkObjectResult(res); } } diff --git a/GanjooRazor/wwwroot/js/bk.js b/GanjooRazor/wwwroot/js/bk.js index 3bb4d3e8..c3778951 100644 --- a/GanjooRazor/wwwroot/js/bk.js +++ b/GanjooRazor/wwwroot/js/bk.js @@ -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'); } }, }); diff --git a/RMuseum/Controllers/GanjoorController.cs b/RMuseum/Controllers/GanjoorController.cs index 5bd732b5..188ca504 100644 --- a/RMuseum/Controllers/GanjoorController.cs +++ b/RMuseum/Controllers/GanjoorController.cs @@ -804,19 +804,19 @@ namespace RMuseum.Controllers } /// - /// get user up votes for the recitations of a poem + /// get user upvoted recitations of a poem /// /// /// [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 GetUserPoemRecitationsUpVotes(int id) { var loggedOnUserId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value); - RServiceResult res = + RServiceResult res = await _ganjoorService.GetUserPoemRecitationsUpVotes(id, loggedOnUserId); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); diff --git a/RMuseum/Models/GanjoorAudio/ViewModels/RecitationUserUpVoteViewModel.cs b/RMuseum/Models/GanjoorAudio/ViewModels/RecitationUserUpVoteViewModel.cs deleted file mode 100644 index 276cb0fd..00000000 --- a/RMuseum/Models/GanjoorAudio/ViewModels/RecitationUserUpVoteViewModel.cs +++ /dev/null @@ -1,18 +0,0 @@ -namespace RMuseum.Models.GanjoorAudio.ViewModels -{ - /// - /// recitation user up vote - /// - public class RecitationUserUpVoteViewModel - { - /// - /// reciation id - /// - public int Id { get; set; } - - /// - /// up vote - /// - public bool UpVote { get; set; } - } -} diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index af4ea37e..94009152 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -958,7 +958,7 @@ - get user up votes for the recitations of a poem + get user upvoted recitations of a poem @@ -4115,21 +4115,6 @@ Finish Date - - - recitation user up vote - - - - - reciation id - - - - - up vote - - Verse Sync Range diff --git a/RMuseum/Services/IGanjoorService.cs b/RMuseum/Services/IGanjoorService.cs index 32ee02c2..35238ed0 100644 --- a/RMuseum/Services/IGanjoorService.cs +++ b/RMuseum/Services/IGanjoorService.cs @@ -129,7 +129,7 @@ namespace RMuseum.Services /// /// /// - Task> GetUserPoemRecitationsUpVotes(int id, Guid userId); + Task> GetUserPoemRecitationsUpVotes(int id, Guid userId); /// /// get poem images by id (some fields are intentionally field with blank or null), diff --git a/RMuseum/Services/Implementation/GanjoorService.cs b/RMuseum/Services/Implementation/GanjoorService.cs index ea6dce5f..f0598563 100644 --- a/RMuseum/Services/Implementation/GanjoorService.cs +++ b/RMuseum/Services/Implementation/GanjoorService.cs @@ -556,7 +556,7 @@ namespace RMuseum.Services.Implementation /// /// /// - public async Task> GetUserPoemRecitationsUpVotes(int id, Guid userId) + public async Task> 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 upVotedRecitations = new List(); + 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(recitationUpVotes); + return new RServiceResult(upVotedRecitations.ToArray()); } private async Task _FillPoemCoupletIndices(RMuseumDbContext context, int poemId)