From 96890e36f6b9e68a847aaa3fe27583072b8116e9 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Thu, 16 Feb 2023 21:17:14 +0330 Subject: [PATCH] docs: #365 API for querying sections by language --- RMuseum/Controllers/GanjoorController.cs | 25 ++++++ RMuseum/RMuseum.xml | 27 +++++++ RMuseum/Services/IGanjoorService.cs | 9 +++ .../Services/Implementation/GanjoorService.cs | 76 +++++++++++++++++++ 4 files changed, 137 insertions(+) diff --git a/RMuseum/Controllers/GanjoorController.cs b/RMuseum/Controllers/GanjoorController.cs index 625ae65c..cd4dd727 100644 --- a/RMuseum/Controllers/GanjoorController.cs +++ b/RMuseum/Controllers/GanjoorController.cs @@ -2247,6 +2247,31 @@ namespace RMuseum.Controllers return Ok(pagedResult.Result.Items); } + /// + /// language tagged poem sections + /// + /// + /// fa-IR, ar, ... + /// 0 means all poets + /// + + [HttpGet] + [Route("sections/tagged/language")] + [AllowAnonymous] + [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable))] + [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] + public async Task GetLanguageTaggedPoemSections([FromQuery] PagingParameterModel paging, string language, int poetId = 0) + { + var pagedResult = await _ganjoorService.GetLanguageTaggedPoemSections(paging, language, poetId == 0 ? (int?)null : poetId); + if (!string.IsNullOrEmpty(pagedResult.ExceptionString)) + return BadRequest(pagedResult.ExceptionString); + + // Paging Header + HttpContext.Response.Headers.Add("paging-headers", JsonConvert.SerializeObject(pagedResult.Result.PagingMeta)); + + return Ok(pagedResult.Result.Items); + } + /// /// search /// diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index d7049d05..c66634a1 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -1529,6 +1529,15 @@ send 0 for all return value is not complete or valid for some parts, you should use only the valid parts! + + + language tagged poem sections + + + fa-IR, ar, ... + 0 means all poets + + search @@ -12564,6 +12573,15 @@ + + + language tagged poem sections + + + + + + modify page @@ -15607,6 +15625,15 @@ + + + language tagged poem sections + + + + + + Search diff --git a/RMuseum/Services/IGanjoorService.cs b/RMuseum/Services/IGanjoorService.cs index 065d8b27..dd29d1c0 100644 --- a/RMuseum/Services/IGanjoorService.cs +++ b/RMuseum/Services/IGanjoorService.cs @@ -451,6 +451,15 @@ namespace RMuseum.Services /// Task> GetSimilarPoems(PagingParameterModel paging, string metre, string rhyme, int? poetId); + /// + /// language tagged poem sections + /// + /// + /// + /// + /// + Task> GetLanguageTaggedPoemSections(PagingParameterModel paging, string language, int? poetId); + /// /// modify page /// diff --git a/RMuseum/Services/Implementation/GanjoorService.cs b/RMuseum/Services/Implementation/GanjoorService.cs index 5276ebf7..ffe40912 100644 --- a/RMuseum/Services/Implementation/GanjoorService.cs +++ b/RMuseum/Services/Implementation/GanjoorService.cs @@ -2331,6 +2331,82 @@ namespace RMuseum.Services.Implementation } } + /// + /// language tagged poem sections + /// + /// + /// + /// + /// + public async Task> GetLanguageTaggedPoemSections(PagingParameterModel paging, string language, int? poetId) + { + if(string.IsNullOrEmpty(language)) + { + language = "fa-IR"; + } + var source = + _context.GanjoorPoemSections.Include(s => s.Poem).Include(s => s.Poet).Include(s => s.GanjoorMetre) + .Where(s => + (poetId == null || s.PoetId == poetId) + && + ((language == "fa-IR" && string.IsNullOrEmpty(s.Language)) || s.Language == language) + && + s.SectionType == PoemSectionType.WholePoem + ) + .OrderBy(p => p.Poet.BirthYearInLHijri).ThenBy(p => p.Poet.Nickname).ThenBy(p => p.Poem.Id) + .Select + ( + section => + new GanjoorPoemCompleteViewModel() + { + Id = section.Poem.Id, + Title = section.Poem.Title, + FullTitle = section.Poem.FullTitle, + FullUrl = section.CachedFirstCoupletIndex == 0 ? section.Poem.FullUrl : section.Poem.FullUrl + "#bn" + (section.CachedFirstCoupletIndex + 1).ToString(), + UrlSlug = section.Poem.UrlSlug, + HtmlText = section.HtmlText, + PlainText = section.PlainText, + MixedModeOrder = section.Poem.MixedModeOrder, + Published = section.Poem.Published, + Language = section.Poem.Language, + Category = new GanjoorPoetCompleteViewModel() + { + Poet = new GanjoorPoetViewModel() + { + Id = section.Poet.Id, + } + }, + SectionIndex = section.Index + + } + ).AsNoTracking(); + + + (PaginationMetadata PagingMeta, GanjoorPoemCompleteViewModel[] Items) paginatedResult = + await QueryablePaginator.Paginate(source, paging); + + + Dictionary cachedPoets = new Dictionary(); + + foreach (var item in paginatedResult.Items) + { + if (cachedPoets.TryGetValue(item.Category.Poet.Id, out GanjoorPoetCompleteViewModel poet)) + { + item.Category = poet; + } + else + { + poet = (await GetPoetById(item.Category.Poet.Id)).Result; + + cachedPoets.Add(item.Category.Poet.Id, poet); + + item.Category = poet; + } + } + + return new RServiceResult<(PaginationMetadata PagingMeta, GanjoorPoemCompleteViewModel[] Items)>(paginatedResult); + } + ///