Пример #1
0
        public async Task <IActionResult> GetArticles()
        {
            var request = new GetArticlesQuery();
            var data    = await _mediator.Send(request);

            return(Ok(data));
        }
Пример #2
0
        public async Task <List <Article> > Handle(GetArticlesQuery request, CancellationToken cancellationToken)
        {
            var query = BuildQuery(request);

            return((await _dbContext.WithConnectionAsync(connection =>
                                                         connection.QueryAsync <Article>(query.RawSql, query.Parameters)))
                   .ToList());
        }
        public async Task <IActionResult> Get(
            CancellationToken ct = default)
        {
            var query    = new GetArticlesQuery();
            var articles = await Mediator.Send(query, ct);

            return(Ok(articles));
        }
Пример #4
0
        public async Task GetArticlesQueryHandler_GivenNoArguments_ShouldReturnArticles()
        {
            GetArticlesQuery        query   = new GetArticlesQuery();
            GetArticlesQueryHandler handler = new GetArticlesQueryHandler();

            var request = await handler.Handle(query, new System.Threading.CancellationToken());

            Assert.IsType <List <ArticleDto> >(request);
        }
Пример #5
0
        public Task <Response <IEnumerable <ArticlesDto> > > Handle(GetArticlesQuery request,
                                                                    CancellationToken cancellationToken)
        {
            var articles = _context.Articles
                           .Include(ar => ar.Tags)
                           .ProjectTo <ArticlesDto>(_mapper.ConfigurationProvider)
                           .AsEnumerable();

            return(Response.OkFromTask(articles, string.Empty));
        }
Пример #6
0
 public async Task <IEnumerable <Article> > Handle(
     GetArticlesQuery request,
     CancellationToken cancellationToken)
 {
     return(await _context.Articles
            .AsNoTracking()
            .Include(a => a.HashTags)
            .OrderByCreateTimeDesc()
            .ToListAsync(cancellationToken));
 }
Пример #7
0
        private SqlBuilder.Template BuildQuery(GetArticlesQuery request)
        {
            var builder  = new SqlBuilder();
            var template = builder.AddTemplate(@"SELECT * FROM Articles /**where**/");

            if (request.Ids != null)
            {
                builder.Where("Id in @Ids", new { request.Ids });
            }

            return(template);
        }
Пример #8
0
        public async Task GivenValidRequest_WhenRequestContainsPossibleQueryParams_ReturnsArticlesVieWModel()
        {
            // Arrange
            var getArticlesCommand = new GetArticlesQuery("dragons", "joey.mckenzie", null, null, null);

            // Act
            var command  = new GetArticlesQueryHandler(UserManager, Context, Mapper);
            var response = await command.Handle(getArticlesCommand, CancellationToken.None);

            // Assert
            response.ShouldNotBeNull();
            response.ShouldBeOfType <ArticleViewModelList>();
            response.Articles.ShouldNotBeNull();
            response.Articles.ShouldBeOfType <List <ArticleDto> >();
            response.Articles.ShouldNotBeEmpty();
            response.Articles.FirstOrDefault()?.TagList.ShouldContain("dragons");
        }
 public async Task <IEnumerable <GetArticlesDTO> > Handle(GetArticlesQuery request, CancellationToken token)
 {
     return(await _context.Article
            .OrderByDescending(article => article.Created)
            .Include(article => article.Author)
            .Skip(request.Skip)
            .Take(request.Take)
            .Select(article => new GetArticlesDTO
     {
         Id = article.Id,
         Author = article.Author.UserName,
         Created = article.Created,
         Description = article.Description,
         Title = article.Title,
         ThumbnailId = article.ThumbnailId
     }).ToListAsync(token));
 }
Пример #10
0
        public async Task GivenValidRequest_WhenRequestContainsLimitParam_ReturnsLimitedResultsArticlesInArticlesVieWModel()
        {
            // Arrange
            var getArticlesCommand = new GetArticlesQuery(null, null, null, 1, null);

            // Act
            var command  = new GetArticlesQueryHandler(UserManager, Context, Mapper);
            var response = await command.Handle(getArticlesCommand, CancellationToken.None);

            // Assert
            response.ShouldNotBeNull();
            response.ShouldBeOfType <ArticleViewModelList>();
            response.Articles.ShouldNotBeNull();
            response.Articles.ShouldBeOfType <List <ArticleDto> >();
            response.Articles.ShouldNotBeEmpty();
            response.Articles.Count().ShouldBe(1);
            response.Articles.ShouldContain(a => a.Author.Username == "joey.mckenzie");
            response.Articles.FirstOrDefault(a => a.Author.Username == "joey.mckenzie")?.TagList.ShouldNotBeEmpty();
        }
Пример #11
0
        public async Task GivenValidRequest_WhenRequestContainsPossibleQueryParamsWithNoResultMatches_ReturnsArticlesVieWModelWithNoResults()
        {
            // Arrange
            var getArticlesCommand = new GetArticlesQuery(null, "joey.mckenzie", "thisUserDoesNotExist", null, null);

            // Act
            var command  = new GetArticlesQueryHandler(UserManager, Context, Mapper);
            var response = await command.Handle(getArticlesCommand, CancellationToken.None);

            // Assert
            response.ShouldNotBeNull();
            response.ShouldBeOfType <ArticleViewModelList>();
            response.Articles.ShouldNotBeNull();
            response.Articles.ShouldBeOfType <List <ArticleDto> >();
            response.Articles.ShouldBeEmpty();
            response.Articles.FirstOrDefault()?.Author.Following.ShouldBeTrue();
            response.Articles.FirstOrDefault()?.Favorited.ShouldBeTrue();
            response.Articles.FirstOrDefault()?.TagList.ShouldNotBeEmpty();
        }
Пример #12
0
        public Task <List <ArticleDto> > Handle(GetArticlesQuery request, CancellationToken cancellationToken)
        {
            var result = new List <ArticleDto>();

            result.Add(new ArticleDto
            {
                DateCreated = new System.DateTime(2020, 01, 01, 01, 02, 03, 04),
                Subject     = "Article One",
                Body        = "Body here"
            });

            result.Add(new ArticleDto
            {
                DateCreated = new System.DateTime(2020, 01, 02, 03, 04, 05, 06),
                Subject     = "Article Two",
                Body        = "Body here"
            });

            return(Task.FromResult(result));
        }