public async Task <IActionResult> AddComment([FromBody] CommentForCreateDto commentForCreate) { if (commentForCreate == null) { return(BadRequest("input was not valid")); } var comment = new Comment { NewsId = commentForCreate.NewsId, AuthorId = commentForCreate.AuthorId, Content = commentForCreate.Content }; var x = _dbContext.Add <Comment>(comment); await _dbContext.SaveChangesAsync(); var commentsFromRepo = await _dbContext.Comments.Include(a => a.Author) .Where(n => n.NewsId == comment.NewsId).ToListAsync(); var commentFromRepo = commentsFromRepo.TakeLast(1).FirstOrDefault(); var commentToReturn = CommentDto.Map(commentFromRepo); return(Ok(commentToReturn)); }
public async Task <IActionResult> Update(string id, CommentForCreateDto commentChanges) { Comment commentToUpdate = _mapper.Map <Comment>(commentChanges); Comment updatedComment = await _commentsService.Update(id, commentToUpdate); return(Ok()); }
public async Task <IActionResult> AddComment(int threadId, int userId, CommentForCreateDto commentForCreateDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _repo.GetUser(userId); var threadFromRepo = await _repo.GetThread(threadId); commentForCreateDto.userId = userFromRepo.Id; commentForCreateDto.threadId = threadFromRepo.Id; var comment = _mapper.Map <Comment>(commentForCreateDto); threadFromRepo.Comments.Add(comment); userFromRepo.Comments.Add(comment); if (await _repo.Save()) { var finalComment = _mapper.Map <CommentsForThreadDto>(comment); return(CreatedAtRoute("GetComment", new { userId = userId, threadId = threadId, id = comment.Id }, finalComment)); } return(BadRequest("Could not add new comment")); }
public async Task <IActionResult> Create(string postId, CommentForCreateDto commentForCreateDto) { Comment commentToCreate = _mapper.Map <Comment>(commentForCreateDto); commentToCreate.PostId = postId; commentToCreate.UserId = User.FindFirst(ClaimTypes.NameIdentifier).Value; commentToCreate.Username = User.FindFirst(ClaimTypes.Name).Value; await _commentsService.Create(commentToCreate); return(Ok()); }