public async Task <ActionResult <PostReactionDto> > AddPostReaction(string userId, string postId, [FromBody] PostReactionToAddDto reactionToAdd) { if (Guid.TryParse(postId, out Guid gPostId) && Guid.TryParse(userId, out Guid gUserId)) { try { if (_postService.CheckIfPostExists(gPostId) && await _userService.CheckIfUserExists(gUserId)) { PostReactionDto addedReaction = await _postReactionService.AddPostReactionAsync(gPostId, reactionToAdd); return(CreatedAtRoute("GetReaction", new { userId, postId = addedReaction.PostId, reactionId = addedReaction.Id }, addedReaction)); } else { return(NotFound($"User: {userId} or post {postId} not found.")); } } catch (Exception ex) { _logger.LogError(ex, "Error occured during adding the reaction. Post id: {postId}", postId); return(StatusCode(StatusCodes.Status500InternalServerError)); } } else { return(BadRequest($"{userId} or {postId} is not valid guid.")); } }
public ActionResult <List <PostReactionDto> > GetPostReaction(string postId, string reactionId) { if (Guid.TryParse(postId, out Guid gPostId) && Guid.TryParse(reactionId, out Guid gReactionId)) { try { if (_postService.CheckIfPostExists(gPostId)) { PostReactionDto postReaction = _postReactionService.GetPostReaction(gReactionId); return(Ok(postReaction)); } else { return(NotFound($"Post: {postId} not found.")); } } catch (Exception ex) { _logger.LogError(ex, "Error occured during getting the reaction: postId: {postId}, reactionId: {reactionId}", postId, reactionId); return(StatusCode(StatusCodes.Status500InternalServerError)); } } else { return(BadRequest($"{postId} or {reactionId} is not valid guid.")); } }
public async Task <Result <ReactionDto> > SetPostReaction(PostReactionDto dto, string userId) { var result = await _setPostReactionCommandHandler .HandleAsync(new SetPostReactionCommand(dto.PostId, userId, dto.Liked)); return(result); }
public async void AddPostReaction_ReturnsCreatedAtRouteResult_WithReactionData() { //Arrange _mockPostService.Setup(Service => Service.CheckIfPostExists(It.IsAny <Guid>())) .Returns(true) .Verifiable(); _mockUserService.Setup(Service => Service.CheckIfUserExists(It.IsAny <Guid>())) .ReturnsAsync(true) .Verifiable(); PostReactionDto reactionEntity = _mapper.Map <PostReactionDto>(_postReactionToAdd); reactionEntity.Id = new Guid(ConstIds.ExampleReactionId); reactionEntity.PostId = new Guid(ConstIds.ExamplePostId); _mockReactionService.Setup(Service => Service.AddPostReactionAsync(It.IsAny <Guid>(), It.IsAny <PostReactionToAddDto>())) .ReturnsAsync(reactionEntity) .Verifiable(); var controller = new PostReactionsController(_loggerMock.Object, _mockPostService.Object, _mockUserService.Object, _mockReactionService.Object); //Act var result = await controller.AddPostReaction(ConstIds.ExampleUserId, ConstIds.ExamplePostId, _postReactionToAdd); //Assert var redirectToActionResult = Assert.IsType <CreatedAtRouteResult>(result.Result); Assert.Equal(ConstIds.ExampleUserId, redirectToActionResult.RouteValues["userId"].ToString()); Assert.Equal(ConstIds.ExamplePostId, redirectToActionResult.RouteValues["postId"].ToString()); Assert.Equal(ConstIds.ExampleReactionId, redirectToActionResult.RouteValues["reactionId"].ToString()); Assert.Equal("GetReaction", redirectToActionResult.RouteName); _mockUserService.Verify(); _mockReactionService.Verify(); _mockPostService.Verify(); }
public async Task <IActionResult> SetPostReaction([FromBody] PostReactionDto dto) { //todo: move to custom attribute if (!HttpContext.User.Identity.IsAuthenticated) { return(Forbid()); } Result <ReactionDto> result = await _postService.SetPostReaction(dto, UserId); return(FromResult(result)); }
public async Task DeletePostReactionAsync(Guid userId, Guid postId) { PostReactionDto reactionToRemove = GetPostReaction(userId, postId); await DeletePostReactionAsync(reactionToRemove.Id); }
public async Task <PostDetailsDto> GetPostDetailsDto(int postId, int userId) { // bool userReaction; var photos = await _context.Photos.Where(x => x.PostId == postId).ToListAsync(); List <PostPhotoDto> postPhotoDto = _mapper.Map <List <Photo>, List <PostPhotoDto> >(photos); var positiveReactions = await _context.Reactions.Where(x => x.PostId == postId && x.IsPositive == true).CountAsync(); var negativeReactions = await _context.Reactions.Where(x => x.PostId == postId && x.IsPositive == false).CountAsync(); PostReactionDto postReactionDto = new PostReactionDto { PositveReactions = positiveReactions, NegativeReactions = negativeReactions }; var comments = await _context.Comments.Where(x => x.PostId == postId).Include(x => x.User).Select(x => new PostCommentDto { CommentId = x.Id, UserId = x.UserId, UserLogin = x.User.Login, Content = x.Content, CreatedAt = x.CreatedAt }).ToListAsync(); var reaction = await _context.Reactions.Where(x => x.PostId == postId && x.UserId == userId).FirstOrDefaultAsync(); // var reaction = await _context.Posts.Where(x => x.Id == postId && x.UserId == userId).FirstOrDefaultAsync(); if (reaction != null) { bool userReaction = reaction.IsPositive; BoolDto boolDto = new BoolDto(userReaction); var postDetailsDto = await _context.Posts.Where(x => x.Id == postId).Select(x => new PostDetailsDto { PostId = postId, UserId = x.UserId, AuthorLogin = x.User.Login, Description = x.Description, CreateAt = x.CreateAt, UserReaction = boolDto, Photos = postPhotoDto, Reactions = postReactionDto, Comments = comments }).FirstOrDefaultAsync(); return(postDetailsDto); } else { var postDetailsDto = await _context.Posts.Where(x => x.Id == postId).Select(x => new PostDetailsDto { PostId = postId, UserId = x.UserId, AuthorLogin = x.User.Login, Description = x.Description, CreateAt = x.CreateAt, Photos = postPhotoDto, Reactions = postReactionDto, Comments = comments }).FirstOrDefaultAsync(); return(postDetailsDto); } }