public async Task<ActionResult<GenericResponse>> RedeemPost(string botNames, [FromBody] BotRedeemRequest request) { if (string.IsNullOrEmpty(botNames) || (request == null)) { ASF.ArchiLogger.LogNullError(nameof(botNames) + " || " + nameof(request)); return BadRequest(new GenericResponse(false, string.Format(Strings.ErrorIsEmpty, nameof(botNames) + " || " + nameof(request)))); } if (request.KeysToRedeem.Count == 0) { return BadRequest(new GenericResponse(false, string.Format(Strings.ErrorIsEmpty, nameof(request.KeysToRedeem)))); } HashSet<Bot> bots = Bot.GetBots(botNames); if ((bots == null) || (bots.Count == 0)) { return BadRequest(new GenericResponse(false, string.Format(Strings.BotNotFound, botNames))); } IList<ArchiHandler.PurchaseResponseCallback> results = await Utilities.InParallel(bots.Select(bot => request.KeysToRedeem.Select(key => bot.Actions.RedeemKey(key))).SelectMany(task => task)).ConfigureAwait(false); Dictionary<string, IReadOnlyDictionary<string, ArchiHandler.PurchaseResponseCallback>> result = new Dictionary<string, IReadOnlyDictionary<string, ArchiHandler.PurchaseResponseCallback>>(bots.Count, Bot.BotsComparer); int count = 0; foreach (Bot bot in bots) { Dictionary<string, ArchiHandler.PurchaseResponseCallback> responses = new Dictionary<string, ArchiHandler.PurchaseResponseCallback>(request.KeysToRedeem.Count, StringComparer.Ordinal); result[bot.BotName] = responses; foreach (string key in request.KeysToRedeem) { responses[key] = results[count++]; } } return Ok(new GenericResponse<IReadOnlyDictionary<string, IReadOnlyDictionary<string, ArchiHandler.PurchaseResponseCallback>>>(result.Values.SelectMany(responses => responses.Values).All(value => value != null), result)); }
public async Task <ActionResult <GenericResponse <IReadOnlyDictionary <string, ArchiHandler.PurchaseResponseCallback> > > > RedeemPost(string botName, [FromBody] BotRedeemRequest request) { if (string.IsNullOrEmpty(botName) || (request == null)) { ASF.ArchiLogger.LogNullError(nameof(botName) + " || " + nameof(request)); return(BadRequest(new GenericResponse <IReadOnlyDictionary <string, ArchiHandler.PurchaseResponseCallback> >(false, string.Format(Strings.ErrorIsEmpty, nameof(botName) + " || " + nameof(request))))); } if (request.KeysToRedeem.Count == 0) { return(BadRequest(new GenericResponse <IReadOnlyDictionary <string, ArchiHandler.PurchaseResponseCallback> >(false, string.Format(Strings.ErrorIsEmpty, nameof(request.KeysToRedeem))))); } if (!Bot.Bots.TryGetValue(botName, out Bot bot)) { return(BadRequest(new GenericResponse <IReadOnlyDictionary <string, ArchiHandler.PurchaseResponseCallback> >(false, string.Format(Strings.BotNotFound, botName)))); } IList <ArchiHandler.PurchaseResponseCallback> results = await Utilities.InParallel(request.KeysToRedeem.Select(key => bot.Actions.RedeemKey(key))).ConfigureAwait(false); Dictionary <string, ArchiHandler.PurchaseResponseCallback> result = new Dictionary <string, ArchiHandler.PurchaseResponseCallback>(request.KeysToRedeem.Count); foreach (string key in request.KeysToRedeem) { result[key] = results[result.Count]; } return(Ok(new GenericResponse <IReadOnlyDictionary <string, ArchiHandler.PurchaseResponseCallback> >(result.Values.All(value => value != null), result))); }