示例#1
0
        public async Task <ObjectResult> AddSubscription(AddSubscriptionModel addSubscriptionModel)
        {
            var subscriberExists = await accountsService.CheckIfUserExists(new UserModel { Username = addSubscriptionModel.Subscriber });

            var authorExists = await accountsService.CheckIfUserExists(new UserModel { Username = addSubscriptionModel.Author });

            logger.LogInformation($"Subscriber response: {subscriberExists?.StatusCode}, author response: {authorExists?.StatusCode}");

            if (subscriberExists == null || authorExists == null)
            {
                return(StatusCode(503, "Accounts service unavailable"));
            }
            if (subscriberExists.StatusCode != System.Net.HttpStatusCode.OK)
            {
                return(StatusCode(500, "Subscriber doesn't exists"));
            }
            if (authorExists.StatusCode != System.Net.HttpStatusCode.OK)
            {
                return(StatusCode(500, "Author doesn't exists"));
            }

            var response = await subscriptionsService.AddSubscription(addSubscriptionModel.Subscriber, addSubscriptionModel.Author);

            if (response != null)
            {
                logger.LogInformation($"Attempt to add subscription {addSubscriptionModel.Subscriber}-{addSubscriptionModel.Author}, response {response.StatusCode}");
                if (response.IsSuccessStatusCode)
                {
                    eventBus.Publish(new AddSubscriptionEvent
                    {
                        Author     = addSubscriptionModel.Author,
                        Subscriber = addSubscriptionModel.Subscriber
                    });
                    return(Ok(""));
                }
                else
                {
                    return(StatusCode(500, response.Content?.ReadAsStringAsync()?.Result));
                }
            }
            else
            {
                logger.LogCritical("Subscriptions service unavailable");
                return(StatusCode(503, "Service unavailable"));
            }
        }
        public async Task <IActionResult> AddSubscription(SubscriptionAddModel model)
        {
            await _subscriptionsService.AddSubscription(model);

            return(CreatedAtAction(nameof(GetSubscription), new { id = model.Id }, null));
        }