public async Task <IActionResult> Edit(int id) { // Get post. var postQuery = new GetPostQuery { Id = id }; var postDTO = await _mediator.Send(postQuery); if (postDTO == null) { return(NotFound(id)); } // Get topic. var topicId = postDTO.TopicId; var topicQuery = new GetTopicQuery { Id = topicId }; var topicDTO = await _mediator.Send(topicQuery); var model = _mapper.Map <PostDTO, EditPostViewModel>(postDTO); model.Topic = topicDTO.Text; // Get topics list. var topicsDTO = await _mediator.Send(new GetTopicsQuery()); var topics = _mapper.Map <IEnumerable <TopicDTO>, ICollection <TopicViewModel> >(topicsDTO); model.Topics = topics; return(View(model)); }
public override async Task <Post> GetPost(GetPostQuery request, ServerCallContext context) { return(await dbContext.Posts .Include(pa => pa.PostAuthor) .Include(pe => pe.PostExtended) .SingleOrDefaultAsync(post => post.PostId == request.Id)); }
public async Task Handle_GivenValidId_ReturnsPostDTO() { // Arrange var post = new PostDTO { Id = 1, AuthorId = 1, Date = new DateTime(2020, 01, 01), Title = "Title_One", Text = "Test_Two", TopicId = 1, }; var query = new GetPostQuery { Id = 1 }; // Act var handler = new GetPostQuery.GetPostQueryHandler(Context, Mapper); var result = await handler.Handle(query, CancellationToken.None); // Assert result.ShouldBeOfType <PostDTO>(); result.ShouldNotBeNull(); result.Id.ShouldBe(post.Id); result.AuthorId.ShouldBe(post.AuthorId); result.Date.ShouldBe(post.Date); result.Title.ShouldBe(post.Title); result.Text.ShouldBe(post.Text); result.TopicId.ShouldBe(post.TopicId); }
public Post GetPost(int id) { return(GetPostQuery <Post> .Execute(new { Id = id })); }
public async Task <IActionResult> Get([FromQuery] int id) { var getPostQuery = new GetPostQuery { Id = id }; var post = await Mediator.Send(getPostQuery); return(Ok(post)); }
public async Task <IActionResult> Get(string id) { var query = new GetPostQuery(_context); var post = await query.Execute(id); return(post == null ? new NotFoundResult() as ActionResult : new ObjectResult(post)); }
public async Task ShouldThrowOnCommandWithDefaults() { await SeedTestData(); var command = new GetPostQuery(); FluentActions.Invoking(() => SendAsync(command)) .Should().Throw <NotFoundException>(); }
public Post GetPost(int id) { var post = GetPostQuery <Post> .Execute(new { Id = id }); Connector connector = new Connector(credentialManager.PublicKey, credentialManager.PrivateKey); post.Hash = connector.Call("getPostHash", id); return(profile); }
public async Task <IActionResult> Post([FromServices] GetPostQuery getPostQuery, string categoryCode, string postUrl) { Data.Post post = await getPostQuery.WithUnpublish().WithoutMarkDown().ExecuteAsync(categoryCode, postUrl); if (post == null) { return(new NotFoundResult()); } return(Json(post)); }
public async Task <ActionResult <Object> > GetPostDetails([FromBody] GetPostQuery query) { try { return(await _mediator.Send(query)); } catch (Exception ex) { throw; } }
public async Task ShouldThrowOnNotExistinUrl() { await SeedTestData(); var command = new GetPostQuery() { Id = 10 }; FluentActions.Invoking(() => SendAsync(command)) .Should().Throw <NotFoundException>(); }
public async Task ShouldReturnAnyData() { await SeedTestData(); var command = new GetPostQuery() { Id = 1 }; var result = await SendAsync(command); result.Should().NotBeNull(); }
public async Task ShouldNotThrow() { await SeedTestData(); var command = new GetPostQuery() { Id = 1 }; FluentActions.Invoking(() => SendAsync(command)) .Should().NotThrow(); }
public async Task Handle_GivenInvalidId_ReturnsNull() { // Arrange var query = new GetPostQuery { Id = 99 }; // Act var handler = new GetPostQuery.GetPostQueryHandler(Context, Mapper); var result = await handler.Handle(query, CancellationToken.None); // Assert result.ShouldBeNull(); }
public async Task <IActionResult> OnGetAsync([FromRoute] string slug) { var query = new GetPostQuery(_db); Post = await query.ExecuteAsync(slug); if (Post == null) { return(NotFound()); } Body = MarkdownRenderer.RenderMarkdown(Post.Body); return(Page()); }
public async Task <Result <PostDto> > Handle(GetPostQuery request, CancellationToken cancellationToken) { var post = await _forumDbContext.Posts .IncludeAuthor() .IncludeLikes() .IncludeTags() .IncludePreview() .AsNoTracking() .SingleOrDefaultAsync(p => p.Id == request.Id); if (post == null) { return(Result <PostDto> .Failure(nameof(request.Id), "Post doesnt exist")); } var result = _mapper.Map <PostDto>(post); return(Result <PostDto> .Success(result)); }
public async Task <IActionResult> GetPostAsync(int id) { var postQuery = new GetPostQuery { Id = id }; var post = await _mediator.Send(postQuery); if (post == null) { return(NoContent()); } var author = await _mediator.Send(new GetAuthorQuery { Id = post.AuthorId }); if (author != null) { post.Author = author.FirstName + " " + author.LastName; } var topic = await _mediator.Send(new GetTopicQuery { Id = post.TopicId }); if (topic != null) { post.Topic = topic.Text; } var postModel = new PostModel { Id = post.Id, Date = post.Date, Title = post.Title, Text = post.Text, AuthorId = post.AuthorId, Author = post.Author, TopicId = post.TopicId, Topic = post.Topic }; _logger.LogInformation(@"Post with Id={id} was successfully sent."); return(Json(postModel, GetOptions())); }
public async Task ShouldCreateReplyPostWithAttachments() { await SeedTestData(); var command = new CreatePostCommand() { TopicId = 1, ParentPostId = 1, Text = "Test Text", Signature = null, Attachments = new FormFileCollection() { new FakeFormFile("TestFiles\\sasha.jpeg", "image/jpeg") } }; var newPostId = await SendAsync(command); var getPostComand = new GetPostQuery() { Id = newPostId }; var newPost = await SendAsync(getPostComand); newPost.Id.Should().Be(newPostId); newPost.TopicId.Should().Be(1); newPost.ParentId.Should().Be(1); newPost.Text.Should().Be("Test Text"); newPost.Attachments .Should().HaveCount(1); var attachment = newPost.Attachments.First(); attachment.OriginalFilename.Should().Be("sasha.jpeg"); attachment.ContentType.Should().Be("image/jpeg"); }
public async Task <IActionResult> GetPosts([FromQuery] GetPostQuery query) => Ok(await _mediator.Send(query));
public Task <GetPostResult> GetPost( [FromRoute] GetPostQuery query) => Mediator.Send(query);
public async Task <IActionResult> Read(int id) { if (id == default) { return(NotFound()); } // Get post. var postQuery = new GetPostQuery { Id = id }; var post = await _mediator.Send(postQuery); // Add author information. var authorQuery = new GetAuthorQuery { Id = post.AuthorId }; var author = await _mediator.Send(authorQuery); post.Author = author.FirstName + " " + author.LastName; // Add topic information. var topicQuery = new GetTopicQuery { Id = post.TopicId }; var topic = await _mediator.Send(topicQuery); post.Topic = topic.Text; // Get comments for current post. var commentsQuery = new GetCommentsByPostIdQuery { PostId = post.Id }; var comments = await _mediator.Send(commentsQuery); // Create post view model. var model = _mapper.Map <PostDTO, PostViewModel>(post); model.AuthorAvatar = author.Avatar; // Check current user if he/she is an author of the current post. var userName = HttpContext.User.Identity.Name; if (userName == null) { model.CurrentReaderId = default; } else { var userId = await _identityService.GetUserIdByNameAsync(userName); var reader = await _mediator.Send(new GetAuthorByUserIdQuery { UserId = userId }); if (reader == null) { model.CurrentReaderId = default; } else { model.CurrentReaderId = reader.Id; } } // Check post comments model.Comments = _mapper.Map <ICollection <CommentDTO>, ICollection <CommentViewModel> >(comments); foreach (var comment in model.Comments) { var commentAuthorQuery = new GetAuthorQuery { Id = comment.AuthorId }; var commentAuthor = await _mediator.Send(commentAuthorQuery); comment.Author = commentAuthor.FirstName + " " + commentAuthor.LastName; (var age, var units) = comment.Date.Age(); comment.Age = age; comment.AgeUnits = units; comment.AuthorAvatar = commentAuthor.Avatar; } return(View(model)); }