public async Task <TossListAdminItems> Handle(TossListAdminQuery request, CancellationToken cancellationToken)
        {
            var count = (await _tossCosmosDB.CreateDocumentQuery <int>("SELECT VALUE COUNT(t) FROM TossEntity t"))
                        .AsEnumerable()
                        .First();
            var query = (await _tossCosmosDB.CreateDocumentQuery()).AsQueryable();

            if (request.MaxDate.HasValue)
            {
                query = query.Where(t => t.CreatedOn < request.MaxDate);
            }
            var items = query
                        .Take(request.ItemCount)
                        .Select(t => new TossListAdminItem()
            {
                Content   = t.Content,
                CreatedOn = t.CreatedOn,
                Id        = t.Id,
                UserName  = t.UserId
            })

                        .ToList();

            return(new TossListAdminItems(items, count));
        }
        public async Task create_setup_username_to_current_user()
        {
            var command = new TossCreateCommand()
            {
                Content = "lorem ipsum lorem ipsum lorem ipsum lorem ipsum"
            };
            var res = await _sut.Handle(command, new System.Threading.CancellationToken());

            var toss = await(await tossTemplate.CreateDocumentQuery()).GetFirstOrDefault();

            Assert.Equal(_m.ApplicationUser.UserName, toss.UserName);
        }
        public async Task <TossLastQueryItem> Handle(SponsoredTossQuery request, CancellationToken cancellationToken)
        {
            var resCollection = (await cosmosDBTemplate.CreateDocumentQuery <SponsoredTossEntity>())
                                .Where(s => s.Type == nameof(SponsoredTossEntity))
                                .Where(s => s.Content.Contains("#" + request.Hashtag))
                                .Where(s => s.DisplayedCount > 0)
                                .Select(t => new TossLastQueryItem()
            {
                Content   = t.Content,
                CreatedOn = t.CreatedOn,
                Id        = t.Id,
                UserName  = t.UserId
            })

                                .AsEnumerable()
                                .ToLookup(t => t.UserName)
                                .ToArray();

            if (!resCollection.Any())
            {
                return(null);
            }
            var index    = random.NewRandom(resCollection.Length - 1);
            var userToss = resCollection[index].ToArray();
            var res      = userToss[random.NewRandom(userToss.Count() - 1)];
            await mediator.Publish(new SponsoredTossDisplayed(res.Id));

            return(res);
        }
        public async Task Handle_removes_toss()
        {
            await _cosmosDBTemplateEntity.Insert(new TossEntity("test content", "user test", DateTimeOffset.Now));

            await _cosmosDBTemplateEntity.Insert(new TossEntity("test content2", "user test", DateTimeOffset.Now));

            var allInsertedToss = (await _cosmosDBTemplateEntity.CreateDocumentQuery()).ToList();

            await _mediator.Send(new DeleteTossCommand(allInsertedToss.First().Id));


            var allRemaining = (await _cosmosDBTemplateEntity.CreateDocumentQuery()).ToList();

            Assert.Single(allRemaining);
            Assert.Null(allRemaining.FirstOrDefault(t => t.Id == allInsertedToss.First().Id));
        }
        public async Task Handle(AccountUserNameUpdated notification, CancellationToken cancellationToken)
        {
            var tosses = (await(await cosmosDBTemplate.CreateDocumentQuery())
                          .Where(t => t.UserId == notification.User.Id)
                          .AsDocumentQuery()
                          .GetAllResultsAsync())
                         .ToList();

            foreach (var item in tosses)
            {
                item.UserName = notification.User.UserName;
                await cosmosDBTemplate.Update(item);
            }
        }
        public async Task Handle(SponsoredTossDisplayed notification, CancellationToken cancellationToken)
        {
            var toss = (await cosmosDBTemplate.CreateDocumentQuery())
                       .Where(t => t.Id == notification.TossId)
                       .Take(1)
                       .AsEnumerable()
                       .FirstOrDefault();

            //we displayed a now removed
            if (toss == null)
            {
                return;
            }
            toss.DecreaseDisplayCount();
            await cosmosDBTemplate.Update(toss);
        }
        public async Task <TossDetail> Handle(TossDetailQuery request, CancellationToken cancellationToken)
        {
            var res = await(await _dbTemplate.CreateDocumentQuery <TossEntity>())
                      .Where(t => t.Id == request.TossId)
                      .Select(t => new TossDetail()
            {
                Content   = t.Content,
                CreatedOn = t.CreatedOn,
                UserName  = t.UserName,
                Id        = t.Id
            })
                      .AsDocumentQuery()
                      .GetAllResultsAsync();

            return(res.FirstOrDefault());
        }
      public async Task create_setup_username_to_current_user()
      {
          await _mediator.Send(
              new TossCreateCommand()
            {
                Content = "lorem ipsum lorem ipsum lorem ipsum lorem ipsum"
            });

          var toss = await(await tossTemplate.CreateDocumentQuery()).GetFirstOrDefault();

          Assert.Equal(TestFixture.UserName, toss.UserName);
      }
        public async Task <IEnumerable <TossLastQueryItem> > Handle(TossLastQuery request, CancellationToken cancellationToken)
        {
            var query = await _dbTemplate.CreateDocumentQuery();

            var results = await query
                          .Where(t => t.Content.Contains("#" + request.HashTag))
                          .OrderByDescending(t => t.CreatedOn)
                          .Take(50)
                          .AsDocumentQuery()
                          .GetAllResultsAsync();

            return(results
                   .Select(t => new TossLastQueryItem()
            {
                Content = t.Content,
                CreatedOn = t.CreatedOn,
                Id = t.Id,
                UserName = t.UserName
            }));
        }