public async Task <IActionResult> Comment(string userId, Comment comment)
        {
            if (!ModelState.IsValid)
            {
                return(View("View"));
            }

            AppUser targetUser = await _userManager.FindByIdAsync(userId);

            if (targetUser == null)
            {
                return(BadRequest());
            }

            comment.OwnerId  = userId;
            comment.AuthorId = User.FindId();
            comment.PostedAt = DateTime.Now;

            await _commentRepository.AddCommentAsync(comment);

            return(RedirectToAction("View",
                                    new
            {
                UserId = userId
            }));
        }
        public async Task <ActionResult <CommentDTO> > Post(AddCommentDTO postCommentDto)
        {
            var postComment = await _commentRepository.AddCommentAsync(postCommentDto);

            var result = new CommentDTO(postComment, new List <CommentDTO>());
            await _mediator.Send(new AddCommentCommand(result));

            return(CreatedAtAction(nameof(Get), new { id = postComment.Id }, postComment));
        }
        public async Task AddComment(CommentRequest commentRequest)
        {
            Comment comment = new Comment();

            comment.Description = commentRequest.Description;
            comment.Title       = commentRequest.Title;
            comment.Creation    = DateTime.UtcNow;

            await _commentRepository.AddCommentAsync(comment);
        }
        public async Task <CommentModel> AddCommentAsync(CommentModel model)
        {
            Comment comment = _mapper.Map <CommentModel, Comment>(model);

            comment.Date = $"{ DateTime.Now.ToShortDateString()} {DateTime.Now.ToShortTimeString()}";
            comment.User = await _userRepository.GetUserByIdAsync(comment.UserId);

            await _commentRepository.AddCommentAsync(comment);

            return(_mapper.Map <Comment, CommentModel>(comment));
        }
        public async Task<ActionResult<CommentViewDto>> CreateCommentForPost([FromRoute] Guid postId, [FromBody] CommentInputDto commentInputDto)
        {
            //model state validation is not required due to the [ApiController] attribute automatically returning UnprocessableEntity (see startup.cs)
            //when model binding fails

            if (commentInputDto == null)
            {
                return BadRequest();
            }

            if (!await _postRepository.PostExistsAsync(postId))
            {
                return NotFound();
            }

            //fetch the user id from the JWT via HttpContext. Then get the user from the repository. This is to ensure that an authorized user
            //is calling the API with a valid user id
            var user = await _userRepository.GetUserAsync(User.GetUserId());

            if (user == null)
            {
                return BadRequest(new { Error = "The user was not found in the system. Please try again with an authorized and valid user." });
            }

            var commentToAdd = _mapper.Map<Comment>(commentInputDto); //map CommentInputDto to Comment
            commentToAdd.UserId = user.Id; //set the user id as otherwise navigation property will be null
            await _commentRepository.AddCommentAsync(postId, commentToAdd);

            if (!await _commentRepository.SaveChangesAsync())
            {
                throw new Exception($"Error saving Comment {commentToAdd.Id} to the database");
            }

            var commentToReturn = _mapper.Map<CommentViewDto>(commentToAdd);

            return CreatedAtRoute("GetComment", new { id = commentToAdd.Id }, commentToReturn);
        }
Beispiel #6
0
        /// <summary>
        /// Create comment command handler async.
        /// </summary>
        /// <param name="commentCommand">Create comment command.</param>
        /// <param name="commentsRepository">Comment repository.</param>
        public async Task HandleCreateCommentAsync(CreateCommentCommand commentCommand)
        {
            commentCommand.NewComment = mapper.Map <Comment>(commentCommand);

            await commentRepository.AddCommentAsync(commentCommand.NewComment);
        }