public JsonResult DeleteComment([FromBody] DeleteCommentModel deleteComment)
        {
            if (deleteComment != null)
            {
                var userId = User.Claims.FirstOrDefault(C => C.Type == ClaimTypes.NameIdentifier).Value;

                if (userId == null)
                {
                    return(Json("Authorize"));
                }
                else
                {
                    try
                    {
                        Comment comment = new Comment {
                            Id = deleteComment.CommentId
                        };
                        _context.Comments.Attach(comment);
                        _context.Comments.Remove(comment);
                        _context.SaveChanges();

                        return(Json("Success"));
                    }
                    catch (Exception ex)
                    {
                        return(Json("Failed"));
                    }
                }
            }
            else
            {
                return(Json("Failed"));
            }
        }
        public async Task <IActionResult> DeleteComment([FromBody] DeleteCommentModel model)
        {
            var user = await TokenHelper.GetUser(User, _userManager);

            if (user == null)
            {
                return(RequestHandler.Unauthorized());
            }

            var comment = await _websiteContext.NewsComments.FirstOrDefaultAsync(x => x.Id == model.Id);

            if (comment == null)
            {
                return(RequestHandler.BadRequest($"Comment with id {model.Id} does not exist"));
            }

            // Only validate admin permissions if the comment is not posted by the user trying to delete it
            if (comment.Author != user.Id)
            {
                if (!User.IsInRole("Admin") && !User.IsInRole("Moderator"))
                {
                    return(RequestHandler.Unauthorized());
                }
            }

            _websiteContext.NewsComments.Remove(comment);
            await _websiteContext.SaveChangesAsync();

            // TODO: Only send the new comment back
            var newComments = await _websiteContext.NewsComments.Where(x => x.NewsId == comment.NewsId).OrderBy(o => o.Id).ToListAsync();

            await GetAuthorNames(newComments);

            return(Ok(newComments));
        }
        public void DeleteComment(DeleteCommentModel model, string posterId)
        {
            var comment = _dbContext.Comments.First(x => x.CommentId == model.CommentId);

            comment.IsDeleted = true;

            _dbContext.SaveChanges();
        }
Beispiel #4
0
        public async Task <IActionResult> DeleteComment(DeleteCommentModel model)
        {
            ctx.Comments.Remove(
                ctx.Comments.Single(c => c.Id == model.CommentId)
                );
            await ctx.SaveChangesAsync();

            return(Ok(new DeleteCommentResult()
            {
                Successful = true
            }));
        }
        public ActionResult DeleteComment(int id, [FromServices] DeleteCommentCommand delCommentcmd)
        {
            var model = new DeleteCommentModel();

            model.Id = id;
            var comment = _dataContext.Comments
                          .Include(c => c.MediaItem)
                          .FirstOrDefault(c => c.Id == id);

            delCommentcmd.Execute(model);

            return(RedirectToAction("Details", new { id = comment.MediaItem.Id }));
        }
Beispiel #6
0
        public async Task <bool> Delete(DeleteCommentModel model)
        {
            var commentId = new Hashids(minHashLength: 5).Decode(model.HashId).FirstOrDefault();

            if (commentId == 0)
            {
                return(false);
            }

            var comment = await StoriesDbContext.Comments.FirstOrDefaultAsync(c => c.Id == commentId);

            comment.IsDeleted = true;

            return(await StoriesDbContext.SaveChangesAsync() > 0);
        }
Beispiel #7
0
        public ActionResult ConfirmDelete(IIdentity <Comment> id)
        {
            var comment = commentReader.Value.Read(id);

            if (ReferenceEquals(comment, null))
            {
                return(HttpNotFound());
            }

            var model = new DeleteCommentModel();

            model.Comment = mapper.Map <CommentDto>(comment);

            return(View(model));
        }
Beispiel #8
0
        public async Task <IActionResult> Delete(DeleteCommentModel model)
        {
            if (!Guid.TryParse(CurrentUser.NameIdentifier, out Guid userId))
            {
                return(Json(new { Status = false, Message = "Error deleting comment." }));
            }

            model.UserId = userId;

            var validationResult = DeleteCommentModelValidator.Validate(model);

            if (!validationResult.IsValid)
            {
                return(Json(new { Status = validationResult.IsValid, Messages = validationResult.Messages }));
            }

            var isDeleted = await CommentService.Delete(model);

            return(Json(new { Status = isDeleted }));
        }
 public async Task HandleRequest(HttpContext ctx)
 {
     try
     {
         if (!(await CheckIfUserIsAdmin(ctx)))
         {
             return;
         }
         DeleteCommentModel deleteRequest  = JsonConvert.DeserializeObject <DeleteCommentModel>(await ctx.Request.ReadBodyAsString());
         CommentModel       deletedComment = null;
         using (var dataAccess = _dataAccessFact())
         {
             deletedComment = dataAccess.DeleteComment(deleteRequest.StaticId, deleteRequest.ReasonForDeleting);
         }
         string response = JsonConvert.SerializeObject(deletedComment);
         await ctx.Response.WriteResponse(response, "application/json", 200);
     }
     catch (Exception ex)
     {
         throw new Exception("Failed to mark comment as deleted.", ex);
     }
 }
Beispiel #10
0
        public IHttpActionResult DeleteComment(DeleteCommentModel comment)
        {
            int comentId      = comment.CommentId;
            var currentComent = this.comments.All().Where(x => x.Id == comentId && x.isDeleted == false).FirstOrDefault();

            if (currentComent == null)
            {
                return(this.BadRequest("comment is either deleted or doesn`t exist"));
            }
            try
            {
                currentComent.isDeleted = true;
                this.comments.Update(currentComent);
                this.comments.SaveChanges();
            }
            catch
            {
                return(this.BadRequest("Comment can`t be deleted "));
            }

            return(this.Ok());
        }
Beispiel #11
0
        public async Task <DeleteCommentResult> DeleteComment(DeleteCommentModel model)
        {
            var result = await _httpClient.PostJsonAsync <DeleteCommentResult>("api/comments/deletecomment", model);

            return(result);
        }