From 7b611067bdd412eda5a0c4781ae335fd8f92ce7c Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Mon, 8 Feb 2021 19:14:27 +0330 Subject: [PATCH] #73 get recent comments api --- RMuseum/Controllers/GanjoorController.cs | 26 ++++++++ .../ViewModels/GanjoorCommentFullViewModel.cs | 43 +++++++++++++ RMuseum/RMuseum.xml | 56 +++++++++++++++++ RMuseum/Services/IGanjoorService.cs | 7 +++ .../Services/Implementation/GanjoorService.cs | 61 ++++++++++++++++++- 5 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 RMuseum/Models/Ganjoor/ViewModels/GanjoorCommentFullViewModel.cs diff --git a/RMuseum/Controllers/GanjoorController.cs b/RMuseum/Controllers/GanjoorController.cs index ae3b40e2..e47acd43 100644 --- a/RMuseum/Controllers/GanjoorController.cs +++ b/RMuseum/Controllers/GanjoorController.cs @@ -10,6 +10,7 @@ using RMuseum.Services; using RSecurityBackend.Models.Generic; using RSecurityBackend.Services; using System; +using System.Collections.Generic; using System.Linq; using System.Net; using System.Threading.Tasks; @@ -417,6 +418,31 @@ namespace RMuseum.Controllers return Ok(res.Result); } + /// + /// get recent comments + /// + /// + /// + [HttpGet] + [Route("comments")] + [AllowAnonymous] + [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable))] + [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] + + public async Task Get([FromQuery] PagingParameterModel paging) + { + var comments = await _ganjoorService.GetRecentComments(paging); + if (!string.IsNullOrEmpty(comments.ExceptionString)) + { + return BadRequest(comments.ExceptionString); + } + + // Paging Header + HttpContext.Response.Headers.Add("paging-headers", JsonConvert.SerializeObject(comments.Result.PagingMeta)); + + return Ok(comments.Result.Items); + } + /// /// imports data from ganjoor SQLite database /// diff --git a/RMuseum/Models/Ganjoor/ViewModels/GanjoorCommentFullViewModel.cs b/RMuseum/Models/Ganjoor/ViewModels/GanjoorCommentFullViewModel.cs new file mode 100644 index 00000000..1f7391b8 --- /dev/null +++ b/RMuseum/Models/Ganjoor/ViewModels/GanjoorCommentFullViewModel.cs @@ -0,0 +1,43 @@ +using System; + +namespace RMuseum.Models.Ganjoor.ViewModels +{ + public class GanjoorCommentFullViewModel + { + /// + /// Id + /// + public int Id { get; set; } + + /// + /// author name (MySql imported field) + /// + public string AuthorName { get; set; } + + /// + /// author url (MySql imported field) + /// + public string AuthorUrl { get; set; } + + /// + /// comment date + /// + public DateTime CommentDate { get; set; } + + /// + /// comment + /// + public string HtmlComment { get; set; } + + /// + /// in reply to + /// + + public GanjoorCommentSummaryViewModel InReplayTo { get; set; } + + /// + /// poem + /// + public GanjoorPoemSummaryViewModel Poem { get; set; } + } +} diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index 204ad5ca..d57b8094 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -751,6 +751,13 @@ + + + get recent comments + + + + imports data from ganjoor SQLite database @@ -4178,6 +4185,41 @@ poems + + + Id + + + + + author name (MySql imported field) + + + + + author url (MySql imported field) + + + + + comment date + + + + + comment + + + + + in reply to + + + + + poem + + GanjoorComment Summary View Model @@ -5949,6 +5991,13 @@ + + + get recent comments + + + + get a random poem from hafez @@ -6745,6 +6794,13 @@ + + + get recent comments + + + + get poem images by id (some fields are intentionally field with blank or null), diff --git a/RMuseum/Services/IGanjoorService.cs b/RMuseum/Services/IGanjoorService.cs index 913b112a..2780c116 100644 --- a/RMuseum/Services/IGanjoorService.cs +++ b/RMuseum/Services/IGanjoorService.cs @@ -152,6 +152,13 @@ namespace RMuseum.Services /// Task> DirectInsertSong(PoemMusicTrackViewModel song); + /// + /// get recent comments + /// + /// + /// + Task> GetRecentComments(PagingParameterModel paging); + /// /// get a random poem from hafez diff --git a/RMuseum/Services/Implementation/GanjoorService.cs b/RMuseum/Services/Implementation/GanjoorService.cs index e5178bec..ca8a61e8 100644 --- a/RMuseum/Services/Implementation/GanjoorService.cs +++ b/RMuseum/Services/Implementation/GanjoorService.cs @@ -17,6 +17,7 @@ using System.Threading.Tasks; using RMuseum.Services.Implementation.ImportedFromDesktopGanjoor; using RMuseum.Models.Artifact; using System.Text.RegularExpressions; +using RSecurityBackend.Services.Implementation; namespace RMuseum.Services.Implementation { @@ -499,7 +500,65 @@ namespace RMuseum.Services.Implementation _FindReplies(reply, allComments); } } - + + /// + /// get recent comments + /// + /// + /// + public async Task> GetRecentComments(PagingParameterModel paging) + { + try + { + var source = + from comment in _context.GanjoorComments.Include(c => c.Poem).Include(c => c.User).Include(c => c.InReplyTo).ThenInclude(r => r.User) + where + comment.Status == PublishStatus.Published + orderby comment.CommentDate descending + select new GanjoorCommentFullViewModel() + { + Id = comment.Id, + AuthorName = comment.User == null ? comment.AuthorName : $"{comment.User.FirstName} {comment.User.SureName}".Trim(), + AuthorUrl = comment.AuthorUrl, + CommentDate = comment.CommentDate, + HtmlComment = comment.HtmlComment, + InReplayTo = comment.InReplyTo == null ? null : + new GanjoorCommentSummaryViewModel() + { + Id = comment.InReplyTo.Id, + AuthorName = comment.InReplyTo.User == null ? comment.InReplyTo.AuthorName : $"{comment.InReplyTo.User.FirstName} {comment.InReplyTo.User.SureName}".Trim(), + AuthorUrl = comment.InReplyTo.AuthorUrl, + CommentDate = comment.InReplyTo.CommentDate, + HtmlComment = comment.InReplyTo.HtmlComment, + PublishStatus = "" + }, + Poem = new GanjoorPoemSummaryViewModel() + { + Id = comment.Poem.Id, + Title = comment.Poem.FullTitle, + UrlSlug = comment.Poem.FullUrl, + Excerpt = "" + } + }; + + (PaginationMetadata PagingMeta, GanjoorCommentFullViewModel[] Items) paginatedResult = + await QueryablePaginator.Paginate(source, paging); + + + foreach (GanjoorCommentFullViewModel comment in paginatedResult.Items) + { + comment.HtmlComment = _Linkify(comment.HtmlComment); + comment.HtmlComment = $"

{comment.HtmlComment.Replace("\r\n", "\n").Replace("\n\n", "\n").Replace("\n", "
".Replace("ي", "ی"))}

"; + } + + return new RServiceResult<(PaginationMetadata PagingMeta, GanjoorCommentFullViewModel[] Items)>(paginatedResult); + } + catch (Exception exp) + { + return new RServiceResult<(PaginationMetadata PagingMeta, GanjoorCommentFullViewModel[] Items)>((PagingMeta: null, Items: null), exp.ToString()); + } + } + /// /// get poem images by id (some fields are intentionally field with blank or null),