Exemplo n.º 1
0
        public async Task <ActionResult <Dictionary <string, FeaturedInfo> > > SearchFeaturedAsync(
            [FromBody] FeaturedQuery model)
        {
            var result = await commentService.SearchFeaturedAsync(model.PublicationIds);

            return(Ok(result));
        }
Exemplo n.º 2
0
        public async Task SearchFeatured_Ok()
        {
            // Setup
            const string firstPublicationId  = "firstId";
            const string secondPublicationId = "secondId";

            int commentId = 1;

            var author = new UserInfo(Guid.NewGuid(), "SomeName", null);

            var featuredComments = new Dictionary <string, FeaturedInfo>()
            {
                {
                    firstPublicationId, new FeaturedInfo(
                        new Comment[]
                    {
                        new Comment((commentId++).ToString(), "someContent1", DateTimeOffset.Now, firstPublicationId, null, author),
                        new Comment((commentId++).ToString(), "someContent2", DateTimeOffset.Now, firstPublicationId, null, author),
                    }, 2)
                },
                {
                    secondPublicationId, new FeaturedInfo(
                        new Comment[]
                    {
                        new Comment((commentId++).ToString(), "someContent1", DateTimeOffset.Now, secondPublicationId, null, author),
                        new Comment((commentId++).ToString(), "someContent2", DateTimeOffset.Now, secondPublicationId, null, author),
                    }, 2)
                }
            };

            var model = new FeaturedQuery()
            {
                Keys = new string[] { firstPublicationId, secondPublicationId }
            };

            var serviceMock = new Mock <ICommentsService>();

            serviceMock
            .Setup(s => s.SearchFeaturedAsync(new string[] { firstPublicationId, secondPublicationId }))
            .ReturnsAsync(featuredComments);

            var client = TestServerHelper.New(collection =>
            {
                collection.AddScoped(_ => serviceMock.Object);
            });

            // Act
            var response = await client.PostAsync("Comments/comments/featured", model.AsJsonContent());

            var result = await response.Content.DeserializeContent <Dictionary <string, FeaturedInfo> >();

            // Assert
            Assert.AreEqual(response.StatusCode, HttpStatusCode.OK);

            Assert.IsTrue(result.Count == 2);
            Assert.IsTrue(result[firstPublicationId].Comments.Count() == 2);
            Assert.IsTrue(result[secondPublicationId].Comments.Count() == 2);
        }
Exemplo n.º 3
0
        private async Task <Dictionary <string, CommentsShort> > LoadCommentsAsync(IEnumerable <string> publicationIds)
        {
            var keys  = publicationIds.Select(KeysBuilder.PublicationCommentKey).ToList();
            var query = new FeaturedQuery(keys);

            var featuredComments = await commentsApi.SearchFeaturedAsync(query);

            return(publicationIds
                   .ToDictionary(publicationId => publicationId, publicationId =>
            {
                var comment = featuredComments.GetValueOrDefault(KeysBuilder.PublicationCommentKey(publicationId));

                return new CommentsShort(
                    comment?.Comments.Select(c => ToDomain(c)) ?? Enumerable.Empty <PublicationComment>(),
                    comment?.TotalCount ?? 0);
            }));
        }