예제 #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>());
        }
        public async Task <IActionResult> PutUserComment(int id, UserCommentDto userComment)
        {
            CommonResponse <UserCommentDto> response = new CommonResponse <UserCommentDto>();

            if (id != userComment.Id)
            {
                response.Error = new Error {
                    Status = 400, Message = "There was a mismatch with the provided id and the object."
                };
                return(BadRequest(response));
            }

            // Maps to model
            UserComment commentModel = _mapper.Map <UserComment>(userComment);

            _context.Entry(commentModel).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserCommentExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
예제 #3
0
 public UserCommentViewModel CreateMap(UserCommentDto entity) => new UserCommentViewModel
 {
     MovieID       = entity.MovieID,
     UserComment   = entity.UserComment,
     CommentID     = entity.CommentID,
     CommentRating = entity.CommentRating,
     UserID        = entity.UserID,
     UserName      = entity.UserName
 };
예제 #4
0
        public void Create(UserCommentDto commentDto)
        {
            var userComment = new UserComment
            {
                Comment  = commentDto.UserComment,
                MovieID  = commentDto.MovieID,
                UserID   = commentDto.UserID,
                UserName = commentDto.UserName
            };

            uow.CommentsRepository.Create(userComment);
            uow.Save();
        }
예제 #5
0
 public JsonResult AddComment(AddCommentViewModel model)
 {
     if (ModelState.IsValid)
     {
         var commentDto = new UserCommentDto
         {
             MovieID       = model.MovieID,
             UserComment   = model.UserComment,
             UserID        = HttpContext.User.Identity.GetUserId(),
             UserName      = HttpContext.User.Identity.GetUserName(),
             CommentRating = 0
         };
         commentsService.Create(commentDto);
         return(Json(new { success = true, responseText = "Comment was added." }));
     }
     return(Json(new { success = false, responseText = "Error during comment adding." }));
 }