public async Task Handle_GivenValidId_ReturnsCommentDTO() { // Arrange var comment = new CommentDTO { Id = 1, AuthorId = 1, Date = new DateTime(2020, 02, 01), PostId = 4, Text = "Comment_One", }; var query = new GetCommentQuery { Id = 1 }; // Act var handler = new GetCommentQuery.GetCommentQueryHandler(Context, Mapper); var result = await handler.Handle(query, CancellationToken.None); // Assert result.ShouldBeOfType <CommentDTO>(); result.ShouldNotBeNull(); result.Id.ShouldBe(comment.Id); result.AuthorId.ShouldBe(comment.AuthorId); result.Date.ShouldBe(comment.Date); result.PostId.ShouldBe(comment.PostId); result.Text.ShouldBe(comment.Text); }
public async Task <GetCommentByIdResponse> Handle(GetCommentByIdRequest request, CancellationToken cancellationToken) { if (request.AuthenticatorRole == AppRole.Employee) { return(new GetCommentByIdResponse() { Error = new ErrorModel(ErrorType.Unauthorized) }); } var query = new GetCommentQuery() { CompanyId = request.AuthenticatorCompanyId, Id = request.CommentId }; var comment = await queryExecutor.Execute(query); if (comment == null) { return(new GetCommentByIdResponse { Error = new ErrorModel(ErrorType.NotFound) }); } var mappedComment = mapper.Map <CommentDto>(comment); return(new GetCommentByIdResponse { Data = mappedComment }); }
public async Task <DeleteCommentByIdResponse> Handle(DeleteCommentByIdRequest request, CancellationToken cancellationToken) { if (request.AuthenticatorRole == AppRole.Employee) { return(new DeleteCommentByIdResponse() { Error = new ErrorModel(ErrorType.Unauthorized) }); } var query = new GetCommentQuery() { Id = request.CommentId }; var comment = await queryExecutor.Execute(query); if (comment == null) { return(new DeleteCommentByIdResponse() { Error = new ErrorModel(ErrorType.NotFound) }); } var command = new DeleteCommentCommand() { Parameter = comment }; var deletedComment = await commandExecutor.Execute(command); return(new DeleteCommentByIdResponse { Data = deletedComment }); }
public async Task <Response <CommentResource> > Handle(GetCommentQuery request, CancellationToken cancellationToken) { var comment = await commentRepo.GetCommentAsync(request.Id); var commentResource = Mapping.Mapper.Map <CommentResource>(comment); return(Response.Ok <CommentResource>(commentResource)); }
private bool IsUserAdminOrCommentSender(GetCommentQuery query) { if (!string.IsNullOrEmpty(query.Id)) { var IsUserAdmin = _Context.User.FindAll(ClaimTypes.Role).Where(c => c.Value == "admin").Any(); var IsSender = commentRepo.FindByExpression(c => c.Id == query.Id && c.senderId == query.Userid).Any(); return(IsUserAdmin || IsSender); } return(false); }
public async Task Handle_GivenInvalidId_ReturnsNull() { // Arrange var query = new GetCommentQuery { Id = 99 }; // Act var handler = new GetCommentQuery.GetCommentQueryHandler(Context, Mapper); var result = await handler.Handle(query, CancellationToken.None); // Assert result.ShouldBeNull(); }
public async Task <IActionResult> Get(long id, [FromCommaSeparatedQuery(Name = "include")] IEnumerable <string> includes) { var flags = EnumFlags.Parse <CommentIncludes>(includes); var query = new GetCommentQuery(User, id) { IncludeAuthor = CommentIncludes.Authors == (flags & CommentIncludes.Authors) }; var result = await mediator.Send(query, HttpContext.RequestAborted); if (false == result.IsSuccess()) { return(NotFound()); } return(Ok(mapper.Map <StoryModel>(result.Data))); }
public async Task <PutCommentByIdResponse> Handle(PutCommentByIdRequest request, CancellationToken cancellationToken) { if (request.AuthenticatorRole == AppRole.Employee) { return(new PutCommentByIdResponse() { Error = new ErrorModel(ErrorType.Unauthorized) }); } var query = new GetAssignmentQuery() { Id = request.AssignmentId }; var query2 = new GetCommentQuery() { Id = request.Id }; var comment = await queryExecutor.Execute(query2); var assignment = await queryExecutor.Execute(query); if (assignment == null || comment == null) { return(new PutCommentByIdResponse() { Error = new ErrorModel(ErrorType.NotFound) }); } var updatecomment = mapper.Map <Comment>(request); var command = new PutCommentCommand { Parameter = updatecomment }; var updatedComment = await commandExecutor.Execute(command); var response = mapper.Map <CommentDto>(updatedComment); return(new PutCommentByIdResponse { Data = response }); }
public async Task <IRequestResult <Comment> > Handle(GetCommentQuery request, CancellationToken cancellationToken) { var authenticated = request.User.Identity.IsAuthenticated; var queryable = context.Comments.AsNoTracking(); if (request.IncludeAuthor) { queryable = queryable.Include(entity => entity.Author); } var comment = await queryable .Where(entity => (authenticated || entity.IsPublic) && entity.Id == request.Id) .FirstOrDefaultAsync(cancellationToken); if (null == comment) { return(new RequestResult <Comment>()); } return(RequestResult.Success(mapper.Map <Comment>(comment))); }
public async Task <IViewComponentResult> InvokeAsync(GetCommentModel commentModel) { // TODO: test with deleted boards var getBoardQuery = new GetBoardQuery(commentModel.BoardId); var board = await getBoardQuery.DefaultIfExceptionAsync(_mediator); var getArticleQuery = new GetArticleQuery(commentModel.ParentArticleId); GetArticleModel parentArticle = await getArticleQuery.DefaultIfExceptionAsync(_mediator); var getCommentQuery = new GetCommentQuery(commentModel.ParentCommentId); GetCommentModel parentComment = await getCommentQuery.DefaultIfExceptionAsync(_mediator); // TODO: will most likely return null when user is deleted var getUserQuery = new GetPublicUserQuery(commentModel.UserId); GetPublicUserModel user = await getUserQuery.DefaultIfExceptionAsync(_mediator); string jwt = _apiJwtManager.GetToken(); bool loggedIn = false; bool saved = false; bool userUpvoted = false; bool userDownvoted = false; bool userCreatedComment = false; try { var loggedInUser = await _mediator.Send(new GetAuthenticatedUserQuery()); if (loggedInUser != null) { loggedIn = true; } saved = loggedInUser.SavedComments.Any(commentId => commentId == commentModel.Id); userUpvoted = loggedInUser.LikedComments.Any(commentId => commentId == commentModel.Id); userDownvoted = loggedInUser.DislikedComments.Any(commentId => commentId == commentModel.Id); userCreatedComment = commentModel.UserId == loggedInUser.Id; } catch (System.Exception) { loggedIn = false; } var model = new CommentCardViewModel { Comment = commentModel, Board = board, ParentArticle = parentArticle, ParentComment = parentComment, User = user, Jwt = jwt, LoggedIn = loggedIn, Saved = saved, UserUpvoted = userUpvoted, UserDownvoted = userDownvoted, UserCreatedComment = userCreatedComment }; return(View(model)); }