From e6f6be5d0ed54d2a2686e8c0f61a0cf82faec76a Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Tue, 29 Dec 2020 18:37:56 +0330 Subject: [PATCH] get category by url --- RMuseum/Controllers/GanjoorController.cs | 23 ++++++++++++++++ RMuseum/Services/IGanjoorService.cs | 8 ++++++ .../Services/Implementation/GanjoorService.cs | 26 +++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/RMuseum/Controllers/GanjoorController.cs b/RMuseum/Controllers/GanjoorController.cs index 7ac8feee..dad1a8e3 100644 --- a/RMuseum/Controllers/GanjoorController.cs +++ b/RMuseum/Controllers/GanjoorController.cs @@ -103,6 +103,29 @@ namespace RMuseum.Controllers return Ok(res.Result); } + /// + /// cat by url + /// + /// + /// + /// + [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 GetCatByUrl(string url, bool poems = true) + { + RServiceResult 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); + } + /// /// get poem by id /// diff --git a/RMuseum/Services/IGanjoorService.cs b/RMuseum/Services/IGanjoorService.cs index a076cba3..d22de313 100644 --- a/RMuseum/Services/IGanjoorService.cs +++ b/RMuseum/Services/IGanjoorService.cs @@ -42,6 +42,14 @@ namespace RMuseum.Services /// Task> GetCatById(int id, bool poems = true); + /// + /// get cat by url + /// + /// + /// + /// + Task> GetCatByUrl(string url, bool poems = true); + /// /// Get Poem By Id diff --git a/RMuseum/Services/Implementation/GanjoorService.cs b/RMuseum/Services/Implementation/GanjoorService.cs index 135b889b..8299fc07 100644 --- a/RMuseum/Services/Implementation/GanjoorService.cs +++ b/RMuseum/Services/Implementation/GanjoorService.cs @@ -113,6 +113,32 @@ namespace RMuseum.Services.Implementation } } + /// + /// get cat by url + /// + /// + /// + /// + public async Task> 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(null); + return await GetCatById(cat.Id); + } + catch (Exception exp) + { + return new RServiceResult(null, exp.ToString()); + } + } + /// /// get cat by id ///