From 849b30c1142cbf9d9b433f3ab81f0874988acc05 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Sun, 14 Feb 2021 18:22:58 +0330 Subject: [PATCH] #75 post comments api --- RMuseum/Controllers/GanjoorController.cs | 42 ++++++++++++- .../ViewModels/GanjoorCommentPostViewModel.cs | 23 +++++++ .../GanjoorCommentSummaryViewModel.cs | 5 ++ RMuseum/RMuseum.xml | 62 ++++++++++++++++++- RMuseum/Services/IGanjoorService.cs | 11 ++++ .../Services/Implementation/GanjoorService.cs | 49 +++++++++++++++ 6 files changed, 190 insertions(+), 2 deletions(-) create mode 100644 RMuseum/Models/Ganjoor/ViewModels/GanjoorCommentPostViewModel.cs diff --git a/RMuseum/Controllers/GanjoorController.cs b/RMuseum/Controllers/GanjoorController.cs index 49eee995..18ea0eef 100644 --- a/RMuseum/Controllers/GanjoorController.cs +++ b/RMuseum/Controllers/GanjoorController.cs @@ -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); } + /// + /// post new comment + /// + /// + /// + [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 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 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); + } + /// /// imports data from ganjoor SQLite database /// @@ -512,15 +545,22 @@ namespace RMuseum.Controllers /// protected IAppUserService _appUserService; + /// + /// for client IP resolution + /// + protected IHttpContextAccessor _httpContextAccessor; + /// /// constructor /// /// /// - public GanjoorController(IGanjoorService ganjoorService, IAppUserService appUserService) + /// + public GanjoorController(IGanjoorService ganjoorService, IAppUserService appUserService, IHttpContextAccessor httpContextAccessor) { _ganjoorService = ganjoorService; _appUserService = appUserService; + _httpContextAccessor = httpContextAccessor; } } } diff --git a/RMuseum/Models/Ganjoor/ViewModels/GanjoorCommentPostViewModel.cs b/RMuseum/Models/Ganjoor/ViewModels/GanjoorCommentPostViewModel.cs new file mode 100644 index 00000000..12fa9e2f --- /dev/null +++ b/RMuseum/Models/Ganjoor/ViewModels/GanjoorCommentPostViewModel.cs @@ -0,0 +1,23 @@ +namespace RMuseum.Models.Ganjoor.ViewModels +{ + /// + /// new comment model + /// + public class GanjoorCommentPostViewModel + { + /// + /// Poem Id + /// + public int PoemId { get; set; } + + /// + /// comment + /// + public string HtmlComment { get; set; } + + /// + /// in reply to + /// + public int? InReplyToId { get; set; } + } +} diff --git a/RMuseum/Models/Ganjoor/ViewModels/GanjoorCommentSummaryViewModel.cs b/RMuseum/Models/Ganjoor/ViewModels/GanjoorCommentSummaryViewModel.cs index 0beb7332..ead505f4 100644 --- a/RMuseum/Models/Ganjoor/ViewModels/GanjoorCommentSummaryViewModel.cs +++ b/RMuseum/Models/Ganjoor/ViewModels/GanjoorCommentSummaryViewModel.cs @@ -43,6 +43,11 @@ namespace RMuseum.Models.Ganjoor.ViewModels /// public int? InReplyToId { get; set; } + /// + /// User Id + /// + public Guid? UserId { get; set; } + /// /// replies /// diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index f64adac7..ad44357b 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -765,6 +765,13 @@ + + + post new comment + + + + imports data from ganjoor SQLite database @@ -787,12 +794,18 @@ IAppUserService instance - + + + for client IP resolution + + + constructor + @@ -4217,6 +4230,26 @@ poem + + + new comment model + + + + + Poem Id + + + + + comment + + + + + in reply to + + GanjoorComment Summary View Model @@ -4257,6 +4290,11 @@ in reply to + + + User Id + + replies @@ -5996,6 +6034,17 @@ + + + new comment + + + + + + + + get recent comments @@ -6807,6 +6856,17 @@ + + + new comment + + + + + + + + get recent comments diff --git a/RMuseum/Services/IGanjoorService.cs b/RMuseum/Services/IGanjoorService.cs index 96e2f5c0..98ceb001 100644 --- a/RMuseum/Services/IGanjoorService.cs +++ b/RMuseum/Services/IGanjoorService.cs @@ -159,6 +159,17 @@ namespace RMuseum.Services /// Task> DirectInsertSong(PoemMusicTrackViewModel song); + /// + /// new comment + /// + /// + /// + /// + /// + /// + /// + Task> NewComment(Guid userId, string ip, int poemId, string content, int? inReplyTo); + /// /// get recent comments /// diff --git a/RMuseum/Services/Implementation/GanjoorService.cs b/RMuseum/Services/Implementation/GanjoorService.cs index 88cdf220..54dd5f83 100644 --- a/RMuseum/Services/Implementation/GanjoorService.cs +++ b/RMuseum/Services/Implementation/GanjoorService.cs @@ -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 } } + /// + /// new comment + /// + /// + /// + /// + /// + /// + /// + public async Task> 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 + ( + 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(null, exp.ToString()); + } + } + + /// /// get recent comments ///