Esempio n. 1
0
        public void ShouldPrepareMessageForUser()
        {
            //Arrange
            var expectedMessage = $"<h1>Subscribed for:</h1>"
                                  + $"<ul>"
                                  + $"<li>.NET Framework</li>"
                                  + $"<li>CI/CD</li>"
                                  + $"<li>TeamCity</li>"
                                  + $"</ul>"
                                  + $"<div>To unsubscribe topics press link:</div>"
                                  + $"<a href=\"http://unsubscribe.test/com\">Unsubscribe</a>";
            var listOfTopics = new[]
            {
                ".NET Framework",
                "CI/CD",
                "TeamCity"
            };
            const string unsubscribeLink = "http://unsubscribe.test/com";

            this.cut = new SubscriptionMessageFormatter(listOfTopics, unsubscribeLink);

            //Act
            var resultMessage = this.cut.Process();

            //Assert
            Assert.AreEqual(expectedMessage, resultMessage);
        }
Esempio n. 2
0
        public async Task <IActionResult> SubscribeAsync([FromBody] SubscribeRequest input)
        {
            const string unubscriptionPath = "api/v1/subscriptions";

            try
            {
                var unsubscribeToken = await this.subscriptionService.SubscribeAsync(input.UserId, input.Topics);

                var domain          = new Uri($"{Request.Scheme}://{Request.Host}");
                var unsubscribeLink =
                    $"{domain}{unubscriptionPath}?{nameof(UnsubscribeRequest.UnsubscribeGuid)}={unsubscribeToken.ToString()}&{nameof(UnsubscribeRequest.UserId)}={input.UserId}";

                //TODO: This formatter below should not be created here - it should be created in some factory the the factory should be injected to this controller
                var formattedMessage = new SubscriptionMessageFormatter(this.GetMatchedTopics(input.Topics), unsubscribeLink);
                await this.mailingService.SendEmailAsync(input.UserId, formattedMessage.Process(), subscribedSubject);

                return(Created(unsubscribeToken.ToString(), new
                {
                    unsubscribeLink
                }));
            }
            catch (TopicNotExistsException ex)
            {
                this.logger.LogError(ex, "Topic not exists");
                return(this.GetBadRequestResult(ex.Message));
            }
            catch (UserExistsException ex)
            {
                this.logger.LogError(ex, "Subscription already exist");
                return(this.GetBadRequestResult(ex.Message));
            }
            catch (Exception ex)
            {
                this.logger.LogError(ex, "Subscription error");
                throw;
            }
        }