public void OnActionExecuting(ActionExecutingContext context)
            {
                NewArticleHolder articleHolder = context.ActionArguments["newArticle"] as NewArticleHolder;

                if (null == articleHolder)
                {
                    InvalidateRequest(context, "No article holder is present", _logger, 422);
                    return;
                }
                NewArticle article = articleHolder.Article;

                if (null == article)
                {
                    InvalidateRequest(context, "There is no article in the holder", _logger, 422);
                    return;
                }
                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);
                }
            }
Exemplo n.º 2
0
        public async Task <IActionResult> CreateArticle([FromBody] NewArticleHolder newArticle)
        {
            try
            {
                string  username = HttpContext.User.Claims.FirstOrDefault(c => c.Type == JwtRegisteredClaimNames.Sub).Value;
                Article article  = await _articleService.CreateArticle(username, newArticle.Article);

                this.HttpContext.Response.StatusCode = 201;
                return(Json(new SingleArticleHolder(article)));
            } catch (Exception ex)
            {
                return(HandleException(ex, _logger));
            }
        }