public override async Task <SendSmsCommand> HandleAsync(
            SendSmsCommand command,
            CancellationToken cancellationToken = default)
        {
            var messages = command.Messages
                           .Select(m => new SmsMessage(m, _smsCreditCalculator.Calculate(m.Body)));

            await EnsureBalanceIsSufficient(messages, command.UserId);
            await PublishEventAsync(command.UserId, messages, cancellationToken);

            return(await base.HandleAsync(command, cancellationToken));
        }
Esempio n. 2
0
        public override async Task <DeductSmsCreditsCommand> HandleAsync(
            DeductSmsCreditsCommand command,
            CancellationToken cancellationToken = default)
        {
            var user = await _applicationDbContext.Users
                       .FirstAsync(u => u.Id == command.UserId, cancellationToken);

            user.DeductSmsBalance(_smsCreditCalculator.Calculate(command.SmsBody));
            await _applicationDbContext.SaveChangesAsync(cancellationToken);

            return(await base.HandleAsync(command, cancellationToken));
        }