#77 edit user's own comment api

This commit is contained in:
Hamid Reza Mohammadi 2021-02-20 18:28:23 +03:30
parent 92cbb927fc
commit e57f0681e2
4 changed files with 99 additions and 0 deletions

View File

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

View File

@ -772,6 +772,14 @@
<param name="comment"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.EditMyComment(System.Int32,System.String)">
<summary>
edit user's own comment
</summary>
<param name="id"></param>
<param name="comment"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.DeleteMyComment(System.Int32)">
<summary>
delete user's own comment
@ -6062,6 +6070,15 @@
<param name="inReplyTo"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.EditMyComment(System.Guid,System.Int32,System.String)">
<summary>
update user's own comment
</summary>
<param name="userId"></param>
<param name="commentId"></param>
<param name="htmlComment"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.DeleteMyComment(System.Guid,System.Int32)">
<summary>
delete user own comment
@ -6892,6 +6909,15 @@
<param name="inReplyTo"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.EditMyComment(System.Guid,System.Int32,System.String)">
<summary>
update user's own comment
</summary>
<param name="userId"></param>
<param name="commentId"></param>
<param name="htmlComment"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.DeleteMyComment(System.Guid,System.Int32)">
<summary>
delete user own comment

View File

@ -170,6 +170,15 @@ namespace RMuseum.Services
/// <returns></returns>
Task<RServiceResult<GanjoorCommentSummaryViewModel>> NewComment(Guid userId, string ip, int poemId, string content, int? inReplyTo);
/// <summary>
/// update user's own comment
/// </summary>
/// <param name="userId"></param>
/// <param name="commentId"></param>
/// <param name="htmlComment"></param>
/// <returns></returns>
Task<RServiceResult<bool>> EditMyComment(Guid userId, int commentId, string htmlComment);
/// <summary>
/// delete user own comment
/// </summary>

View File

@ -581,6 +581,36 @@ namespace RMuseum.Services.Implementation
}
}
/// <summary>
/// update user's own comment
/// </summary>
/// <param name="userId"></param>
/// <param name="commentId"></param>
/// <param name="htmlComment"></param>
/// <returns></returns>
public async Task<RServiceResult<bool>> EditMyComment(Guid userId, int commentId, string htmlComment)
{
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
}
comment.HtmlComment = htmlComment;
_context.GanjoorComments.Update(comment);
await _context.SaveChangesAsync();
return new RServiceResult<bool>(true);
}
catch (Exception exp)
{
return new RServiceResult<bool>(false, exp.ToString());
}
}
/// <summary>
/// delete user own comment
/// </summary>