From acbe582f58e8efb2c508a7fdac3d74a088fd8e76 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Mon, 21 Dec 2020 21:20:14 +0330 Subject: [PATCH] #53 cat subcategories --- RMuseum/Controllers/GanjoorController.cs | 25 +++++++++++++++++++ RMuseum/RMuseum.xml | 20 ++++++++++++--- RMuseum/Services/IGanjoorService.cs | 7 ++++++ .../Services/Implementation/GanjoorService.cs | 23 ++++++++++++----- 4 files changed, 66 insertions(+), 9 deletions(-) diff --git a/RMuseum/Controllers/GanjoorController.cs b/RMuseum/Controllers/GanjoorController.cs index 0eae839c..f0fdf4db 100644 --- a/RMuseum/Controllers/GanjoorController.cs +++ b/RMuseum/Controllers/GanjoorController.cs @@ -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 GetPoetById(int id) { RServiceResult res = await _ganjoorService.GetPoetById(id); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); + if (res.Result == null) + return NotFound(); + return Ok(res.Result); + } + + /// + /// cat by id + /// + /// + /// + [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 GetCatById(int id) + { + RServiceResult res = + await _ganjoorService.GetCatById(id); + if (!string.IsNullOrEmpty(res.ExceptionString)) + return BadRequest(res.ExceptionString); + if (res.Result == null) + return NotFound(); return Ok(res.Result); } diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index eac6d172..81285628 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -629,6 +629,13 @@ + + + cat by id + + + + get poem by id @@ -4488,6 +4495,13 @@ + + + get cat by id + + + + get poem by id @@ -5241,11 +5255,11 @@ - + - get poet by url + get cat by id - base web site address should be removed: hafez or /hafez and not https://ganjoor.net/hafez + diff --git a/RMuseum/Services/IGanjoorService.cs b/RMuseum/Services/IGanjoorService.cs index 3e5f24fe..a05a9285 100644 --- a/RMuseum/Services/IGanjoorService.cs +++ b/RMuseum/Services/IGanjoorService.cs @@ -26,6 +26,13 @@ namespace RMuseum.Services /// Task> GetPoetById(int id); + /// + /// get cat by id + /// + /// + /// + Task> GetCatById(int id); + /// /// get poem by id diff --git a/RMuseum/Services/Implementation/GanjoorService.cs b/RMuseum/Services/Implementation/GanjoorService.cs index 5373d14a..d23290df 100644 --- a/RMuseum/Services/Implementation/GanjoorService.cs +++ b/RMuseum/Services/Implementation/GanjoorService.cs @@ -78,19 +78,29 @@ namespace RMuseum.Services.Implementation } /// - /// get poet by url + /// get cat by id /// - /// base web site address should be removed: hafez or /hafez and not https://ganjoor.net/hafez + /// /// - public async Task> GetPoetByUrl(string url) + public async Task> 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(null); - return await GetPoetById(cat.PoetId); + + return new RServiceResult + ( + 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 } } + /// /// get poem by id ///