Exemplo n.º 1
0
        /// <inheritdoc />
        public async Task <(IEnumerable <(Registration, ChatBot)> Registrations, Error Error)> GetUserRegistrationsAsync(
            string username,
            CancellationToken cancellationToken = default
            )
        {
            (IEnumerable <(Registration, ChatBot)> Registrations, Error Error)result;

            var regs = (
                await _regsRepo.GetAllForUserAsync(username, cancellationToken)
                .ConfigureAwait(false)
                ).ToArray();

            if (regs.Any())
            {
                // ToDo maybe use mongodb functions for this aggregation
                var getBotTasks = regs
                                  .Select(r => r.ChatBotDbRef.Id.ToString())
                                  .Distinct(StringComparer.OrdinalIgnoreCase)
                                  .Select(botId => _botsRepo.GetByIdAsync(botId, cancellationToken));

                var bots = await Task.WhenAll(getBotTasks)
                           .ConfigureAwait(false);

                var aggregatedRegs = regs
                                     .Select(r => (r, bots.Single(b => b.Id == r.ChatBotDbRef.Id.ToString())))
                                     .ToArray();

                result = (aggregatedRegs, null);
            }
            else
            {
                result = (null, new Error(ErrorCode.RegistrationNotFound));
            }

            return(result);
        }