#73 get recent comments api

This commit is contained in:
Hamid Reza Mohammadi 2021-02-08 19:14:27 +03:30
parent db7174747c
commit 7b611067bd
5 changed files with 192 additions and 1 deletions

View File

@ -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);
}
/// <summary>
/// get recent comments
/// </summary>
/// <param name="paging"></param>
/// <returns></returns>
[HttpGet]
[Route("comments")]
[AllowAnonymous]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable<GanjoorCommentFullViewModel>))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public async Task<IActionResult> 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);
}
/// <summary>
/// imports data from ganjoor SQLite database
/// </summary>

View File

@ -0,0 +1,43 @@
using System;
namespace RMuseum.Models.Ganjoor.ViewModels
{
public class GanjoorCommentFullViewModel
{
/// <summary>
/// Id
/// </summary>
public int Id { get; set; }
/// <summary>
/// author name (MySql imported field)
/// </summary>
public string AuthorName { get; set; }
/// <summary>
/// author url (MySql imported field)
/// </summary>
public string AuthorUrl { get; set; }
/// <summary>
/// comment date
/// </summary>
public DateTime CommentDate { get; set; }
/// <summary>
/// comment
/// </summary>
public string HtmlComment { get; set; }
/// <summary>
/// in reply to
/// </summary>
public GanjoorCommentSummaryViewModel InReplayTo { get; set; }
/// <summary>
/// poem
/// </summary>
public GanjoorPoemSummaryViewModel Poem { get; set; }
}
}

View File

@ -751,6 +751,13 @@
</summary>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.Get(RSecurityBackend.Models.Generic.PagingParameterModel)">
<summary>
get recent comments
</summary>
<param name="paging"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.ImportLocalSQLiteDb">
<summary>
imports data from ganjoor SQLite database
@ -4178,6 +4185,41 @@
poems
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.ViewModels.GanjoorCommentFullViewModel.Id">
<summary>
Id
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.ViewModels.GanjoorCommentFullViewModel.AuthorName">
<summary>
author name (MySql imported field)
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.ViewModels.GanjoorCommentFullViewModel.AuthorUrl">
<summary>
author url (MySql imported field)
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.ViewModels.GanjoorCommentFullViewModel.CommentDate">
<summary>
comment date
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.ViewModels.GanjoorCommentFullViewModel.HtmlComment">
<summary>
comment
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.ViewModels.GanjoorCommentFullViewModel.InReplayTo">
<summary>
in reply to
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.ViewModels.GanjoorCommentFullViewModel.Poem">
<summary>
poem
</summary>
</member>
<member name="T:RMuseum.Models.Ganjoor.ViewModels.GanjoorCommentSummaryViewModel">
<summary>
GanjoorComment Summary View Model
@ -5949,6 +5991,13 @@
<param name="song"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.GetRecentComments(RSecurityBackend.Models.Generic.PagingParameterModel)">
<summary>
get recent comments
</summary>
<param name="paging"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.Faal">
<summary>
get a random poem from hafez
@ -6745,6 +6794,13 @@
<param name="userId"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.GetRecentComments(RSecurityBackend.Models.Generic.PagingParameterModel)">
<summary>
get recent comments
</summary>
<param name="paging"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.GetPoemImages(System.Int32)">
<summary>
get poem images by id (some fields are intentionally field with blank or null),

View File

@ -152,6 +152,13 @@ namespace RMuseum.Services
/// <returns></returns>
Task<RServiceResult<PoemMusicTrackViewModel>> DirectInsertSong(PoemMusicTrackViewModel song);
/// <summary>
/// get recent comments
/// </summary>
/// <param name="paging"></param>
/// <returns></returns>
Task<RServiceResult<(PaginationMetadata PagingMeta, GanjoorCommentFullViewModel[] Items)>> GetRecentComments(PagingParameterModel paging);
/// <summary>
/// get a random poem from hafez

View File

@ -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);
}
}
/// <summary>
/// get recent comments
/// </summary>
/// <param name="paging"></param>
/// <returns></returns>
public async Task<RServiceResult<(PaginationMetadata PagingMeta, GanjoorCommentFullViewModel[] Items)>> 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<GanjoorCommentFullViewModel>.Paginate(source, paging);
foreach (GanjoorCommentFullViewModel comment in paginatedResult.Items)
{
comment.HtmlComment = _Linkify(comment.HtmlComment);
comment.HtmlComment = $"<p>{comment.HtmlComment.Replace("\r\n", "\n").Replace("\n\n", "\n").Replace("\n", "<br />".Replace("ي", "ی"))}</p>";
}
return new RServiceResult<(PaginationMetadata PagingMeta, GanjoorCommentFullViewModel[] Items)>(paginatedResult);
}
catch (Exception exp)
{
return new RServiceResult<(PaginationMetadata PagingMeta, GanjoorCommentFullViewModel[] Items)>((PagingMeta: null, Items: null), exp.ToString());
}
}
/// <summary>
/// get poem images by id (some fields are intentionally field with blank or null),