#53 cat subcategories

This commit is contained in:
Hamid Reza Mohammadi 2020-12-21 21:20:14 +03:30
parent 00a29085f6
commit acbe582f58
4 changed files with 66 additions and 9 deletions

View File

@ -45,12 +45,37 @@ namespace RMuseum.Controllers
[AllowAnonymous]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorPoetCompleteViewModel))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
[ProducesResponseType((int)HttpStatusCode.NotFound)]
public async Task<IActionResult> GetPoetById(int id)
{
RServiceResult<GanjoorPoetCompleteViewModel> res =
await _ganjoorService.GetPoetById(id);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
if (res.Result == null)
return NotFound();
return Ok(res.Result);
}
/// <summary>
/// cat by id
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet]
[Route("cat/{id}")]
[AllowAnonymous]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorPoetCompleteViewModel))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
[ProducesResponseType((int)HttpStatusCode.NotFound)]
public async Task<IActionResult> GetCatById(int id)
{
RServiceResult<GanjoorPoetCompleteViewModel> res =
await _ganjoorService.GetCatById(id);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
if (res.Result == null)
return NotFound();
return Ok(res.Result);
}

View File

@ -629,6 +629,13 @@
<param name="id"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.GetCatById(System.Int32)">
<summary>
cat by id
</summary>
<param name="id"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.GetPoemById(System.Int32)">
<summary>
get poem by id
@ -4488,6 +4495,13 @@
<param name="id"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.GetCatById(System.Int32)">
<summary>
get cat by id
</summary>
<param name="id"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.GetPoemById(System.Int32)">
<summary>
get poem by id
@ -5241,11 +5255,11 @@
<param name="id"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.GetPoetByUrl(System.String)">
<member name="M:RMuseum.Services.Implementation.GanjoorService.GetCatById(System.Int32)">
<summary>
get poet by url
get cat by id
</summary>
<param name="url">base web site address should be removed: hafez or /hafez and not https://ganjoor.net/hafez </param>
<param name="id"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.GetPoemById(System.Int32)">

View File

@ -26,6 +26,13 @@ namespace RMuseum.Services
/// <returns></returns>
Task<RServiceResult<GanjoorPoetCompleteViewModel>> GetPoetById(int id);
/// <summary>
/// get cat by id
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
Task<RServiceResult<GanjoorPoetCompleteViewModel>> GetCatById(int id);
/// <summary>
/// get poem by id

View File

@ -78,19 +78,29 @@ namespace RMuseum.Services.Implementation
}
/// <summary>
/// get poet by url
/// get cat by id
/// </summary>
/// <param name="url">base web site address should be removed: hafez or /hafez and not https://ganjoor.net/hafez </param>
/// <param name="id"></param>
/// <returns></returns>
public async Task<RServiceResult<GanjoorPoetCompleteViewModel>> GetPoetByUrl(string url)
public async Task<RServiceResult<GanjoorPoetCompleteViewModel>> GetCatById(int id)
{
try
{
url = url.Replace("/", "");
var cat = await _context.GanjoorCategories.Where(c => c.UrlSlug == url).SingleOrDefaultAsync();
var cat = await _context.GanjoorCategories.Include(c => c.Poet).Include(c => c.Parent).Where(c => c.Id == id).FirstOrDefaultAsync();
if (cat == null)
return new RServiceResult<GanjoorPoetCompleteViewModel>(null);
return await GetPoetById(cat.PoetId);
return new RServiceResult<GanjoorPoetCompleteViewModel>
(
new GanjoorPoetCompleteViewModel()
{
Poet = cat.Poet,
Cat = cat,
Children = await _context.GanjoorCategories.Where(c => c.ParentId == cat.Id).ToListAsync(),
Poems = await _context.GanjoorPoems.Where(p => p.CatId == cat.Id).ToListAsync()
}
);
}
catch (Exception exp)
{
@ -98,6 +108,7 @@ namespace RMuseum.Services.Implementation
}
}
/// <summary>
/// get poem by id
/// </summary>