示例#1
0
        private async Task UpdateWebhookParameters(Offer offer, Webhook webhook, long webhookId)
        {
            List <WebhookParameter>        incompleteParams = ParseWebhookParameters(webhook);
            List <WebhookWebhookParameter> joinEntries      = await _webhookWebhookParameterService.GetAllJoinEntries(webhookId);

            Dictionary <string, WebhookParameter> paramsDb = new Dictionary <string, WebhookParameter>();
            HashSet <string> usedParamNames = new HashSet <string>();

            // Populate paramsDb so that it maps the WebhookParameter name to the WebhookParameter object
            foreach (WebhookWebhookParameter entry in joinEntries)
            {
                WebhookParameter webhookParameter = await _context.WebhookParameters.FindAsync(entry.WebhookParameterId);

                if (!paramsDb.ContainsKey(webhookParameter.Name))
                {
                    paramsDb.Add(webhookParameter.Name, webhookParameter);
                }
            }

            foreach (WebhookParameter incompleteParam in incompleteParams)
            {
                // Check if a param with the same name as the incompleteParam already exists
                if (!paramsDb.ContainsKey(incompleteParam.Name))
                {
                    // A param with the same name as the incompleteParam does not exist, so create it
                    await _webhookParameterService.CreateAsync(offer.OfferName, webhookId, incompleteParam);
                }

                // Keep track of all the new parameters we are using in usedParamNames
                if (!usedParamNames.Contains(incompleteParam.Name))
                {
                    usedParamNames.Add(incompleteParam.Name);
                }
            }

            foreach (KeyValuePair <string, WebhookParameter> paramDb in paramsDb)
            {
                // Check if there is a param in the db that we are no longer using
                if (!usedParamNames.Contains(paramDb.Key))
                {
                    WebhookWebhookParameter webhookWebhookParameter = await _context.WebhookWebhookParameters.FindAsync(webhookId, paramDb.Value.Id);

                    // Remove the join entry for any unused params
                    _context.WebhookWebhookParameters.Remove(webhookWebhookParameter);
                    await _context._SaveChangesAsync();

                    _logger.LogInformation(LoggingUtils.ComposeResourceDeletedMessage(
                                               nameof(WebhookWebhookParameter),
                                               $"(webhookId={webhookId}, webhookParameterId={webhookWebhookParameter.WebhookParameter})",
                                               offerName: offer.OfferName));
                }
            }
        }
        /// <summary>
        /// Creates an entry in the webhookWebhookParameters table if it does not exist.
        /// </summary>
        /// <param name="webhookId">The ID of the webhook.</param>
        /// <param name="webhookParameterId">The ID of the webhookParameter.</param>
        /// <returns></returns>
        public async Task CreateJoinEntryAsync(long webhookId, long webhookParameterId)
        {
            if (await ExistsAsync(webhookId, webhookParameterId))
            {
                throw new LunaConflictUserException(LoggingUtils.ComposeAlreadyExistsErrorMessage(typeof(WebhookWebhookParameter).Name,
                                                                                                  $"({webhookId}, {webhookParameterId})"));
            }

            WebhookWebhookParameter webhookWebhookParameter = new WebhookWebhookParameter
            {
                WebhookId          = webhookId,
                WebhookParameterId = webhookParameterId
            };

            _context.WebhookWebhookParameters.Add(webhookWebhookParameter);
            await _context._SaveChangesAsync();

            _logger.LogInformation(LoggingUtils.ComposeResourceCreatedMessage(typeof(WebhookWebhookParameter).Name, $"(webhookId={webhookId}, webhookParameterId={webhookParameterId})"));
        }