#76 delete user's own comment api

This commit is contained in:
Hamid Reza Mohammadi 2021-02-18 19:03:06 +03:30
parent 9c5d5213c9
commit c0c30ad75b
5 changed files with 145 additions and 9 deletions

View File

@ -1220,7 +1220,7 @@ namespace RMuseum.Controllers
RServiceResult<bool> res = await _artifactService.DeleteUserBookmark(bookmarkId);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
return Ok();;
return Ok();
}
/// <summary>

View File

@ -498,6 +498,39 @@ namespace RMuseum.Controllers
return Ok(res.Result);
}
/// <summary>
/// delete user's own comment
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpDelete]
[Route("comment")]
[Authorize]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
[ProducesResponseType((int)HttpStatusCode.NotFound)]
[ProducesResponseType((int)HttpStatusCode.Forbidden)]
public async Task<IActionResult> DeleteMyComment(int id)
{
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.DeleteMyComment(userId, id);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
if (!res.Result)
return NotFound();
return Ok();
}
/// <summary>
/// imports data from ganjoor SQLite database
/// </summary>

View File

@ -772,6 +772,13 @@
<param name="comment"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.DeleteMyComment(System.Int32)">
<summary>
delete user's own comment
</summary>
<param name="id"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.ImportLocalSQLiteDb">
<summary>
imports data from ganjoor SQLite database
@ -6045,6 +6052,14 @@
<param name="inReplyTo"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.DeleteMyComment(System.Guid,System.Int32)">
<summary>
delete user own comment
</summary>
<param name="userId"></param>
<param name="commentId"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.GetRecentComments(RSecurityBackend.Models.Generic.PagingParameterModel)">
<summary>
get recent comments
@ -6867,6 +6882,14 @@
<param name="inReplyTo"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.DeleteMyComment(System.Guid,System.Int32)">
<summary>
delete user own comment
</summary>
<param name="userId"></param>
<param name="commentId"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.GetRecentComments(RSecurityBackend.Models.Generic.PagingParameterModel)">
<summary>
get recent comments
@ -6986,7 +7009,12 @@
IAppUserService instance
</summary>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.#ctor(RMuseum.DbContext.RMuseumDbContext,Microsoft.Extensions.Configuration.IConfiguration,RSecurityBackend.Services.IBackgroundTaskQueue,RSecurityBackend.Services.IAppUserService)">
<member name="F:RMuseum.Services.Implementation.GanjoorService._notificationService">
<summary>
Messaging service
</summary>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.#ctor(RMuseum.DbContext.RMuseumDbContext,Microsoft.Extensions.Configuration.IConfiguration,RSecurityBackend.Services.IBackgroundTaskQueue,RSecurityBackend.Services.IAppUserService,RSecurityBackend.Services.IRNotificationService)">
<summary>
constructor
</summary>
@ -6994,6 +7022,7 @@
<param name="configuration"></param>
<param name="backgroundTaskQueue"></param>
<param name="appUserService"></param>
<param name="notificationService"></param>
</member>
<member name="T:RMuseum.Services.Implementation.ImportedFromDesktopGanjoor.GPersianTextSync">
<summary>

View File

@ -170,6 +170,14 @@ namespace RMuseum.Services
/// <returns></returns>
Task<RServiceResult<GanjoorCommentSummaryViewModel>> NewComment(Guid userId, string ip, int poemId, string content, int? inReplyTo);
/// <summary>
/// delete user own comment
/// </summary>
/// <param name="userId"></param>
/// <param name="commentId"></param>
/// <returns></returns>
Task<RServiceResult<bool>> DeleteMyComment(Guid userId, int commentId);
/// <summary>
/// get recent comments
/// </summary>

View File

@ -495,9 +495,7 @@ namespace RMuseum.Services.Implementation
foreach(GanjoorCommentSummaryViewModel comment in allComments)
{
comment.AuthorName = comment.AuthorName.ToPersianNumbers().ApplyCorrectYeKe();
comment.HtmlComment = comment.HtmlComment.ToPersianNumbers().ApplyCorrectYeKe();
comment.HtmlComment = _Linkify(comment.HtmlComment);
comment.HtmlComment = $"<p>{comment.HtmlComment.Replace("\r\n", "\n").Replace("\n\n", "\n").Replace("\n", "<br />")}</p>";
comment.HtmlComment = _PrepareCommentHtml(comment.HtmlComment);
}
GanjoorCommentSummaryViewModel[] rootComments = allComments.Where(c => c.InReplyToId == null).ToArray();
@ -583,6 +581,59 @@ namespace RMuseum.Services.Implementation
}
}
/// <summary>
/// delete user own comment
/// </summary>
/// <param name="userId"></param>
/// <param name="commentId"></param>
/// <returns></returns>
public async Task<RServiceResult<bool>> DeleteMyComment(Guid userId, int commentId)
{
try
{
GanjoorComment comment = await _context.GanjoorComments.Where(c => c.Id == commentId && c.UserId == userId).SingleOrDefaultAsync();//userId is not part of key but it helps making call secure
if(comment == null)
{
return new RServiceResult<bool>(false); //not found
}
//if user has got replies, delete them and notify their owners of what happened
var replies = await _FindReplies(comment);
for(int i = replies.Count - 1; i >= 0; i--)
{
if(replies[i].UserId != null)
{
await _notificationService.PushNotification((Guid)replies[i].UserId,
"حذف پاسخ شما به حاشیه",
$"پاسخ شما به یکی از حاشیه‌های گنجور به دلیل حذف زنجیرهٔ حاشیه توسط یکی از حاشیه‌گذاران حذف شده است.{Environment.NewLine}" +
$"این متن حاشیهٔ حذف شدهٔ شماست: {Environment.NewLine}" +
$"{_PrepareCommentHtml(replies[i].HtmlComment)}"
);
}
_context.GanjoorComments.Remove(replies[i]);
}
_context.GanjoorComments.Remove(comment);
await _context.SaveChangesAsync();
return new RServiceResult<bool>(true);
}
catch (Exception exp)
{
return new RServiceResult<bool>(false, exp.ToString());
}
}
private async Task<List<GanjoorComment>> _FindReplies(GanjoorComment comment)
{
List<GanjoorComment> replies = await _context.GanjoorComments.Where(c => c.InReplyToId == comment.Id).ToListAsync();
foreach (GanjoorComment reply in replies)
{
replies.AddRange(await _FindReplies(reply));
}
return replies;
}
/// <summary>
/// get recent comments
@ -631,9 +682,7 @@ namespace RMuseum.Services.Implementation
foreach (GanjoorCommentFullViewModel comment in paginatedResult.Items)
{
comment.AuthorName = comment.AuthorName.ToPersianNumbers().ApplyCorrectYeKe();
comment.HtmlComment = comment.HtmlComment.ToPersianNumbers().ApplyCorrectYeKe();
comment.HtmlComment = _Linkify(comment.HtmlComment);
comment.HtmlComment = $"<p>{comment.HtmlComment.Replace("\r\n", "\n").Replace("\n\n", "\n").Replace("\n", "<br />")}</p>";
comment.HtmlComment = _PrepareCommentHtml(comment.HtmlComment);
}
return new RServiceResult<(PaginationMetadata PagingMeta, GanjoorCommentFullViewModel[] Items)>(paginatedResult);
@ -644,6 +693,15 @@ namespace RMuseum.Services.Implementation
}
}
private string _PrepareCommentHtml(string comment)
{
comment = comment.ToPersianNumbers().ApplyCorrectYeKe();
comment = _Linkify(comment);
comment = $"<p>{comment.Replace("\r\n", "\n").Replace("\n\n", "\n").Replace("\n", "<br />")}</p>";
return comment;
}
/// <summary>
/// get poem images by id (some fields are intentionally field with blank or null),
@ -1322,6 +1380,12 @@ namespace RMuseum.Services.Implementation
protected IAppUserService _appUserService;
/// <summary>
/// Messaging service
/// </summary>
protected readonly IRNotificationService _notificationService;
/// <summary>
/// constructor
@ -1330,11 +1394,13 @@ namespace RMuseum.Services.Implementation
/// <param name="configuration"></param>
/// <param name="backgroundTaskQueue"></param>
/// <param name="appUserService"></param>
public GanjoorService(RMuseumDbContext context, IConfiguration configuration, IBackgroundTaskQueue backgroundTaskQueue, IAppUserService appUserService)
/// <param name="notificationService"></param>
public GanjoorService(RMuseumDbContext context, IConfiguration configuration, IBackgroundTaskQueue backgroundTaskQueue, IAppUserService appUserService, IRNotificationService notificationService)
{
_context = context;
_backgroundTaskQueue = backgroundTaskQueue;
_appUserService = appUserService;
_notificationService = notificationService;
Configuration = configuration;
}
}