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);
        }
예제 #2
0
        public async Task <List <BestTagsResult> > Handle(BestTagsQuery request, CancellationToken cancellationToken)
        {
            var firstDay = _now.Get().Date.AddDays(-30);

            return((await _session
                    .Query <TagByDayIndex, Toss_TagPerDay>()
                    .Where(i => i.CreatedOn >= firstDay)
                    .ToListAsync())
                   //this needs has to be handled server side or at least cached
                   .GroupBy(i => i.Tag)
                   .OrderByDescending(g => g.Sum(i => i.Count))
                   .Take(50)
                   .Select(t => new BestTagsResult()
            {
                CountLastMonth = t.Sum(i => i.Count),
                Tag = t.Key
            })
                   .ToList());
        }