Ejemplo n.º 1
0
        public async Task <ActionResult <WebhookSubscriptionReadDto> > CreateSubscription(
            WebhookSubscriptionCreateDto webhookSubscriptionCreateDto)
        {
            var subscription =
                await _context.WebhookSubscriptions.FirstOrDefaultAsync(s =>
                                                                        s.WebhookURI == webhookSubscriptionCreateDto.WebhookURI);

            if (subscription is not null)
            {
                return(await Task.FromResult(NoContent()));
            }
            subscription                  = _mapper.Map <WebhookSubscription>(webhookSubscriptionCreateDto);
            subscription.Secret           = Guid.NewGuid().ToString();
            subscription.WebhookPublisher = "AmTech";
            try
            {
                await _context.AddAsync(subscription);

                await _context.SaveChangesAsync();

                var webhookSecret = new WebhookSecretMessageDto
                {
                    Publisher = subscription.WebhookPublisher,
                    Secret    = subscription.Secret
                };

                _messageBus.SendWebhookSecretMessage(webhookSecret);
            }
            catch (Exception ex)
            {
                return(await Task.FromResult(BadRequest(ex.Message)));
            }

            var response = _mapper.Map <WebhookSubscriptionReadDto>(subscription);

            return(CreatedAtRoute(
                       routeName: "GetSubscriptionBySecret",
                       routeValues: new { secret = response.Secret },
                       value: response));
        }