get category by url

This commit is contained in:
Hamid Reza Mohammadi 2020-12-29 18:37:56 +03:30
parent 14ac8201fc
commit e6f6be5d0e
3 changed files with 57 additions and 0 deletions

View File

@ -103,6 +103,29 @@ namespace RMuseum.Controllers
return Ok(res.Result);
}
/// <summary>
/// cat by url
/// </summary>
/// <param name="url"></param>
/// <param name="poems"></param>
/// <returns></returns>
[HttpGet]
[Route("cat")]
[AllowAnonymous]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorPoetCompleteViewModel))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
[ProducesResponseType((int)HttpStatusCode.NotFound)]
public async Task<IActionResult> GetCatByUrl(string url, bool poems = true)
{
RServiceResult<GanjoorPoetCompleteViewModel> res =
await _ganjoorService.GetCatByUrl(url, poems);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
if (res.Result == null)
return NotFound();
return Ok(res.Result);
}
/// <summary>
/// get poem by id
/// </summary>

View File

@ -42,6 +42,14 @@ namespace RMuseum.Services
/// <returns></returns>
Task<RServiceResult<GanjoorPoetCompleteViewModel>> GetCatById(int id, bool poems = true);
/// <summary>
/// get cat by url
/// </summary>
/// <param name="url"></param>
/// <param name="poems"></param>
/// <returns></returns>
Task<RServiceResult<GanjoorPoetCompleteViewModel>> GetCatByUrl(string url, bool poems = true);
/// <summary>
/// Get Poem By Id

View File

@ -113,6 +113,32 @@ namespace RMuseum.Services.Implementation
}
}
/// <summary>
/// get cat by url
/// </summary>
/// <param name="url"></param>
/// <param name="poems"></param>
/// <returns></returns>
public async Task<RServiceResult<GanjoorPoetCompleteViewModel>> GetCatByUrl(string url, bool poems = true)
{
try
{
// /hafez/ => /hafez :
if (url.LastIndexOf('/') == url.Length - 1)
{
url = url.Substring(0, url.Length - 1);
}
var cat = await _context.GanjoorCategories.Where(c => c.FullUrl == url).SingleOrDefaultAsync();
if (cat == null)
return new RServiceResult<GanjoorPoetCompleteViewModel>(null);
return await GetCatById(cat.Id);
}
catch (Exception exp)
{
return new RServiceResult<GanjoorPoetCompleteViewModel>(null, exp.ToString());
}
}
/// <summary>
/// get cat by id
/// </summary>