Example #1
0
        public async Task <IActionResult> AddComment(int userId, int articleId, ArticleComment comment)
        {
            if (comment == null)
            {
                throw new Exception("No comment");
            }

            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }


            if (await _articlesRepo.GetArticle(articleId) == null)
            {
                return(NotFound());
            }


            comment = new ArticleComment
            {
                UserId    = userId,
                ArticleId = articleId,
                Content   = comment.Content
            };

            _boardologyRepo.Add(comment);

            await _articlesRepo.IncreaseArticleComments(articleId);

            if (await _boardologyRepo.SaveAll())
            {
                return(Ok());
            }

            return(BadRequest("Failed to add comment"));
        }