예제 #1
0
        /// <summary>
        /// 用户写评论
        /// 2019/5/11
        /// </summary>
        /// <returns></returns>
        public async Task <OutputUserCommentDto> CreateComment([FromBody] UserCommentDto userCommentDto)
        {
            string articleId = userCommentDto.ArticleId;

            //查询评论区
            var commentArea = await _commentRepository
                              .GetCommentAreaByIdAsync(articleId);

            //如果评论区不存在,直接返回false
            if (commentArea == null)
            {
                _logger.LogError("评论区不存在,用户写评论失败");
                return(null);
            }

            //如果评论区存在,写入评论(1级评论)
            //AutoMapper映射
            UserComment userComment = userCommentDto.MapTo <UserComment>();

            //如果留言成功,会返回留言内容
            UserComment successCommentContent = await _commentRepository.AddCommentToAreaAsync(userComment, articleId);

            //向客户端返回留言内容
            return(successCommentContent.MapTo <OutputUserCommentDto>());
        }