#101 similar poems api

This commit is contained in:
Hamid Reza Mohammadi 2021-03-28 11:25:37 +04:30
parent 4927c78dcd
commit 1d1a85d5ca
4 changed files with 149 additions and 0 deletions

View File

@ -854,6 +854,33 @@ namespace RMuseum.Controllers
return Ok(pagedResult.Result.items);
}
/// <summary>
/// Get Similar Poems accroding to prosody and rhyme informations
/// </summary>
/// <param name="paging"></param>
/// <param name="metre">cannot be empty</param>
/// <param name="rhyme">can be empty</param>
/// <param name="poetId">send 0 for all</param>
/// <returns>return value is not complete or valid for some parts, you should use only the valid parts!</returns>
[HttpGet]
[Route("poems/similar")]
[AllowAnonymous]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable<GanjoorSearchVerseViewModel>))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public async Task<IActionResult> 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);
}
/// <summary>
/// Ganjoor Service
/// </summary>

View File

@ -859,6 +859,16 @@
<param name="paging"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.GetSimilarPoems(RSecurityBackend.Models.Generic.PagingParameterModel,System.String,System.String,System.Int32)">
<summary>
Get Similar Poems accroding to prosody and rhyme informations
</summary>
<param name="paging"></param>
<param name="metre">cannot be empty</param>
<param name="rhyme">can be empty</param>
<param name="poetId">send 0 for all</param>
<returns>return value is not complete or valid for some parts, you should use only the valid parts!</returns>
</member>
<member name="F:RMuseum.Controllers.GanjoorController._ganjoorService">
<summary>
Ganjoor Service
@ -6428,6 +6438,16 @@
<param name="paging"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.GetSimilarPoems(RSecurityBackend.Models.Generic.PagingParameterModel,System.String,System.String,System.Nullable{System.Int32})">
<summary>
Get Similar Poems accroding to prosody and rhyme informations
</summary>
<param name="paging"></param>
<param name="metre"></param>
<param name="rhyme"></param>
<param name="poetId"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.Faal">
<summary>
get a random poem from hafez
@ -7423,6 +7443,16 @@
</summary>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.GetSimilarPoems(RSecurityBackend.Models.Generic.PagingParameterModel,System.String,System.String,System.Nullable{System.Int32})">
<summary>
Get Similar Poems accroding to prosody and rhyme informations
</summary>
<param name="paging"></param>
<param name="metre"></param>
<param name="rhyme"></param>
<param name="poetId"></param>
<returns></returns>
</member>
<member name="F:RMuseum.Services.Implementation.GanjoorService._context">
<summary>
Database Context

View File

@ -232,6 +232,16 @@ namespace RMuseum.Services
/// <returns></returns>
Task<RServiceResult<(PaginationMetadata PagingMeta, GanjoorCommentAbuseReportViewModel[] Items)>> GetReportedComments(PagingParameterModel paging);
/// <summary>
/// Get Similar Poems accroding to prosody and rhyme informations
/// </summary>
/// <param name="paging"></param>
/// <param name="metre"></param>
/// <param name="rhyme"></param>
/// <param name="poetId"></param>
/// <returns></returns>
Task<RServiceResult<(PaginationMetadata PagingMeta, GanjoorPoemCompleteViewModel[] Items)>> GetSimilarPoems(PagingParameterModel paging, string metre, string rhyme, int? poetId);
/// <summary>
/// get a random poem from hafez
/// </summary>

View File

@ -1649,6 +1649,88 @@ namespace RMuseum.Services.Implementation
}
}
/// <summary>
/// Get Similar Poems accroding to prosody and rhyme informations
/// </summary>
/// <param name="paging"></param>
/// <param name="metre"></param>
/// <param name="rhyme"></param>
/// <param name="poetId"></param>
/// <returns></returns>
public async Task<RServiceResult<(PaginationMetadata PagingMeta, GanjoorPoemCompleteViewModel[] Items)>> 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<GanjoorPoemCompleteViewModel>.Paginate(source, paging);
Dictionary<int, GanjoorPoetCompleteViewModel> cachedPoets = new Dictionary<int, GanjoorPoetCompleteViewModel>();
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());
}
}
/// <summary>
/// Database Context
/// </summary>