private async Task UpdateAuthorsListInDatabase(int bookId, List <string> authors) { var authorsToUpdateIds = await _booksAuthorsRepository.GetAuthorsIdsByBookId(bookId); foreach (var idToUpdate in authorsToUpdateIds) { await _booksAuthorsRepository.RemoveAuthorForBook(new BookAuthor(bookId, idToUpdate)); var relations = await _booksAuthorsRepository.GetBooksIdsByAuthorid(idToUpdate); if (relations.Count() == 0) { await _authorsRepository.RemoveAuthorById(idToUpdate); } } List <Author> newAuthors = new List <Author>(); foreach (var authorToLoad in authors) { newAuthors.Add(new Author() { FirstName = authorToLoad.Split(" ")[0], LastName = authorToLoad.Split(" ")[1] }); } List <int> newAuthorsIds = new List <int>(); foreach (var newAuthor in newAuthors) { var IsAuthorExists = await _authorsRepository.IsAuthorExists(newAuthor); if (!IsAuthorExists) { await _authorsRepository.AddAuthorData(newAuthor); } int authorId = await _authorsRepository.GetAuthorId(newAuthor); newAuthorsIds.Add(authorId); } foreach (var newAuthorsId in newAuthorsIds) { await _booksAuthorsRepository.AddBookAuthor(new BookAuthor(bookId, newAuthorsId)); } }