From daab3b6735a2a4a2cf959b2d3c37f01690bf103d Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Sat, 3 Feb 2024 15:22:24 +0330 Subject: [PATCH] /api/ganjoor/comments/id API for retrieving a single comment information --- RMuseum/Controllers/GanjoorController.cs | 25 ++++++++++ RMuseum/RMuseum.xml | 21 ++++++++ RMuseum/Services/IGanjoorService.cs | 7 +++ .../Services/Implementation/GanjoorService.cs | 49 ++++++++++++++++++- 4 files changed, 100 insertions(+), 2 deletions(-) diff --git a/RMuseum/Controllers/GanjoorController.cs b/RMuseum/Controllers/GanjoorController.cs index 4429d7b3..6ae92c77 100644 --- a/RMuseum/Controllers/GanjoorController.cs +++ b/RMuseum/Controllers/GanjoorController.cs @@ -1848,6 +1848,31 @@ namespace RMuseum.Controllers return Ok(res.Result); } + /// + /// get a single comment information (replies are not included) + /// + /// + /// + [HttpGet] + [Route("comments/{id}")] + [AllowAnonymous] + [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorCommentFullViewModel))] + [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] + [ProducesResponseType((int)HttpStatusCode.NotFound)] + public async Task GetCommentByIdAsync(int id) + { + var res = await _ganjoorService.GetCommentByIdAsync(id); + if (!string.IsNullOrEmpty(res.ExceptionString)) + { + return BadRequest(res.ExceptionString); + } + if(res.Result == null) + { + return NotFound(); + } + return Ok(res.Result); + } + /// /// get recent comments /// diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index 9d781cae..82d86c33 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -1458,6 +1458,13 @@ + + + get a single comment information (replies are not included) + + + + get recent comments @@ -14888,6 +14895,13 @@ + + + get a single comment information (replies are not included) + + + + get a section related sections @@ -18462,6 +18476,13 @@ + + + get a single comment information (replies are not included) + + + + new comment diff --git a/RMuseum/Services/IGanjoorService.cs b/RMuseum/Services/IGanjoorService.cs index 852fe945..37d3b388 100644 --- a/RMuseum/Services/IGanjoorService.cs +++ b/RMuseum/Services/IGanjoorService.cs @@ -211,6 +211,13 @@ namespace RMuseum.Services /// Task> GetPoemComments(int poemId, Guid userId, int? coupletIndex); + /// + /// get a single comment information (replies are not included) + /// + /// + /// + Task> GetCommentByIdAsync(int commentId); + /// /// get a section related sections /// diff --git a/RMuseum/Services/Implementation/GanjoorService.cs b/RMuseum/Services/Implementation/GanjoorService.cs index 525bb238..f1f291b8 100644 --- a/RMuseum/Services/Implementation/GanjoorService.cs +++ b/RMuseum/Services/Implementation/GanjoorService.cs @@ -854,8 +854,6 @@ namespace RMuseum.Services.Implementation } - - /// /// get poem comments /// @@ -919,6 +917,53 @@ namespace RMuseum.Services.Implementation } } + /// + /// get a single comment information (replies are not included) + /// + /// + /// + public async Task> GetCommentByIdAsync(int commentId) + { + var source = + from c in _context.GanjoorComments.Include(c => c.User) + where + c.Status == PublishStatus.Published + && + c.Id == commentId + select new GanjoorCommentSummaryViewModel() + { + Id = c.Id, + AuthorName = c.User == null ? c.AuthorName : $"{c.User.NickName}", + AuthorUrl = c.AuthorUrl, + CommentDate = c.CommentDate, + HtmlComment = c.HtmlComment, + PublishStatus = c.Status == PublishStatus.Awaiting ? "در انتظار تأیید" : "", + InReplyToId = c.InReplyToId, + UserId = c.UserId, + CoupletIndex = c.CoupletIndex == null ? -1 : (int)c.CoupletIndex, + }; + + GanjoorCommentSummaryViewModel comment = await source.AsNoTracking().SingleOrDefaultAsync(); + if (comment == null) + { + return new RServiceResult(null); + } + + comment.AuthorName = comment.AuthorName.ToPersianNumbers().ApplyCorrectYeKe(); + + var dbComment = await _context.GanjoorComments.AsNoTracking().Where(c => c.Id == commentId).SingleAsync(); + + var relatedVerses = comment.CoupletIndex == -1 ? new List() : await _context.GanjoorVerses.Where(v => v.PoemId == dbComment.PoemId && v.CoupletIndex == comment.CoupletIndex).OrderBy(v => v.VOrder).ToListAsync(); + string coupleText = relatedVerses.Count == 0 ? "" : relatedVerses[0].Text; + for (int nVerseIndex = 1; nVerseIndex < relatedVerses.Count; nVerseIndex++) + { + coupleText += $" {relatedVerses[nVerseIndex].Text}"; + } + comment.CoupletSummary = _CutSummary(coupleText); + + return new RServiceResult(comment); + } + /// /// new comment ///