From 7d19980fe0ae4ac3aeea7a806417e7444834ed2b Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Mon, 4 Oct 2021 13:10:02 +0330 Subject: [PATCH] #217 api for retrieving couplet numbers --- RMuseum/Controllers/NumberingController.cs | 19 ++++++++ .../GanjoorCoupletNumberViewModel.cs | 23 ++++++++++ RMuseum/RMuseum.xml | 44 +++++++++++++++++++ RMuseum/Services/IGanjoorNumberingService.cs | 9 ++++ .../Implementation/GanjoorNumberingService.cs | 37 +++++++++++++--- 5 files changed, 127 insertions(+), 5 deletions(-) create mode 100644 RMuseum/Models/Ganjoor/ViewModels/GanjoorCoupletNumberViewModel.cs diff --git a/RMuseum/Controllers/NumberingController.cs b/RMuseum/Controllers/NumberingController.cs index f46ac796..9e2ec5cc 100644 --- a/RMuseum/Controllers/NumberingController.cs +++ b/RMuseum/Controllers/NumberingController.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using RMuseum.Models.Auth.Memory; using RMuseum.Models.Ganjoor; +using RMuseum.Models.Ganjoor.ViewModels; using RMuseum.Services; using RSecurityBackend.Models.Auth.Memory; using System.Collections.Generic; @@ -82,6 +83,24 @@ namespace RMuseum.Controllers return Ok(combined); } + /// + /// get all numbering patterns for a couplet + /// + /// + /// + /// + [HttpGet("couplet/{poemId}/{coupletId}")] + [AllowAnonymous] + [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorCoupletNumberViewModel[]))] + [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] + public async Task GetNumberingsForCouplet(int poemId, int coupletIndex) + { + var res = await _numberingService.GetNumberingsForCouplet(poemId, coupletIndex); + if (!string.IsNullOrEmpty(res.ExceptionString)) + return BadRequest(res.ExceptionString); + return Ok(res.Result); + } + /// /// add new numbering /// diff --git a/RMuseum/Models/Ganjoor/ViewModels/GanjoorCoupletNumberViewModel.cs b/RMuseum/Models/Ganjoor/ViewModels/GanjoorCoupletNumberViewModel.cs new file mode 100644 index 00000000..eae916f2 --- /dev/null +++ b/RMuseum/Models/Ganjoor/ViewModels/GanjoorCoupletNumberViewModel.cs @@ -0,0 +1,23 @@ +namespace RMuseum.Models.Ganjoor.ViewModels +{ + /// + /// Ganjoor Couplet Number View Model + /// + public class GanjoorCoupletNumberViewModel + { + /// + /// name + /// + public string NumberingName { get; set; } + + /// + /// number + /// + public int CoupletNumber { get; set; } + + /// + /// total couplets + /// + public int TotalCouplets { get; set; } + } +} diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index 0217153c..15adaa2d 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -1401,6 +1401,14 @@ category id + + + get all numbering patterns for a couplet + + + + + add new numbering @@ -6082,6 +6090,26 @@ couplet summary + + + Ganjoor Couplet Number View Model + + + + + name + + + + + number + + + + + total couplets + + Rhyme Analysis Result @@ -8343,6 +8371,14 @@ + + + get all numbering patterns for a couplet + + + + + start counting @@ -10030,6 +10066,14 @@ + + + get all numbering patterns for a couplet + + + + + start counting diff --git a/RMuseum/Services/IGanjoorNumberingService.cs b/RMuseum/Services/IGanjoorNumberingService.cs index 217d460a..33c08553 100644 --- a/RMuseum/Services/IGanjoorNumberingService.cs +++ b/RMuseum/Services/IGanjoorNumberingService.cs @@ -1,4 +1,5 @@ using RMuseum.Models.Ganjoor; +using RMuseum.Models.Ganjoor.ViewModels; using RSecurityBackend.Models.Generic; using System.Threading.Tasks; @@ -55,6 +56,14 @@ namespace RMuseum.Services /// Task> GetNumberingsForDirectSubCatsAsync(int parentCatId); + /// + /// get all numbering patterns for a couplet + /// + /// + /// + /// + Task> GetNumberingsForCouplet(int poemId, int coupletIndex); + /// /// start counting /// diff --git a/RMuseum/Services/Implementation/GanjoorNumberingService.cs b/RMuseum/Services/Implementation/GanjoorNumberingService.cs index 57906c94..4d6d9054 100644 --- a/RMuseum/Services/Implementation/GanjoorNumberingService.cs +++ b/RMuseum/Services/Implementation/GanjoorNumberingService.cs @@ -1,6 +1,7 @@ using Microsoft.EntityFrameworkCore; using RMuseum.DbContext; using RMuseum.Models.Ganjoor; +using RMuseum.Models.Ganjoor.ViewModels; using RSecurityBackend.Models.Generic; using RSecurityBackend.Services; using RSecurityBackend.Services.Implementation; @@ -157,6 +158,32 @@ namespace RMuseum.Services.Implementation } } + /// + /// get all numbering patterns for a couplet + /// + /// + /// + /// + public async Task> GetNumberingsForCouplet(int poemId, int coupletIndex) + { + return new RServiceResult + ( + await _context.GanjoorVerseNumbers.Include(n => n.Numbering) + .Where(n => n.PoemId == poemId && n.CoupletIndex == coupletIndex) + .OrderBy(n => n.NumberingId) + .Select + ( + n => + new GanjoorCoupletNumberViewModel() + { + NumberingName = n.Numbering.Name, + CoupletNumber = n.Number, + TotalCouplets = n.Numbering.TotalCouplets + } + ).ToArrayAsync() + ); + } + private async Task> _FindAllSubCategories(RMuseumDbContext context, int catId) { List cats = new List(); @@ -167,7 +194,7 @@ namespace RMuseum.Services.Implementation var subCats = await _FindAllSubCategories(context, cat.Id); - if(subCats.Count > 0) + if (subCats.Count > 0) { cats.AddRange(subCats); } @@ -179,11 +206,11 @@ namespace RMuseum.Services.Implementation { var parents = await context.GanjoorCategories.AsNoTracking().Where(c => c.ParentId == startCat.ParentId && c.Id >= startCat.Id && c.Id <= endCatId).OrderBy(c => c.Id).ToListAsync(); List cats = new List(); - foreach(GanjoorCat parentCat in parents) + foreach (GanjoorCat parentCat in parents) { cats.Add(parentCat); var subCats = await _FindAllSubCategories(context, parentCat.Id); - if(subCats.Count > 0) + if (subCats.Count > 0) { cats.AddRange(subCats); } @@ -194,7 +221,7 @@ namespace RMuseum.Services.Implementation private async Task> _FindTargetCategories(RMuseumDbContext context, GanjoorCat startCat, int? endCatId) { - if(endCatId == null || startCat.Id == endCatId) + if (endCatId == null || startCat.Id == endCatId) { List cats = new List(); cats.Add(startCat); @@ -244,7 +271,7 @@ namespace RMuseum.Services.Implementation await jobProgressServiceEF.UpdateJob(job.Id, 0, $"Counting:: {cat.Title}"); - foreach(var poem in poems) + foreach (var poem in poems) { totalVerseCount += await context.GanjoorVerses.Where(v => v.PoemId == poem.Id).CountAsync(); var verses = await context.GanjoorVerses.AsNoTracking()