/// <inheritdoc /> public async Task <ThreadStatusDto> GetPost(ThreadStatusRequest request) { var normalizedCharacterUrlIdentifier = request.CharacterUrlIdentifier?.ToLowerInvariant(); if (string.IsNullOrWhiteSpace(request.PostId) || string.IsNullOrWhiteSpace(normalizedCharacterUrlIdentifier)) { return(null); } var post = await RetrieveApiDataByPost(request.PostId, normalizedCharacterUrlIdentifier); return(new ThreadStatusDto(post, request)); }
public async Task ReturnsNullForMissingPostId() { // Arrange var request = new ThreadStatusRequest { PostId = null, CharacterUrlIdentifier = "blackjackkent" }; var postService = new PostService(_mockPolicyProvider.Object, _mockConfigWrapper.Object, _mockFactory.Object, _mockLogger.Object); // Act var result = await postService.GetPost(request); // Assert result.Should().Be(null); }
public async Task <IActionResult> Get(string postId, string characterUrlIdentifier, string partnerUrlIdentifier = null, DateTime?dateMarkedQueued = null) { try { var request = new ThreadStatusRequest { PostId = postId, CharacterUrlIdentifier = characterUrlIdentifier, PartnerUrlIdentifier = partnerUrlIdentifier, DateMarkedQueued = dateMarkedQueued }; var post = await _postService.GetPost(request); return(Ok(post)); } catch (Exception e) { _logger.LogError($"Error retrieving thread with post ID {postId}: {e.Message}", e); return(StatusCode(500, "An unknown error occurred.")); } }
public ThreadStatusDtoTests() { _mockAdapter = new Mock <IPostAdapter>(); var queuedDate = DateTime.Now; _beforeQueuedDate = DateTime.Now.Subtract(TimeSpan.FromHours(3)); _afterQueuedDate = DateTime.Now.Add(TimeSpan.FromHours(3)); _request = new ThreadStatusRequest { ThreadId = 54321, CharacterUrlIdentifier = "blackjackkent", DateMarkedQueued = queuedDate, PostId = "12345", PartnerUrlIdentifier = "mypartner" }; _mockAdapter.Setup(a => a.GetMostRecentRelevantNote(_request.CharacterUrlIdentifier, _request.PartnerUrlIdentifier)) .Returns((BaseNote)null); _mockAdapter.SetupGet(a => a.Id).Returns(_request.PostId); _mockAdapter.SetupGet(a => a.Timestamp).Returns(_beforeQueuedDate); _mockAdapter.SetupGet(a => a.Url).Returns("http://www.test.com"); _mockAdapter.SetupGet(a => a.BlogName).Returns(_request.PartnerUrlIdentifier); }
public async Task NormalizesCharacterUrl() { // Arrange var request = new ThreadStatusRequest { PostId = "123456", CharacterUrlIdentifier = "BlackjackKent" }; var mockResult = new Posts { Result = new List <BasePost>().ToArray() }; InitMockClient(mockResult); var postService = new PostService(_mockPolicyProvider.Object, _mockConfigWrapper.Object, _mockFactory.Object, _mockLogger.Object); // Act await postService.GetPost(request); // Assert _passedMethod.Url.Should().Contain("blackjackkent"); }
public async Task ReturnsEmptyAdapterIfNoResults() { // Arrange var request = new ThreadStatusRequest { PostId = "123456", CharacterUrlIdentifier = "test" }; var mockResult = new Posts() { Result = System.Array.Empty <BasePost>() }; InitMockClient(mockResult); var postService = new PostService(_mockPolicyProvider.Object, _mockConfigWrapper.Object, _mockFactory.Object, _mockLogger.Object); // Act var result = await postService.GetPost(request); // Assert result.PostId.Should().Be(request.PostId); result.LastPostUrl.Should().BeNull(); }
public async Task ReturnsPopulatedAdapterIfResultValid() { // Arrange var request = new ThreadStatusRequest { PostId = "123456", CharacterUrlIdentifier = "test" }; var mockResult = new Posts { Result = new[] { new BasePost { Id = int.Parse(request.PostId, CultureInfo.InvariantCulture), BlogName = request.CharacterUrlIdentifier, Url = "http://www.test.com" }, new BasePost { Id = int.Parse(request.PostId, CultureInfo.InvariantCulture), BlogName = request.CharacterUrlIdentifier, Url = "http://www.test2.com" } } }; InitMockClient(mockResult); var postService = new PostService(_mockPolicyProvider.Object, _mockConfigWrapper.Object, _mockFactory.Object, _mockLogger.Object); // Act var result = await postService.GetPost(request); // Assert result.PostId.Should().Be(request.PostId); result.LastPostUrl.Should().Be("http://www.test.com"); }
/// <summary> /// Initializes a new instance of the <see cref="ThreadStatusDto"/> class. /// </summary> /// <param name="post">The post adapter.</param> /// <param name="request">The initial request object that triggered the retrieval of thread information.</param> public ThreadStatusDto(IPostAdapter post, ThreadStatusRequest request) { ThreadId = request.ThreadId; if (post == null) { PostId = request.PostId; LastPostDate = null; LastPosterUrlIdentifier = null; LastPostUrl = null; IsCallingCharactersTurn = true; IsQueued = false; return; } var note = post.GetMostRecentRelevantNote(request.CharacterUrlIdentifier, request.PartnerUrlIdentifier); var dateMarkedQueued = request.DateMarkedQueued?.ToUniversalTime(); var lastPostDate = note?.Timestamp.ToUniversalTime() ?? post.Timestamp.ToUniversalTime(); PostId = post.Id; LastPostDate = lastPostDate; LastPosterUrlIdentifier = note?.BlogName ?? post.BlogName; LastPostUrl = note != null ? note.BlogUrl + "post/" + note.PostId : post.Url; IsCallingCharactersTurn = !string.Equals(LastPosterUrlIdentifier, request.CharacterUrlIdentifier, StringComparison.CurrentCultureIgnoreCase); IsQueued = dateMarkedQueued != null && dateMarkedQueued.Value > lastPostDate; }