From f71a31effeb98ccfb9eccd9be7903875b9c028be Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Fri, 5 Feb 2021 14:09:40 +0330 Subject: [PATCH] #65 api for displaying poem comments --- .../GanjoorCommentSummaryViewModel.cs | 1 + .../GanjoorPoemCompleteViewModel.cs | 5 ++ RMuseum/RMuseum.xml | 13 ++++ .../Services/Implementation/GanjoorService.cs | 78 ++++++++++++------- 4 files changed, 67 insertions(+), 30 deletions(-) diff --git a/RMuseum/Models/Ganjoor/ViewModels/GanjoorCommentSummaryViewModel.cs b/RMuseum/Models/Ganjoor/ViewModels/GanjoorCommentSummaryViewModel.cs index 9ba2d14d..54fce823 100644 --- a/RMuseum/Models/Ganjoor/ViewModels/GanjoorCommentSummaryViewModel.cs +++ b/RMuseum/Models/Ganjoor/ViewModels/GanjoorCommentSummaryViewModel.cs @@ -44,4 +44,5 @@ namespace RMuseum.Models.Ganjoor.ViewModels public GanjoorCommentSummaryViewModel[] Replies { get; set; } } + } diff --git a/RMuseum/Models/Ganjoor/ViewModels/GanjoorPoemCompleteViewModel.cs b/RMuseum/Models/Ganjoor/ViewModels/GanjoorPoemCompleteViewModel.cs index 2f3acd57..baf8cfd2 100644 --- a/RMuseum/Models/Ganjoor/ViewModels/GanjoorPoemCompleteViewModel.cs +++ b/RMuseum/Models/Ganjoor/ViewModels/GanjoorPoemCompleteViewModel.cs @@ -96,5 +96,10 @@ namespace RMuseum.Models.Ganjoor.ViewModels /// Songs /// public PoemMusicTrackViewModel[] Songs { get; set; } + + /// + /// Comments + /// + public GanjoorCommentSummaryViewModel[] Comments { get; set; } } } diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index 23367c05..e802e112 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -4368,6 +4368,11 @@ Songs + + + Comments + + poem summary @@ -6727,6 +6732,14 @@ + + + get poem comments + + + + + get poem images by id (some fields are intentionally field with blank or null), diff --git a/RMuseum/Services/Implementation/GanjoorService.cs b/RMuseum/Services/Implementation/GanjoorService.cs index ab6a64e9..9354ae2d 100644 --- a/RMuseum/Services/Implementation/GanjoorService.cs +++ b/RMuseum/Services/Implementation/GanjoorService.cs @@ -416,23 +416,18 @@ namespace RMuseum.Services.Implementation return new RServiceResult(null, exp.ToString()); } } - /* - public async Task> GetPoemComments(int id, Guid userId) + + /// + /// get poem comments + /// + /// + /// + /// + public async Task> GetPoemComments(int poemId, Guid userId) { try { - var source = - from comment in _context.GanjoorComments - where - (comment.Status == PublishStatus.Published || (userId != Guid.Empty && comment.Status == PublishStatus.Awaiting && comment.UserId == userId)) - && - comment.Id == id - orderby comment.CommentDate - select new GanjoorCommentSummaryViewModel() - { - Id = comment.Id, - }; - return new RServiceResult(await source.ToArrayAsync()); + return new RServiceResult(await _GetPoemComments(poemId, userId, null)); } catch (Exception exp) { @@ -440,24 +435,35 @@ namespace RMuseum.Services.Implementation } } - private async Task _GetPoemComments(int id, Guid userId, int? inReplyToId) + private async Task _GetPoemComments(int poemId, Guid userId, int? inReplyToId) { var source = - from comment in _context.GanjoorComments - where - (comment.Status == PublishStatus.Published || (userId != Guid.Empty && comment.Status == PublishStatus.Awaiting && comment.UserId == userId)) - && - comment.Id == id - && - comment.InReplyToId == inReplyToId - orderby comment.CommentDate - select new GanjoorCommentSummaryViewModel() - { - Id = comment.Id, - }; - return await source.ToArrayAsync(); + from comment in _context.GanjoorComments.Include(c => c.User) + where + (comment.Status == PublishStatus.Published || (userId != Guid.Empty && comment.Status == PublishStatus.Awaiting && comment.UserId == userId)) + && + comment.PoemId == poemId + && + comment.InReplyToId == inReplyToId + orderby comment.CommentDate + select new GanjoorCommentSummaryViewModel() + { + 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, + PublishStatus = comment.Status == PublishStatus.Awaiting ? "در انتظار تأیید" : "" + }; + var comments = await source.ToArrayAsync(); + + foreach(var comment in comments) + { + comment.Replies = await _GetPoemComments(poemId, userId, comment.Id); + } + return comments; } - */ + /// /// get poem images by id (some fields are intentionally field with blank or null), @@ -712,6 +718,17 @@ namespace RMuseum.Services.Implementation tracks = songsRes.Result; } + GanjoorCommentSummaryViewModel[] poemComments = null; + + if(comments) + { + var commentsRes = await GetPoemComments(id, Guid.Empty); + if (!string.IsNullOrEmpty(commentsRes.ExceptionString)) + return new RServiceResult(null, commentsRes.ExceptionString); + poemComments = commentsRes.Result; + } + + return new RServiceResult ( @@ -734,7 +751,8 @@ namespace RMuseum.Services.Implementation Recitations = rc, Images = imgs, Verses = verses, - Songs = tracks + Songs = tracks, + Comments = poemComments } ); }