From 9fdd65be293eba608743ec2a2cd7ddbb97b05103 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Thu, 24 Dec 2020 18:20:46 +0330 Subject: [PATCH] improved API for poem by id --- RMuseum/Controllers/GanjoorController.cs | 14 ++- RMuseum/RMuseum.xml | 38 ++++-- RMuseum/Services/IGanjoorService.cs | 10 +- .../Services/Implementation/GanjoorService.cs | 119 +++++++++++------- 4 files changed, 121 insertions(+), 60 deletions(-) diff --git a/RMuseum/Controllers/GanjoorController.cs b/RMuseum/Controllers/GanjoorController.cs index 5c94e690..569c778b 100644 --- a/RMuseum/Controllers/GanjoorController.cs +++ b/RMuseum/Controllers/GanjoorController.cs @@ -83,16 +83,22 @@ namespace RMuseum.Controllers /// get poem by id /// /// + /// + /// not implemented yet + /// + /// + /// not implemented yet + /// not implemented yet /// [HttpGet] [Route("poem/{id}")] [AllowAnonymous] - [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorPoem))] + [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorPoemCompleteViewModel))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] - public async Task GetPoemById(int id) + public async Task GetPoemById(int id, bool catInfo = true, bool rhymes = true, bool recitations = true, bool images = true, bool songs = true, bool comments = true) { - RServiceResult res = - await _ganjoorService.GetPoemById(id); + RServiceResult res = + await _ganjoorService.GetPoemById(id, catInfo, rhymes, recitations, images, songs, comments); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(res.Result); diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index 6bec737f..c7124d5f 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -636,11 +636,17 @@ - + get poem by id + + not implemented yet + + + not implemented yet + not implemented yet @@ -4612,11 +4618,17 @@ - + - get poem by id + Get Poem By Id + + + + + + @@ -5372,13 +5384,6 @@ - - - get poem by id - - - - get poem recitations (PlainText/HtmlText are intentionally empty) @@ -5394,6 +5399,19 @@ + + + Get Poem By Id + + + + + + + + + + get a random poem from hafez diff --git a/RMuseum/Services/IGanjoorService.cs b/RMuseum/Services/IGanjoorService.cs index c6303213..eca9d136 100644 --- a/RMuseum/Services/IGanjoorService.cs +++ b/RMuseum/Services/IGanjoorService.cs @@ -35,11 +35,17 @@ namespace RMuseum.Services /// - /// get poem by id + /// Get Poem By Id /// /// + /// + /// + /// + /// + /// + /// /// - Task> GetPoemById(int id); + Task> GetPoemById(int id, bool catInfo = true, bool rhymes = true, bool recitations = true, bool images = true, bool songs = true, bool comments = true); /// /// get poem recitations (PlainText/HtmlText are intentionally empty) diff --git a/RMuseum/Services/Implementation/GanjoorService.cs b/RMuseum/Services/Implementation/GanjoorService.cs index 76dfc466..75117d90 100644 --- a/RMuseum/Services/Implementation/GanjoorService.cs +++ b/RMuseum/Services/Implementation/GanjoorService.cs @@ -164,33 +164,7 @@ namespace RMuseum.Services.Implementation } - /// - /// get poem by id - /// - /// - /// - public async Task> GetPoemById(int id) - { - try - { - var poem = await _context.GanjoorPoems.Include(p => p.Cat).Where(p => p.Id == id).SingleOrDefaultAsync(); - if (poem == null) - return new RServiceResult(null); - var cat = poem.Cat; - while(cat != null) - { - cat.Parent = await _context.GanjoorCategories.Where(c => c.Id == cat.ParentId).SingleOrDefaultAsync(); - cat = cat.Parent; - } - - return new RServiceResult(poem); - } - catch(Exception exp) - { - return new RServiceResult(null, exp.ToString()); - } - } - + /// /// get poem recitations (PlainText/HtmlText are intentionally empty) /// @@ -292,6 +266,78 @@ namespace RMuseum.Services.Implementation return poemId; } + /// + /// Get Poem By Id + /// + /// + /// + /// + /// + /// + /// + /// + /// + public async Task> GetPoemById(int id, bool catInfo = true, bool rhymes = true, bool recitations = true, bool images = true, bool songs = true, bool comments = true) + { + try + { + var poem = await _context.GanjoorPoems.Where(p => p.Id == id).SingleOrDefaultAsync(); + if(poem == null) + { + return new RServiceResult(null); //not found + } + GanjoorPoetCompleteViewModel cat = null; + if(catInfo) + { + var catRes = await GetCatById(poem.CatId); + if(!string.IsNullOrEmpty(catRes.ExceptionString)) + { + return new RServiceResult(null, catRes.ExceptionString); + } + } + PublicRecitationViewModel[] rc = null; + if(recitations) + { + var rcRes = await GetPoemRecitations(id); + if (!string.IsNullOrEmpty(rcRes.ExceptionString)) + return new RServiceResult(null, rcRes.ExceptionString); + rc = rcRes.Result; + } + + GanjoorLinkViewModel[] imgs = null; + if(images) + { + var imgsRes = await GetPoemImages(id); + if (!string.IsNullOrEmpty(imgsRes.ExceptionString)) + return new RServiceResult(null, imgsRes.ExceptionString); + imgs = imgsRes.Result; + } + + + + return new RServiceResult + ( + new GanjoorPoemCompleteViewModel() + { + Id = poem.Id, + Title = poem.Title, + FullTitle = poem.FullTitle, + FullUrl = poem.FullUrl, + UrlSlug = poem.UrlSlug, + HtmlText = poem.HtmlText, + PlainText = poem.PlainText, + Category = cat, + Recitations = rc, + Images = imgs + } + ); + } + catch (Exception exp) + { + return new RServiceResult(null, exp.ToString()); + } + } + /// /// get a random poem from hafez /// @@ -306,29 +352,14 @@ namespace RMuseum.Services.Implementation int loopPreventer = 0; while (poem == null || recitations.Length == 0) { - poem = (await GetPoemById(poemId)).Result; + poem = await _context.GanjoorPoems.Where(p => p.Id == poemId).SingleOrDefaultAsync(); recitations = poem == null ? new PublicRecitationViewModel[] { } : (await GetPoemRecitations(poemId)).Result; loopPreventer++; if (loopPreventer > 5) break; } - return new RServiceResult - ( - new GanjoorPoemCompleteViewModel() - { - Id = poem.Id, - Title = poem.Title, - FullTitle = poem.FullTitle, - FullUrl = poem.FullUrl, - UrlSlug = poem.UrlSlug, - HtmlText = poem.HtmlText, - PlainText = poem.PlainText, - Category = null,//no usage for now, so do not waste resources - Recitations = recitations, - Images = null //no usage for now, so do not waste resources - } - ); + return await GetPoemById(poemId, false, false, true, false, false, false); } catch (Exception exp) {