Beispiel #1
0
        public void GetPosts_WhenEmptyContext_ReturnsEmptyList()
        {
            // Arrange

            // Act
            var postsDtos = _postManager.GetAllAsync().GetAwaiter().GetResult();

            // Assert
            Assert.Empty(postsDtos);
        }
Beispiel #2
0
        // Endpoints
        public async Task OnGet()
        {
            try
            {
                Posts = await _postMinion.GetAllAsync();

                if (Posts == null)
                {
                    List <Post> None = new List <Post>();
                    Post        nope = new Post();
                    nope.Author = "nope";
                    nope.Title  = "nada";
                    None.Add(nope);
                    Posts = None;
                    throw new NullReferenceException("Couldn't find any posts");
                }
            } catch (Exception e)
            {
                ViewData["ErrorMessage"] = e.Message;
                List <Post> None = new List <Post>();
                Post        nope = new Post();
                nope.Author = "ERROR";
                nope.Title  = "ERROR";
                None.Add(nope);
                Posts = None;
            }
        }
        /// <inheritdoc/>
        public async Task AddAsync()
        {
            _logger.LogInformation(SyncMessage.PostAddStart);

            var postsCloud = await _cloudManager.GetPosts().ToListAsync();

            var postsApp  = (await _postManager.GetAllAsync()).ToList();
            var peopleApp = (await _personManager.GetAllAsync()).ToList();

            var postsCloudIds = postsCloud.Select(p => p.Id);
            var postsAppIds   = postsApp.Select(p => p.CloudId);

            var newIds = postsCloudIds.Except(postsAppIds);

            var posts = postsCloud.Join(
                newIds,
                postCloud => postCloud.Id,
                id => id,
                (postCloud, id) => postCloud);

            if (posts.Any())
            {
                foreach (var post in posts)
                {
                    var user = peopleApp.FirstOrDefault(p => p.CloudId == post.UserId);

                    if (user != null)
                    {
                        var postDto = new PostDto
                        {
                            CloudId = post.Id,
                            UserId  = user.Id,
                            Title   = post.Title,
                            Body    = post.Body
                        };

                        await _postManager.CreateAsync(postDto);
                    }
                    else
                    {
                        _logger.LogError(SyncMessage.PostAddError, post.UserId);
                    }
                }
            }

            _logger.LogInformation(SyncMessage.PostAddEnd);
        }
        /// <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);
        }