From 16ab4ca6da9306813cfa04b9d81ccb11cb884329 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Tue, 18 May 2021 16:49:12 +0430 Subject: [PATCH] #110 api added (ganjoor/poem/random) --- RMuseum/Controllers/GanjoorController.cs | 22 +++++- RMuseum/RMuseum.xml | 22 ++++-- RMuseum/Services/IGanjoorService.cs | 6 +- .../Services/Implementation/GanjoorService.cs | 72 ++++++++++++++++--- 4 files changed, 104 insertions(+), 18 deletions(-) diff --git a/RMuseum/Controllers/GanjoorController.cs b/RMuseum/Controllers/GanjoorController.cs index 993c76c1..56fb8083 100644 --- a/RMuseum/Controllers/GanjoorController.cs +++ b/RMuseum/Controllers/GanjoorController.cs @@ -564,7 +564,27 @@ namespace RMuseum.Controllers public async Task Faal() { RServiceResult res = - await _ganjoorService.Faal(); + await _ganjoorService.Faal(2, true); + if (!string.IsNullOrEmpty(res.ExceptionString)) + return BadRequest(res.ExceptionString); + return Ok(res.Result); + } + + /// + /// 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) + /// + /// + /// + [HttpGet] + [Route("poem/random")] + [AllowAnonymous] + [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorPoemCompleteViewModel))] + [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] + public async Task GetARandomPoem(int poetId) + { + RServiceResult res = + await _ganjoorService.Faal(poetId, false); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(res.Result); diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index b62482be..c6ef6fa6 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -766,6 +766,14 @@ + + + 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) + + + + get recent comments @@ -6817,10 +6825,12 @@ - + - get a random poem from hafez + get a random poem (2 = from hafez) + + @@ -7862,16 +7872,18 @@ - + - random poem id from hafez sonnets + random poem id from hafez sonnets and old c.ganjoor.net service - + get a random poem from hafez + + diff --git a/RMuseum/Services/IGanjoorService.cs b/RMuseum/Services/IGanjoorService.cs index ae1a5aef..0d598d72 100644 --- a/RMuseum/Services/IGanjoorService.cs +++ b/RMuseum/Services/IGanjoorService.cs @@ -299,10 +299,12 @@ namespace RMuseum.Services Task> GetARandomActiveSiteBanner(); /// - /// get a random poem from hafez + /// get a random poem (2 = from hafez) /// + /// + /// /// - Task> Faal(); + Task> Faal(int poetId = 2, bool recitation = true); /// /// imports unimported poem data from a locally accessible ganjoor SqlLite database diff --git a/RMuseum/Services/Implementation/GanjoorService.cs b/RMuseum/Services/Implementation/GanjoorService.cs index 513b8dcf..ff51cf28 100644 --- a/RMuseum/Services/Implementation/GanjoorService.cs +++ b/RMuseum/Services/Implementation/GanjoorService.cs @@ -1697,12 +1697,15 @@ namespace RMuseum.Services.Implementation /// - /// random poem id from hafez sonnets + /// random poem id from hafez sonnets and old c.ganjoor.net service /// /// - 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 /// /// get a random poem from hafez /// + /// + /// /// - public async Task> Faal() + public async Task> 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(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) {