Ejemplo n.º 1
0
        public async Task <ActionResult <ArticleCommentDto> > AddComment(int id, ArticleCommentRequest model)
        {
            var article = await _repository.GetById(id);

            if (article == null)
            {
                return(NotFound(ArticleNotFound));
            }

            var result = await _repository.AddComment(id, model);

            return(result != null
                ? Created(nameof(AddComment), result)
                : StatusCode(StatusCodes.Status500InternalServerError, null));
        }
        public async Task <ArticleCommentDto> AddComment(int id, ArticleCommentRequest model)
        {
            var comment = new ArticleComment
            {
                Body      = model.Body,
                Name      = model.Name,
                ArticleId = id
            };

            _db.ArticleComments.Add(comment);
            await _db.SaveChangesAsync();

            return(new ArticleCommentDto
            {
                Body = comment.Body, Id = comment.Id, Name = comment.Body, CreatedAt = comment.CreatedAt
            });
        }