Ejemplo n.º 1
0
        public async Task <Result> Subscribe(AddSubscriptionsDTO addSubscriptionsDto)
        {
            var message = new HttpRequestMessage(HttpMethod.Post, "subscription");

            message.Content = new StringContent(JsonHandler.Serialize(addSubscriptionsDto), Encoding.UTF8,
                                                "application/json");
            var response = await SendRequest(message);

            if (response.IsFailure)
            {
                return(Result.Failure("Unsubscribe failed."));
            }

            var deserializeResult =
                JsonHandler.Deserialize <List <SerializableResult> >(await response.Value.Content.ReadAsStringAsync());

            if (deserializeResult.IsFailure)
            {
                _logger.LogError("Deserialization failed due to: {errorDetails}.", deserializeResult.Error);
                return(Result.Failure("Deserialization failed."));
            }

            var failedSubscriptions = deserializeResult.Value.Where(r => !r.IsSuccess).ToList();

            if (failedSubscriptions.Any())
            {
                _logger.LogError("Subscribe failed due to: {errorDetails}", string.Join('\n', failedSubscriptions.Select(fSub => fSub.Error)));
                return(Result.Failure("Subscribe failed."));
            }

            return(Result.Success());
        }
Ejemplo n.º 2
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 addSubscriptionDtos = mediaNames.Select(mediaName => new AddSubscriptionDTO(mediaName)).ToList();
            var addSubscriptionsDto = new AddSubscriptionsDTO(notificationEndpointNotifierIdentifier, addSubscriptionDtos);
            var result = await _vrPersistenceClient.Subscribe(addSubscriptionsDto);

            var message = result.IsFailure
                ? "Something went wrong."
                : "Successfully subscribed.";
            await Context.Channel.SendMessageAsync(message);
        }
Ejemplo n.º 3
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()));
            }
        }