public async Task <IActionResult> CreateComment(string slug, [FromBody] NewCommentHolder newCommentHolder) { try { string username = HttpContext.User.Claims.FirstOrDefault(c => c.Type == JwtRegisteredClaimNames.Sub).Value; Comment comment = await _commentService.CreateComment(username, newCommentHolder.Comment.Body, slug); this.HttpContext.Response.StatusCode = 201; return(Json(new SingleCommentHolder(comment))); } catch (Exception ex) { return(HandleException(ex, _logger)); } }
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { string slug = context.ActionArguments["slug"] as string; Article article = await _articleService.GetArticle(slug, null); if (null == article || null == article.Slug) { InvalidateRequest(context, $"No article with the specified slug {slug}", _logger, 404); await next(); } NewCommentHolder newCommentHolder = context.ActionArguments["newCommentHolder"] as NewCommentHolder; if (null == newCommentHolder) { InvalidateRequest(context, "No comment holder is present", _logger, 404); await next(); } NewComment newComment = newCommentHolder.Comment; if (null == newComment) { InvalidateRequest(context, "No comment in the holder", _logger, 404); await next(); } if (!context.ModelState.IsValid) { List <string> errors = context .ModelState .Values .SelectMany(e => e.Errors) .Select(e => e.ErrorMessage) .Distinct() .Cast <string>() .ToList(); InvalidateRequest(context, errors, _logger, 422); await next(); } await next(); }