Exemple #1
0
        public async Task SynchronizeInformationAsync()
        {
            // Update books
            var booksApiResult = await _operationHttp.GetRequestAsync("api/v1/books", null, HttpMethod.Get);

            var booksApiList = JsonConvert.DeserializeObject <List <BookDto> >(booksApiResult);
            var booksDbFound = await _bookRepository.GetBooksAsync(booksApiList.Select(b => b.Id).ToList());

            var booksToModify = _mapper.Map <List <Books> >(booksApiList);

            booksDbFound.ToList()
            .ForEach(b =>
            {
                var book      = booksToModify.FirstOrDefault(p => p.BookId == b.BookId);
                b.Description = book?.Description ?? b.Description;
                b.Excerpt     = book?.Excerpt ?? b.Excerpt;
                b.PageCount   = book?.PageCount ?? b.PageCount;
                b.PublishDate = book?.PublishDate ?? b.PublishDate;
                b.Title       = book?.Title ?? b.Title;
            });

            await _bookRepository.UpdateBooksAsync(booksDbFound.ToList());

            await _bookRepository.AddBooksAsync(booksToModify.Where(p => !booksDbFound.Any(m => m.BookId == p.BookId)).ToList());

            // Update authors
            var authorsApiResult = await _operationHttp.GetRequestAsync("api/v1/authors", null, HttpMethod.Get);

            var authorsList    = JsonConvert.DeserializeObject <List <AuthorApiDto> >(authorsApiResult);
            var authorsDbFound = await _authorRepository.GetAuthorsAsync(false, authorsList.Select(b => b.Id).ToList());

            var authorsToModify = _mapper.Map <List <Authors> >(authorsList);

            authorsDbFound.ToList()
            .ForEach(a =>
            {
                var author  = authorsToModify.FirstOrDefault(p => p.Id == a.Id);
                a.FirstName = author?.FirstName ?? a.FirstName;
                a.LastName  = author?.LastName ?? a.LastName;
                a.BookId    = author?.BookId ?? a.BookId;
            });

            await _authorRepository.UpdateAuthorsAsync(authorsDbFound.ToList());

            await _authorRepository.AddAuthorsAsync(authorsToModify.Where(p => !authorsDbFound.Any(m => m.Id == p.Id)).ToList());
        }