improved API for poem by id

This commit is contained in:
Hamid Reza Mohammadi 2020-12-24 18:20:46 +03:30
parent 8084b6f6de
commit 9fdd65be29
4 changed files with 121 additions and 60 deletions

View File

@ -83,16 +83,22 @@ namespace RMuseum.Controllers
/// get poem by id
/// </summary>
/// <param name="id"></param>
/// <param name="catInfo"></param>
/// <param name="rhymes">not implemented yet</param>
/// <param name="recitations"></param>
/// <param name="images"></param>
/// <param name="songs">not implemented yet</param>
/// <param name="comments">not implemented yet</param>
/// <returns></returns>
[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<IActionResult> GetPoemById(int id)
public async Task<IActionResult> GetPoemById(int id, bool catInfo = true, bool rhymes = true, bool recitations = true, bool images = true, bool songs = true, bool comments = true)
{
RServiceResult<GanjoorPoem> res =
await _ganjoorService.GetPoemById(id);
RServiceResult<GanjoorPoemCompleteViewModel> res =
await _ganjoorService.GetPoemById(id, catInfo, rhymes, recitations, images, songs, comments);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
return Ok(res.Result);

View File

@ -636,11 +636,17 @@
<param name="id"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.GetPoemById(System.Int32)">
<member name="M:RMuseum.Controllers.GanjoorController.GetPoemById(System.Int32,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean)">
<summary>
get poem by id
</summary>
<param name="id"></param>
<param name="catInfo"></param>
<param name="rhymes">not implemented yet</param>
<param name="recitations"></param>
<param name="images"></param>
<param name="songs">not implemented yet</param>
<param name="comments">not implemented yet</param>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.GetPoemRecitations(System.Int32)">
@ -4612,11 +4618,17 @@
<param name="id"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.GetPoemById(System.Int32)">
<member name="M:RMuseum.Services.IGanjoorService.GetPoemById(System.Int32,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean)">
<summary>
get poem by id
Get Poem By Id
</summary>
<param name="id"></param>
<param name="catInfo"></param>
<param name="rhymes"></param>
<param name="recitations"></param>
<param name="images"></param>
<param name="songs"></param>
<param name="comments"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.GetPoemRecitations(System.Int32)">
@ -5372,13 +5384,6 @@
<param name="id"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.GetPoemById(System.Int32)">
<summary>
get poem by id
</summary>
<param name="id"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.GetPoemRecitations(System.Int32)">
<summary>
get poem recitations (PlainText/HtmlText are intentionally empty)
@ -5394,6 +5399,19 @@
<param name="id"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.GetPoemById(System.Int32,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean)">
<summary>
Get Poem By Id
</summary>
<param name="id"></param>
<param name="catInfo"></param>
<param name="rhymes"></param>
<param name="recitations"></param>
<param name="images"></param>
<param name="songs"></param>
<param name="comments"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.Faal">
<summary>
get a random poem from hafez

View File

@ -35,11 +35,17 @@ namespace RMuseum.Services
/// <summary>
/// get poem by id
/// Get Poem By Id
/// </summary>
/// <param name="id"></param>
/// <param name="catInfo"></param>
/// <param name="rhymes"></param>
/// <param name="recitations"></param>
/// <param name="images"></param>
/// <param name="songs"></param>
/// <param name="comments"></param>
/// <returns></returns>
Task<RServiceResult<GanjoorPoem>> GetPoemById(int id);
Task<RServiceResult<GanjoorPoemCompleteViewModel>> GetPoemById(int id, bool catInfo = true, bool rhymes = true, bool recitations = true, bool images = true, bool songs = true, bool comments = true);
/// <summary>
/// get poem recitations (PlainText/HtmlText are intentionally empty)

View File

@ -164,33 +164,7 @@ namespace RMuseum.Services.Implementation
}
/// <summary>
/// get poem by id
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<RServiceResult<GanjoorPoem>> 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<GanjoorPoem>(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<GanjoorPoem>(poem);
}
catch(Exception exp)
{
return new RServiceResult<GanjoorPoem>(null, exp.ToString());
}
}
/// <summary>
/// get poem recitations (PlainText/HtmlText are intentionally empty)
/// </summary>
@ -292,6 +266,78 @@ namespace RMuseum.Services.Implementation
return poemId;
}
/// <summary>
/// Get Poem By Id
/// </summary>
/// <param name="id"></param>
/// <param name="catInfo"></param>
/// <param name="rhymes"></param>
/// <param name="recitations"></param>
/// <param name="images"></param>
/// <param name="songs"></param>
/// <param name="comments"></param>
/// <returns></returns>
public async Task<RServiceResult<GanjoorPoemCompleteViewModel>> 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<GanjoorPoemCompleteViewModel>(null); //not found
}
GanjoorPoetCompleteViewModel cat = null;
if(catInfo)
{
var catRes = await GetCatById(poem.CatId);
if(!string.IsNullOrEmpty(catRes.ExceptionString))
{
return new RServiceResult<GanjoorPoemCompleteViewModel>(null, catRes.ExceptionString);
}
}
PublicRecitationViewModel[] rc = null;
if(recitations)
{
var rcRes = await GetPoemRecitations(id);
if (!string.IsNullOrEmpty(rcRes.ExceptionString))
return new RServiceResult<GanjoorPoemCompleteViewModel>(null, rcRes.ExceptionString);
rc = rcRes.Result;
}
GanjoorLinkViewModel[] imgs = null;
if(images)
{
var imgsRes = await GetPoemImages(id);
if (!string.IsNullOrEmpty(imgsRes.ExceptionString))
return new RServiceResult<GanjoorPoemCompleteViewModel>(null, imgsRes.ExceptionString);
imgs = imgsRes.Result;
}
return new RServiceResult<GanjoorPoemCompleteViewModel>
(
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<GanjoorPoemCompleteViewModel>(null, exp.ToString());
}
}
/// <summary>
/// get a random poem from hafez
/// </summary>
@ -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<GanjoorPoemCompleteViewModel>
(
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)
{