Ejemplo n.º 1
0
        public async Task <Unit> Handle(TossCreateCommand command, CancellationToken cancellationToken)
        {
            TossEntity toss;

            if (!command.SponsoredDisplayedCount.HasValue)
            {
                toss = new TossEntity(
                    command.Content,
                    _httpContextAccessor.HttpContext.User.Identity.Name,
                    DateTimeOffset.Now);
            }
            else
            {
                toss = new SponsoredTossEntity(
                    command.Content,
                    _httpContextAccessor.HttpContext.User.Identity.Name,
                    DateTimeOffset.Now,
                    command.SponsoredDisplayedCount.Value);
            }
            toss.Id = await _dbTemplate.Insert(toss);

            if (command.SponsoredDisplayedCount.HasValue)
            {
                ApplicationUser applicationUser = (await userManager.GetUserAsync(_httpContextAccessor.HttpContext.User));
                var             paymentResult   = await stripeClient.Charge(command.StripeChargeToken, command.SponsoredDisplayedCount.Value *TossCreateCommand.CtsCostPerDisplay, "Payment for sponsored Toss #" + toss.Id, applicationUser.Email);

                if (!paymentResult)
                {
                    await _dbTemplate.Delete(toss.Id);
                }
            }
            return(Unit.Value);
        }
Ejemplo n.º 2
0
        public async Task <Unit> Handle(TossCreateCommand command, CancellationToken cancellationToken)
        {
            var toss = new TossEntity(
                command.Content,
                _httpContextAccessor.HttpContext.User.Identity.Name,
                DateTimeOffset.Now);

            await _dbTemplate.Insert(toss);

            return(Unit.Value);
        }
Ejemplo n.º 3
0
        public async Task Handle_map_fields()
        {
            TossEntity instance = new TossEntity("lorem ipsum", "test", DateTimeOffset.Now);
            await _tossCosmosDB.Insert(instance);

            var res = await _mediator.Send(new Toss.Shared.Tosses.TossListAdminQuery(15, null));

            var first = res.Result.FirstOrDefault();

            Assert.Equal("lorem ipsum", first.Content);
            Assert.Equal(instance.CreatedOn, first.CreatedOn);
            Assert.Equal("test", first.UserName);
            Assert.NotNull(first.Id);
        }
Ejemplo n.º 4
0
        public void Should_handle_map_reduce_index_tombstones_update_delete()
        {
            using (var store = GetDocumentStore())
            {
                var mapReduceIndex = new Toss_TagPerDay();
                mapReduceIndex.Execute(store);

                using (var session = store.OpenSession())
                {
                    var entity = new TossEntity()
                    {
                        Tags = new List <string>()
                        {
                            "raven",
                            "nosql"
                        },
                        CreatedOn = DateTime.Now.AddDays(-1)
                    };
                    session.Store(entity);

                    session.SaveChanges();

                    Indexes.WaitForIndexing(store);

                    var mapReduceResults = session
                                           .Query <TagByDayIndex, Toss_TagPerDay>()
                                           .ToList();

                    Assert.Equal(2, mapReduceResults.Count);

                    store.Maintenance.Send(new DisableIndexOperation(mapReduceIndex.IndexName));

                    entity.CreatedOn = DateTime.Now;
                    session.SaveChanges();

                    session.Delete(entity);
                    session.SaveChanges();

                    store.Maintenance.Send(new EnableIndexOperation(mapReduceIndex.IndexName));

                    Indexes.WaitForIndexing(store);

                    mapReduceResults = session
                                       .Query <TagByDayIndex, Toss_TagPerDay>()
                                       .ToList();
                    Assert.Equal(0, mapReduceResults.Count);
                }
            }
        }
Ejemplo n.º 5
0
        public void Should_handle_map_index_tombstones_delete_and_put()
        {
            using (var store = GetDocumentStore())
            {
                var mapIndex = new Toss_ByCreatedOn();
                mapIndex.Execute(store);

                using (var session = store.OpenSession())
                {
                    var entity = new TossEntity()
                    {
                        Tags = new List <string>()
                        {
                            "raven",
                            "nosql"
                        },
                        CreatedOn = DateTime.Now.AddDays(-1)
                    };
                    session.Store(entity);

                    session.SaveChanges();

                    Indexes.WaitForIndexing(store);

                    var mapResults = session.Query <TossEntity, Toss_ByCreatedOn>().ToList();

                    Assert.Equal(1, mapResults.Count);

                    store.Maintenance.Send(new DisableIndexOperation(mapIndex.IndexName));

                    session.Delete(entity);
                    session.SaveChanges();

                    session.Store(entity, "TossEntities/1-A");
                    session.SaveChanges();

                    store.Maintenance.Send(new EnableIndexOperation(mapIndex.IndexName));

                    Indexes.WaitForIndexing(store);

                    mapResults = session.Query <TossEntity, Toss_ByCreatedOn>().Statistics(out var stats).ToList();
                    Assert.Equal(1, mapResults.Count);
                    Assert.Equal(2, stats.TotalResults);
                }
            }
        }
Ejemplo n.º 6
0
        public async Task <Unit> Handle(TossCreateCommand command, CancellationToken cancellationToken)
        {
            TossEntity toss;
            var        user = _httpContextAccessor.HttpContext.User;

            if (!command.SponsoredDisplayedCount.HasValue)
            {
                toss = new TossEntity(
                    command.Content,
                    user.UserId(),
                    now.Get());
            }
            else
            {
                toss = new SponsoredTossEntity(
                    command.Content,
                    user.UserId(),
                    now.Get(),
                    command.SponsoredDisplayedCount.Value);
            }
            toss.UserName = user.Identity.Name;
            var matchCollection = TossCreateCommand.TagRegex.Matches(toss.Content);

            toss.Tags   = matchCollection.Select(m => m.Groups[1].Value).ToList();
            toss.UserIp = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
            await _session.StoreAsync(toss);

            if (command.SponsoredDisplayedCount.HasValue)
            {
                ApplicationUser applicationUser = (await userManager.GetUserAsync(user));
                var             paymentResult   = await stripeClient.Charge(command.StripeChargeToken, command.SponsoredDisplayedCount.Value *TossCreateCommand.CtsCostPerDisplay, "Payment for sponsored Toss #" + toss.Id, applicationUser.Email);

                if (!paymentResult)
                {
                    _session.Delete(toss.Id);
                    throw new InvalidOperationException("Payment error on sponsored Toss ");
                }
            }
            await mediator.Publish(new TossCreated(toss));

            return(Unit.Value);
        }
Ejemplo n.º 7
0
        public async Task <Unit> Handle(TossCreateCommand command, CancellationToken cancellationToken)
        {
            TossEntity toss;
            var        user = _httpContextAccessor.HttpContext.User;

            if (!command.SponsoredDisplayedCount.HasValue)
            {
                toss = new TossEntity(
                    command.Content,
                    user.UserId(),
                    DateTimeOffset.Now);
            }
            else
            {
                toss = new SponsoredTossEntity(
                    command.Content,
                    user.UserId(),
                    DateTimeOffset.Now,
                    command.SponsoredDisplayedCount.Value);
            }
            toss.UserName = user.Identity.Name;
            toss.Id       = await _dbTemplate.Insert(toss);

            if (command.SponsoredDisplayedCount.HasValue)
            {
                ApplicationUser applicationUser = (await userManager.GetUserAsync(user));
                var             paymentResult   = await stripeClient.Charge(command.StripeChargeToken, command.SponsoredDisplayedCount.Value *TossCreateCommand.CtsCostPerDisplay, "Payment for sponsored Toss #" + toss.Id, applicationUser.Email);

                if (!paymentResult)
                {
                    await _dbTemplate.Delete(toss.Id);

                    throw new InvalidOperationException("Payment error on sponsored Toss ");
                }
            }
            await mediator.Publish(new TossCreated(toss.Id));

            return(Unit.Value);
        }