Ejemplo n.º 1
0
        public async Task Subscribe(params String[] mediaNames)
        {
            if (mediaNames == null || mediaNames.Length < 1)
            {
                await Context.Channel.SendMessageAsync("Nothing to do.");
            }
            var notificationEndpointNotifierIdentifier = GetNotificationEndpointNotifierIdentifier(Context);
            var notificationEndpointResult             = await _notificationEndpointService.AddNotificationEndpoint(new NotificationEndpoint(notificationEndpointNotifierIdentifier, new List <Subscription>()), CancellationToken.None);

            var message = "Something went wrong";

            if (notificationEndpointResult.IsSuccess)
            {
                var subscriptions = mediaNames
                                    .Select(mediaName => new Subscription(mediaName, notificationEndpointNotifierIdentifier))
                                    .ToList();

                var result = await _subscriptionService.AddSubscriptions(subscriptions, CancellationToken.None);

                if (result.All(r => r.IsSuccess))
                {
                    message = "Successfully subscribed";
                }
            }
            await Context.Channel.SendMessageAsync(message);
        }
Ejemplo n.º 2
0
        public async Task <JsonResult> AddSubscriptions([FromBody] AddSubscriptionsDTO addSubscriptionsDto)
        {
            try
            {
                var results = new List <Result>();
                var result  =
                    await _notificationEndpointService.AddNotificationEndpoint(
                        new NotificationEndpoint(addSubscriptionsDto.NotificationEndpointIdentifier));

                if (result.IsSuccess)
                {
                    // we do not use the automatic integration of fluentValidation into ASP.NET Core (validating objects that are passed in to controller actions), as we want to add ALL valid releases and not stop and throw if one is invalid)
                    var subscriptionValidator = new SubscriptionValidator(_trackedMediaSettings);
                    foreach (var addSubscription in addSubscriptionsDto.Subscriptions)
                    {
                        var validationResult = await subscriptionValidator.ValidateAsync(addSubscription);

                        if (validationResult.IsValid)
                        {
                            results.Add(await _subscriptionService.AddSubscription(new Subscription(addSubscription,
                                                                                                    addSubscriptionsDto.NotificationEndpointIdentifier)));
                        }
                        else
                        {
                            results.Add(Result.Failure(validationResult.GetMessage()));
                        }
                    }
                }
                else
                {
                    results.Add(result);
                }

                return(new JsonResult(results.Select(r => r.AsSerializableResult())));
            }
            catch (Exception e)
            {
                _logger.LogError("Something went wrong. {exceptionMessage}", e.Message);
                return(new JsonResult(Result.Failure("Adding the subscriptions failed.").AsSerializableResult()));
            }
        }