예제 #1
0
        public IActionResult AddComment(Comment comment, bool isSuccess = false)
        {
            try
            {
                var userId = _userManager.GetUserId(this.HttpContext.User);
                comment.UserId = userId;

                if (ModelState.IsValid)
                {
                    _comment.AddNewComment(comment);
                    _comment.Save();
                    //comment.Description = " ";
                    ViewBag.IsSuccess = isSuccess;
                    TempData["Alert"] = true;

                    //Adding Json here
                    return(Json(new { isValid = true, html = Helper.RenderRazorViewToString(this, "_AddComment", _db.Comment.ToListAsync()) }));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
            }
            // JSON Return
            return(Json(new { isValid = false, html = Helper.RenderRazorViewToString(this, "_AddComment", comment) }));
        }
예제 #2
0
        // [ValidateAntiForgeryToken]
        public async Task <IActionResult> NewComment([FromBody] NewCommentViewModel newCommentViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var comment = new Comment()
            {
                ServiceId       = newCommentViewModel.ServiceId,
                NodePubKey      = newCommentViewModel.NodePubKey,
                DateCreated     = DateTime.Now,
                DateModified    = DateTime.Now,
                UserId          = GetUserId(),
                Username        = User.Identity.Name,
                Body            = newCommentViewModel.CommentBody,
                ParentCommentId = newCommentViewModel.ParentCommentId
            };

            var authorizationResult = await authorizationService.AuthorizeAsync(User, comment, "CanCrudOwnComment");

            if (authorizationResult.Succeeded)
            {
                _repository.AddNewComment(comment);
                _unitOfWork.Complete();
                return(Ok(CreateCommentViewModel(comment)));
            }
            else if (User.Identity.IsAuthenticated)
            {
                return(new ForbidResult());
            }
            else
            {
                return(new ChallengeResult());
            }
        }