예제 #1
0
        public void GetComments_WhenEmptyContext_ReturnsEmptyList()
        {
            // Arrange

            // Act
            var commentDtos = _commentManager.GetAllAsync().GetAwaiter().GetResult();

            // Assert
            Assert.Empty(commentDtos);
        }
        /// <inheritdoc/>
        public async Task AddAsync()
        {
            _logger.LogInformation(SyncMessage.CommentAddStart);

            var commentsCloud = await _cloudManager.GetComments().ToListAsync();

            var commentsApp = (await _commentManager.GetAllAsync()).ToList();
            var postsApp    = (await _postManager.GetAllAsync()).ToList();

            var commentCloudIds = commentsCloud.Select(c => c.Id);
            var commentAppIds   = commentsApp.Select(c => c.CloudId);

            var newIds   = commentCloudIds.Except(commentAppIds);
            var comments = commentsCloud.Join(
                newIds,
                commentCloud => commentCloud.Id,
                id => id,
                (commentCloud, id) => commentCloud);

            if (comments.Any())
            {
                foreach (var comment in comments)
                {
                    var post = postsApp.FirstOrDefault(p => p.CloudId == comment.PostId);

                    if (post != null)
                    {
                        var commentDto = new CommentDto
                        {
                            CloudId = comment.Id,
                            PostId  = post.Id,
                            Name    = comment.Name,
                            Email   = comment.Email,
                            Body    = comment.Body
                        };

                        await _commentManager.CreateAsync(commentDto);
                    }
                    else
                    {
                        _logger.LogError(SyncMessage.CommentAddError, comment.PostId);
                    }
                }
            }

            _logger.LogInformation(SyncMessage.CommentAddEnd);
        }