#75 post comments api

This commit is contained in:
Hamid Reza Mohammadi 2021-02-14 18:22:58 +03:30
parent c5a76542e4
commit 849b30c114
6 changed files with 190 additions and 2 deletions

View File

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using RMuseum.Models.Auth.Memory;
@ -465,6 +466,38 @@ namespace RMuseum.Controllers
return Ok(comments.Result.Items);
}
/// <summary>
/// post new comment
/// </summary>
/// <param name="comment"></param>
/// <returns></returns>
[HttpPost]
[Route("comment")]
[Authorize]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorCommentSummaryViewModel))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
[ProducesResponseType((int)HttpStatusCode.Forbidden)]
public async Task<IActionResult> NewComment([FromBody] GanjoorCommentPostViewModel comment)
{
string clientIPAddress = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
Guid userId =
new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value);
Guid sessionId =
new Guid(User.Claims.FirstOrDefault(c => c.Type == "SessionId").Value);
RServiceResult<bool> sessionCheckResult = await _appUserService.SessionExists(userId, sessionId);
if (!string.IsNullOrEmpty(sessionCheckResult.ExceptionString))
{
return StatusCode((int)HttpStatusCode.Forbidden);
}
var res =
await _ganjoorService.NewComment(userId, clientIPAddress, comment.PoemId, comment.HtmlComment, comment.InReplyToId);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
return Ok(res.Result);
}
/// <summary>
/// imports data from ganjoor SQLite database
/// </summary>
@ -512,15 +545,22 @@ namespace RMuseum.Controllers
/// </summary>
protected IAppUserService _appUserService;
/// <summary>
/// for client IP resolution
/// </summary>
protected IHttpContextAccessor _httpContextAccessor;
/// <summary>
/// constructor
/// </summary>
/// <param name="ganjoorService"></param>
/// <param name="appUserService"></param>
public GanjoorController(IGanjoorService ganjoorService, IAppUserService appUserService)
/// <param name="httpContextAccessor"></param>
public GanjoorController(IGanjoorService ganjoorService, IAppUserService appUserService, IHttpContextAccessor httpContextAccessor)
{
_ganjoorService = ganjoorService;
_appUserService = appUserService;
_httpContextAccessor = httpContextAccessor;
}
}
}

View File

@ -0,0 +1,23 @@
namespace RMuseum.Models.Ganjoor.ViewModels
{
/// <summary>
/// new comment model
/// </summary>
public class GanjoorCommentPostViewModel
{
/// <summary>
/// Poem Id
/// </summary>
public int PoemId { get; set; }
/// <summary>
/// comment
/// </summary>
public string HtmlComment { get; set; }
/// <summary>
/// in reply to
/// </summary>
public int? InReplyToId { get; set; }
}
}

View File

@ -43,6 +43,11 @@ namespace RMuseum.Models.Ganjoor.ViewModels
/// </summary>
public int? InReplyToId { get; set; }
/// <summary>
/// User Id
/// </summary>
public Guid? UserId { get; set; }
/// <summary>
/// replies
/// </summary>

View File

@ -765,6 +765,13 @@
<param name="paging"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.NewComment(RMuseum.Models.Ganjoor.ViewModels.GanjoorCommentPostViewModel)">
<summary>
post new comment
</summary>
<param name="comment"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.ImportLocalSQLiteDb">
<summary>
imports data from ganjoor SQLite database
@ -787,12 +794,18 @@
IAppUserService instance
</summary>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.#ctor(RMuseum.Services.IGanjoorService,RSecurityBackend.Services.IAppUserService)">
<member name="F:RMuseum.Controllers.GanjoorController._httpContextAccessor">
<summary>
for client IP resolution
</summary>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.#ctor(RMuseum.Services.IGanjoorService,RSecurityBackend.Services.IAppUserService,Microsoft.AspNetCore.Http.IHttpContextAccessor)">
<summary>
constructor
</summary>
<param name="ganjoorService"></param>
<param name="appUserService"></param>
<param name="httpContextAccessor"></param>
</member>
<member name="M:RMuseum.Controllers.ImageController.Get(System.Guid,System.String)">
<summary>
@ -4217,6 +4230,26 @@
poem
</summary>
</member>
<member name="T:RMuseum.Models.Ganjoor.ViewModels.GanjoorCommentPostViewModel">
<summary>
new comment model
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.ViewModels.GanjoorCommentPostViewModel.PoemId">
<summary>
Poem Id
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.ViewModels.GanjoorCommentPostViewModel.HtmlComment">
<summary>
comment
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.ViewModels.GanjoorCommentPostViewModel.InReplyToId">
<summary>
in reply to
</summary>
</member>
<member name="T:RMuseum.Models.Ganjoor.ViewModels.GanjoorCommentSummaryViewModel">
<summary>
GanjoorComment Summary View Model
@ -4257,6 +4290,11 @@
in reply to
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.ViewModels.GanjoorCommentSummaryViewModel.UserId">
<summary>
User Id
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.ViewModels.GanjoorCommentSummaryViewModel.Replies">
<summary>
replies
@ -5996,6 +6034,17 @@
<param name="song"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.NewComment(System.Guid,System.String,System.Int32,System.String,System.Nullable{System.Int32})">
<summary>
new comment
</summary>
<param name="userId"></param>
<param name="ip"></param>
<param name="poemId"></param>
<param name="content"></param>
<param name="inReplyTo"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.GetRecentComments(RSecurityBackend.Models.Generic.PagingParameterModel)">
<summary>
get recent comments
@ -6807,6 +6856,17 @@
<param name="userId"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.NewComment(System.Guid,System.String,System.Int32,System.String,System.Nullable{System.Int32})">
<summary>
new comment
</summary>
<param name="userId"></param>
<param name="ip"></param>
<param name="poemId"></param>
<param name="content"></param>
<param name="inReplyTo"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.GetRecentComments(RSecurityBackend.Models.Generic.PagingParameterModel)">
<summary>
get recent comments

View File

@ -159,6 +159,17 @@ namespace RMuseum.Services
/// <returns></returns>
Task<RServiceResult<PoemMusicTrackViewModel>> DirectInsertSong(PoemMusicTrackViewModel song);
/// <summary>
/// new comment
/// </summary>
/// <param name="userId"></param>
/// <param name="ip"></param>
/// <param name="poemId"></param>
/// <param name="content"></param>
/// <param name="inReplyTo"></param>
/// <returns></returns>
Task<RServiceResult<GanjoorCommentSummaryViewModel>> NewComment(Guid userId, string ip, int poemId, string content, int? inReplyTo);
/// <summary>
/// get recent comments
/// </summary>

View File

@ -487,6 +487,7 @@ namespace RMuseum.Services.Implementation
HtmlComment = comment.HtmlComment,
PublishStatus = comment.Status == PublishStatus.Awaiting ? "در انتظار تأیید" : "",
InReplyToId = comment.InReplyToId,
UserId = comment.UserId
};
GanjoorCommentSummaryViewModel[] allComments = await source.ToArrayAsync();
@ -523,6 +524,54 @@ namespace RMuseum.Services.Implementation
}
}
/// <summary>
/// new comment
/// </summary>
/// <param name="userId"></param>
/// <param name="ip"></param>
/// <param name="poemId"></param>
/// <param name="content"></param>
/// <param name="inReplyTo"></param>
/// <returns></returns>
public async Task<RServiceResult<GanjoorCommentSummaryViewModel>> NewComment(Guid userId, string ip, int poemId, string content, int? inReplyTo)
{
try
{
GanjoorComment comment = new GanjoorComment()
{
UserId = userId,
AuthorIpAddress = ip,
CommentDate = DateTime.Now,
HtmlComment = content,
InReplyToId = inReplyTo,
PoemId = poemId,
Status = PublishStatus.Published,
};
_context.GanjoorComments.Add(comment);
await _context.SaveChangesAsync();
return new RServiceResult<GanjoorCommentSummaryViewModel>
(
new GanjoorCommentSummaryViewModel()
{
Id = comment.Id,
AuthorName = $"{comment.User.FirstName} {comment.User.SureName}".Trim(),
AuthorUrl = comment.AuthorUrl,
CommentDate = comment.CommentDate,
HtmlComment = comment.HtmlComment,
PublishStatus = comment.Status == PublishStatus.Awaiting ? "در انتظار تأیید" : "",
InReplyToId = comment.InReplyToId,
UserId = comment.UserId
}
);
}
catch (Exception exp)
{
return new RServiceResult<GanjoorCommentSummaryViewModel>(null, exp.ToString());
}
}
/// <summary>
/// get recent comments
/// </summary>