diff --git a/RMuseum/Controllers/GanjoorController.cs b/RMuseum/Controllers/GanjoorController.cs index 0939184d..f069a044 100644 --- a/RMuseum/Controllers/GanjoorController.cs +++ b/RMuseum/Controllers/GanjoorController.cs @@ -854,6 +854,33 @@ namespace RMuseum.Controllers return Ok(pagedResult.Result.items); } + /// + /// Get Similar Poems accroding to prosody and rhyme informations + /// + /// + /// cannot be empty + /// can be empty + /// send 0 for all + /// return value is not complete or valid for some parts, you should use only the valid parts! + + [HttpGet] + [Route("poems/similar")] + [AllowAnonymous] + [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable))] + [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] + + public async Task GetSimilarPoems([FromQuery]PagingParameterModel paging, string metre, string rhyme, int poetId = 0) + { + var pagedResult = await _ganjoorService.GetSimilarPoems(paging, metre, rhyme, 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); + } + /// /// Ganjoor Service /// diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index ff4ce099..d452a3ac 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -859,6 +859,16 @@ + + + Get Similar Poems accroding to prosody and rhyme informations + + + cannot be empty + can be empty + send 0 for all + return value is not complete or valid for some parts, you should use only the valid parts! + Ganjoor Service @@ -6428,6 +6438,16 @@ + + + Get Similar Poems accroding to prosody and rhyme informations + + + + + + + get a random poem from hafez @@ -7423,6 +7443,16 @@ + + + Get Similar Poems accroding to prosody and rhyme informations + + + + + + + Database Context diff --git a/RMuseum/Services/IGanjoorService.cs b/RMuseum/Services/IGanjoorService.cs index 90d3bf84..ba802817 100644 --- a/RMuseum/Services/IGanjoorService.cs +++ b/RMuseum/Services/IGanjoorService.cs @@ -232,6 +232,16 @@ namespace RMuseum.Services /// Task> GetReportedComments(PagingParameterModel paging); + /// + /// Get Similar Poems accroding to prosody and rhyme informations + /// + /// + /// + /// + /// + /// + Task> GetSimilarPoems(PagingParameterModel paging, string metre, string rhyme, int? poetId); + /// /// get a random poem from hafez /// diff --git a/RMuseum/Services/Implementation/GanjoorService.cs b/RMuseum/Services/Implementation/GanjoorService.cs index f901c9a7..4c623012 100644 --- a/RMuseum/Services/Implementation/GanjoorService.cs +++ b/RMuseum/Services/Implementation/GanjoorService.cs @@ -1649,6 +1649,88 @@ namespace RMuseum.Services.Implementation } } + /// + /// Get Similar Poems accroding to prosody and rhyme informations + /// + /// + /// + /// + /// + /// + public async Task> GetSimilarPoems(PagingParameterModel paging, string metre, string rhyme, int? poetId) + { + try + { + if (string.IsNullOrEmpty(rhyme)) + rhyme = ""; + var source = + _context.GanjoorPoems.Include(p => p.Cat).ThenInclude(c => c.Poet).Include(p => p.GanjoorMetre) + .Where(p => + (poetId == null || p.Cat.PoetId == poetId) + && + (p.GanjoorMetre.Rhythm == metre) + && + (rhyme == "" || p.RhymeLetters == rhyme) + ) + .OrderBy(p => p.CatId).ThenBy(p => p.Id) + .Select + ( + poem => + new GanjoorPoemCompleteViewModel() + { + Id = poem.Id, + Title = poem.Title, + FullTitle = poem.FullTitle, + FullUrl = poem.FullUrl, + UrlSlug = poem.UrlSlug, + HtmlText = poem.HtmlText, + PlainText = poem.PlainText, + GanjoorMetre = poem.GanjoorMetre, + RhymeLetters = poem.RhymeLetters, + Category = new GanjoorPoetCompleteViewModel() + { + Poet = new GanjoorPoetViewModel() + { + Id = poem.Cat.Poet.Id, + } + }, + + } + ); + + + (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); + } + catch(Exception exp) + { + return new RServiceResult<(PaginationMetadata PagingMeta, GanjoorPoemCompleteViewModel[] Items)>((null, null), exp.ToString()); + } + } + /// /// Database Context ///