#110 api added (ganjoor/poem/random)

This commit is contained in:
Hamid Reza Mohammadi 2021-05-18 16:49:12 +04:30
parent d79c575490
commit 16ab4ca6da
4 changed files with 104 additions and 18 deletions

View File

@ -564,7 +564,27 @@ namespace RMuseum.Controllers
public async Task<IActionResult> Faal()
{
RServiceResult<GanjoorPoemCompleteViewModel> res =
await _ganjoorService.Faal();
await _ganjoorService.Faal(2, true);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
return Ok(res.Result);
}
/// <summary>
/// get a random poem from a poet (c.ganjoor.net replacement), 0 means random poet,
/// حافظ (2)، خیام (3)، ابوسعید ابوالخیر (26)، صائب (22)، سعدی (7)، باباطاهر (28)، مولوی (5)، اوحدی (19)، خواجو (20)، شهریار (35)، عراقی (21)، فروغی بسطامی (32)، سلمان ساوجی (40)، محتشم کاشانی (29)، امیرخسرو دهلوی (34)، سیف فرغانی (31)، عبید زاکانی (33)، هاتف اصفهانی (25) یا رهی معیری (41)
/// </summary>
/// <param name="poetId"></param>
/// <returns></returns>
[HttpGet]
[Route("poem/random")]
[AllowAnonymous]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorPoemCompleteViewModel))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public async Task<IActionResult> GetARandomPoem(int poetId)
{
RServiceResult<GanjoorPoemCompleteViewModel> res =
await _ganjoorService.Faal(poetId, false);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
return Ok(res.Result);

View File

@ -766,6 +766,14 @@
</summary>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.GetARandomPoem(System.Int32)">
<summary>
get a random poem from a poet (c.ganjoor.net replacement), 0 means random poet,
حافظ (2)، خیام (3)، ابوسعید ابوالخیر (26)، صائب (22)، سعدی (7)، باباطاهر (28)، مولوی (5)، اوحدی (19)، خواجو (20)، شهریار (35)، عراقی (21)، فروغی بسطامی (32)، سلمان ساوجی (40)، محتشم کاشانی (29)، امیرخسرو دهلوی (34)، سیف فرغانی (31)، عبید زاکانی (33)، هاتف اصفهانی (25) یا رهی معیری (41)
</summary>
<param name="poetId"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.GetRecentComments(RSecurityBackend.Models.Generic.PagingParameterModel,System.Nullable{System.Guid})">
<summary>
get recent comments
@ -6817,10 +6825,12 @@
</summary>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.Faal">
<member name="M:RMuseum.Services.IGanjoorService.Faal(System.Int32,System.Boolean)">
<summary>
get a random poem from hafez
get a random poem (2 = from hafez)
</summary>
<param name="poetId"></param>
<param name="recitation"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.ImportLocalSQLiteDb">
@ -7862,16 +7872,18 @@
<param name="song"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService._GetRandomPoemId(System.Int32)">
<member name="M:RMuseum.Services.Implementation.GanjoorService._GetRandomPoemId(System.Int32,System.Int32)">
<summary>
random poem id from hafez sonnets
random poem id from hafez sonnets and old c.ganjoor.net service
</summary>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.Faal">
<member name="M:RMuseum.Services.Implementation.GanjoorService.Faal(System.Int32,System.Boolean)">
<summary>
get a random poem from hafez
</summary>
<param name="poetId"></param>
<param name="recitation"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.GetVersesByQuery(System.String,System.Int32,RSecurityBackend.Models.Generic.PagingParameterModel)">

View File

@ -299,10 +299,12 @@ namespace RMuseum.Services
Task<RServiceResult<GanjoorSiteBannerViewModel>> GetARandomActiveSiteBanner();
/// <summary>
/// get a random poem from hafez
/// get a random poem (2 = from hafez)
/// </summary>
/// <param name="poetId"></param>
/// <param name="recitation"></param>
/// <returns></returns>
Task<RServiceResult<GanjoorPoemCompleteViewModel>> Faal();
Task<RServiceResult<GanjoorPoemCompleteViewModel>> Faal(int poetId = 2, bool recitation = true);
/// <summary>
/// imports unimported poem data from a locally accessible ganjoor SqlLite database

View File

@ -1697,12 +1697,15 @@ namespace RMuseum.Services.Implementation
/// <summary>
/// random poem id from hafez sonnets
/// random poem id from hafez sonnets and old c.ganjoor.net service
/// </summary>
/// <returns></returns>
private int _GetRandomPoemId(int poetId)
private int _GetRandomPoemId(int poetId, int loopBreaker = 0)
{
if (loopBreaker > 10)
return 0;
Random r = new Random(DateTime.Now.Millisecond);
switch (poetId)
{
case 2://حافظ
@ -1730,10 +1733,55 @@ namespace RMuseum.Services.Implementation
case 5://مولانا
return r.Next(2625, 5853);
case 19://اوحدی
return r.Next(16955, 5853);
return r.Next(16955, 17839);
case 35://شهریار
return r.Next(27065, 27224);
case 20://خواجو
return r.Next(18288, 19219);
case 32://فروغی
return r.Next(22996, 23511);
case 21://عراقی
return r.Next(19222, 19526);
case 40://سلمان
return r.Next(38411, 39320);
case 29://محتشم
return r.Next(21744, 22338);
case 34://امیرخسرو
return r.Next(60582, 62578);
case 31://سیف
return r.Next(62837, 63418);
case 33://عبید
return r.Next(23551, 23656);
case 25://هاتف
return r.Next(20275, 20364);
case 41://رهی
return r.Next(39441, 39546);
}
return 0;
int[] poetIdArray = new int[]
{
2,
3,
26,
22,
7,
28,
5,
19,
35,
20,
23,
21,
40,
29,
34,
31,
33,
25,
41
};
return _GetRandomPoemId(poetIdArray[r.Next(0, poetIdArray.Length - 1)], loopBreaker++);
}
@ -1742,25 +1790,29 @@ namespace RMuseum.Services.Implementation
/// <summary>
/// get a random poem from hafez
/// </summary>
/// <param name="poetId"></param>
/// <param name="recitation"></param>
/// <returns></returns>
public async Task<RServiceResult<GanjoorPoemCompleteViewModel>> Faal()
public async Task<RServiceResult<GanjoorPoemCompleteViewModel>> Faal(int poetId = 2, bool recitation = true)
{
try
{
int poemId = _GetRandomPoemId(2);
int poemId = _GetRandomPoemId(poetId);
var poem = await _context.GanjoorPoems.Where(p => p.Id == poemId).AsNoTracking().SingleOrDefaultAsync();
PublicRecitationViewModel[] recitations = poem == null ? new PublicRecitationViewModel[] { } : (await GetPoemRecitations(poemId)).Result;
PublicRecitationViewModel[] recitations = poem == null || !recitation ? new PublicRecitationViewModel[] { } : (await GetPoemRecitations(poemId)).Result;
int loopPreventer = 0;
while (poem == null || recitations.Length == 0)
while (poem == null || (recitation && recitations.Length == 0))
{
poem = await _context.GanjoorPoems.Where(p => p.Id == poemId).AsNoTracking().SingleOrDefaultAsync();
recitations = poem == null ? new PublicRecitationViewModel[] { } : (await GetPoemRecitations(poemId)).Result;
loopPreventer++;
if (loopPreventer > 5)
break;
{
return new RServiceResult<GanjoorPoemCompleteViewModel>(null);
}
}
return await GetPoemById(poemId, false, false, false, true, false, false, false, false, false);
return await GetPoemById(poemId, false, false, false, recitation, false, false, false, true /*verse details*/, false);
}
catch (Exception exp)
{