From a7a01fa50576087930ca479ca9a6bf515a7d53e3 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Mon, 23 May 2022 12:59:10 +0430 Subject: [PATCH] docs: #324 changes probable meters to support poem sections --- RMuseum/Controllers/GanjoorController.cs | 6 +- .../Ganjoor/GanjoorPoemProbableMetre.cs | 5 ++ RMuseum/RMuseum.xml | 9 +- RMuseum/Services/IGanjoorService.cs | 8 +- .../GanjoorService-ProbableMetres.cs | 89 +++++++++---------- 5 files changed, 63 insertions(+), 54 deletions(-) diff --git a/RMuseum/Controllers/GanjoorController.cs b/RMuseum/Controllers/GanjoorController.cs index fa9804df..25e93e6c 100644 --- a/RMuseum/Controllers/GanjoorController.cs +++ b/RMuseum/Controllers/GanjoorController.cs @@ -2580,12 +2580,12 @@ namespace RMuseum.Controllers [HttpGet] [Route("probablemetre/next")] [AllowAnonymous] - [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorPoemCompleteViewModel))] + [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorPoemSection))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] [ProducesResponseType((int)HttpStatusCode.NotFound)] public async Task GetNextGanjoorPoemProbableMetre() { - RServiceResult res = + RServiceResult res = await _ganjoorService.GetNextGanjoorPoemProbableMetre(); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); @@ -2603,7 +2603,7 @@ namespace RMuseum.Controllers [HttpGet] [Route("probablemetre/list")] [AllowAnonymous] - [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable))] + [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] [ProducesResponseType((int)HttpStatusCode.NotFound)] public async Task GetUnreviewedGanjoorPoemProbableMetres([FromQuery] PagingParameterModel paging) diff --git a/RMuseum/Models/Ganjoor/GanjoorPoemProbableMetre.cs b/RMuseum/Models/Ganjoor/GanjoorPoemProbableMetre.cs index b7e1d65f..64405630 100644 --- a/RMuseum/Models/Ganjoor/GanjoorPoemProbableMetre.cs +++ b/RMuseum/Models/Ganjoor/GanjoorPoemProbableMetre.cs @@ -24,5 +24,10 @@ /// metre /// public string Metre { get; set; } + + /// + /// section id + /// + public int SectionId { get; set; } } } diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index fee49f14..f24621b9 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -7098,6 +7098,11 @@ metre + + + section id + + Ganjoor Poem Section @@ -12420,13 +12425,13 @@ - get next ganjoor poem probable metre + get next ganjoor section probable metre - get a list of ganjoor poems probable metres + get a list of ganjoor poem sections probable metres diff --git a/RMuseum/Services/IGanjoorService.cs b/RMuseum/Services/IGanjoorService.cs index daf6450e..52c390d4 100644 --- a/RMuseum/Services/IGanjoorService.cs +++ b/RMuseum/Services/IGanjoorService.cs @@ -788,17 +788,17 @@ namespace RMuseum.Services /// - /// get next ganjoor poem probable metre + /// get next ganjoor section probable metre /// /// - Task> GetNextGanjoorPoemProbableMetre(); + Task> GetNextGanjoorPoemProbableMetre(); /// - /// get a list of ganjoor poems probable metres + /// get a list of ganjoor poem sections probable metres /// /// /// - Task> GetUnreviewedGanjoorPoemProbableMetres(PagingParameterModel paging); + Task> GetUnreviewedGanjoorPoemProbableMetres(PagingParameterModel paging); /// /// save ganjoor poem probable metre diff --git a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-ProbableMetres.cs b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-ProbableMetres.cs index 8d333519..25daeddd 100644 --- a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-ProbableMetres.cs +++ b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-ProbableMetres.cs @@ -1,10 +1,7 @@ using Microsoft.EntityFrameworkCore; using RMuseum.DbContext; using RMuseum.Models.Ganjoor; -using RMuseum.Models.Ganjoor.ViewModels; -using RMuseum.Services.Implementation.ImportedFromDesktopGanjoor; using RSecurityBackend.Models.Generic; -using RSecurityBackend.Models.Generic.Db; using RSecurityBackend.Services.Implementation; using System; using System.Collections.Generic; @@ -38,38 +35,42 @@ namespace RMuseum.Services.Implementation var job = (await jobProgressServiceEF.NewJob($"StartFindingMissingRhythms", "Query data")).Result; try { - var poemIds = await context.GanjoorPoems.AsNoTracking() + var sectionIds = await context.GanjoorPoemSections.AsNoTracking() .Where(p => p.GanjoorMetreId == null && (onlyPoemsWithRhymes == false || !string.IsNullOrEmpty(p.RhymeLetters)) && - false == (context.GanjoorVerses.Where(v => v.PoemId == p.Id && (v.VersePosition == VersePosition.Paragraph || v.VersePosition == VersePosition.Single)).Any()) - && - false == (context.GanjoorPoemProbableMetres.Where(r => r.PoemId == p.Id).Any()) + false == context.GanjoorPoemProbableMetres.Where(r => r.SectionId == p.Id).Any() ) .Take(poemsNum) .Select(p => p.Id) .ToArrayAsync(); - await jobProgressServiceEF.UpdateJob(job.Id, 0, $"Total: {poemIds.Length}"); + await jobProgressServiceEF.UpdateJob(job.Id, 0, $"Total: {sectionIds.Length}"); var metres = await context.GanjoorMetres.OrderBy(m => m.Rhythm).AsNoTracking().Select(m => m.Rhythm).ToArrayAsync(); using (HttpClient httpClient = new HttpClient()) { - for (int i = 0; i < poemIds.Length; i++) + for (int i = 0; i < sectionIds.Length; i++) { - var id = poemIds[i]; - var res = await _FindPoemMainSectionRhythm(id, context, httpClient, metres, true); - if (res.Result == null) - res.Result = ""; - - GanjoorPoemProbableMetre prometre = new GanjoorPoemProbableMetre() + var id = sectionIds[i]; + var section = await context.GanjoorPoemSections.AsNoTracking().Where(s => s.Id == id).SingleOrDefaultAsync(); + if(section != null) { - PoemId = id, - Metre = res.Result - }; + var res = await _FindSectionRhythm(section, context, httpClient, metres, true); + if (res.Result == null) + res.Result = ""; - context.GanjoorPoemProbableMetres.Add(prometre); + GanjoorPoemProbableMetre prometre = new GanjoorPoemProbableMetre() + { + PoemId = section.PoemId, + SectionId = id, + Metre = res.Result + }; - await jobProgressServiceEF.UpdateJob(job.Id, i); + context.GanjoorPoemProbableMetres.Add(prometre); + + await jobProgressServiceEF.UpdateJob(job.Id, i); + } + } } @@ -90,22 +91,21 @@ namespace RMuseum.Services.Implementation /// get next ganjoor poem probable metre /// /// - public async Task> GetNextGanjoorPoemProbableMetre() + public async Task> GetNextGanjoorPoemProbableMetre() { var next = await _context.GanjoorPoemProbableMetres.Where(p => p.Metre != "dismissed").AsNoTracking().FirstOrDefaultAsync(); if (next == null) - return new RServiceResult(null); - var res = await GetPoemById(next.PoemId); - if (!string.IsNullOrEmpty(res.ExceptionString)) - return new RServiceResult(null, res.ExceptionString); - if (res.Result == null) - return new RServiceResult(null, "poem does not exist!"); - res.Result.GanjoorMetre = new GanjoorMetre() + return new RServiceResult(null); + var res = await _context.GanjoorPoemSections.AsNoTracking().Where(s => s.Id == next.SectionId).SingleOrDefaultAsync(); + + if (res == null) + return new RServiceResult(null, "poem section does not exist!"); + res.GanjoorMetre = new GanjoorMetre() { Id = next.Id, Rhythm = next.Metre }; - return res; + return new RServiceResult(res); } /// @@ -113,30 +113,29 @@ namespace RMuseum.Services.Implementation /// /// /// - public async Task> GetUnreviewedGanjoorPoemProbableMetres(PagingParameterModel paging) + public async Task> GetUnreviewedGanjoorPoemProbableMetres(PagingParameterModel paging) { try { var source = from probable in _context.GanjoorPoemProbableMetres.AsNoTracking() where probable.Metre != "dismissed" select probable; (PaginationMetadata PagingMeta, GanjoorPoemProbableMetre[] Items) paginatedResult = await QueryablePaginator.Paginate(source, paging); - List poems = new List(); + List sections = new List(); foreach (var next in paginatedResult.Items) { - var res = await GetPoemById(next.PoemId); - var poem = res.Result; - poem.GanjoorMetre = new GanjoorMetre() + var section = await _context.GanjoorPoemSections.AsNoTracking().Where(s => s.Id == next.SectionId).SingleOrDefaultAsync(); + section.GanjoorMetre = new GanjoorMetre() { Id = next.Id, Rhythm = next.Metre }; - poems.Add(poem); + sections.Add(section); } - return new RServiceResult<(PaginationMetadata PagingMeta, GanjoorPoemCompleteViewModel[] Items)>((paginatedResult.PagingMeta, poems.ToArray())); + return new RServiceResult<(PaginationMetadata PagingMeta, GanjoorPoemSection[] Items)>((paginatedResult.PagingMeta, sections.ToArray())); } catch (Exception exp) { - return new RServiceResult<(PaginationMetadata PagingMeta, GanjoorPoemCompleteViewModel[] Items)>((null, null), exp.ToString()); + return new RServiceResult<(PaginationMetadata PagingMeta, GanjoorPoemSection[] Items)>((null, null), exp.ToString()); } } @@ -172,10 +171,10 @@ namespace RMuseum.Services.Implementation _context.GanjoorMetres.Add(rhythm); await _context.SaveChangesAsync(); } - var poem = await _context.GanjoorPoems.Where(p => p.Id == item.PoemId).SingleAsync(); - int? oldMetreId = poem.GanjoorMetreId; - poem.GanjoorMetreId = rhythm.Id; - _context.Update(poem); + var section = await _context.GanjoorPoemSections.Where(p => p.Id == item.SectionId).SingleAsync(); + int? oldMetreId = section.GanjoorMetreId; + section.GanjoorMetreId = rhythm.Id; + _context.Update(section); _context.Remove(item); await _context.SaveChangesAsync(); _backgroundTaskQueue.QueueBackgroundWorkItem @@ -184,15 +183,15 @@ namespace RMuseum.Services.Implementation { using (RMuseumDbContext context = new RMuseumDbContext(new DbContextOptions())) //this is long running job, so _context might be already been freed/collected by GC { - if (oldMetreId != null && !string.IsNullOrEmpty(poem.RhymeLetters)) + if (oldMetreId != null && !string.IsNullOrEmpty(section.RhymeLetters)) { - await _UpdateRelatedPoems(context, (int)oldMetreId, poem.RhymeLetters); + await _UpdateRelatedSections(context, (int)oldMetreId, section.RhymeLetters); await context.SaveChangesAsync(); } - if (poem.GanjoorMetreId != null && !string.IsNullOrEmpty(poem.RhymeLetters)) + if (section.GanjoorMetreId != null && !string.IsNullOrEmpty(section.RhymeLetters)) { - await _UpdateRelatedPoems(context, (int)poem.GanjoorMetreId, poem.RhymeLetters); + await _UpdateRelatedSections(context, (int)section.GanjoorMetreId, section.RhymeLetters); await context.SaveChangesAsync(); } }