public List <Book> AddBook(List <Book> books) { List <Book> notAdded = new List <Book>(); foreach (Book b in books) { DataRow authorRow = null; var canAdd = IsUniqueBookinDb(b, out authorRow); if (!canAdd) { DataRow[] items = _libraryDataSet.Items.Select($"Name = '{b.Name}' and Publisher = '{b.Publisher}'"); foreach (var dataRow in items) { var item = (libraryDataSet.ItemsRow)dataRow; if (item.GetBooksRows()[0].AuthorsRow.Name != b.Name) { continue; } libraryDataSet.CopiesRow Copy = _libraryDataSet.Copies.AddCopiesRow(Guid.NewGuid().ToString(), item, false); Copy.ItemID = item.ID; _provider.UpdateAllData(); } notAdded.Add(b); continue; } libraryDataSet.ItemsRow itemRow = _libraryDataSet.Items.AddItemsRow(b.Id, b.Name, b.Publisher, b.PublishedDate); //если такого автора еще не было libraryDataSet.AuthorsRow author; if (authorRow == null) { author = _libraryDataSet.Authors.AddAuthorsRow(Guid.NewGuid().ToString(), b.AuthorName); } else { author = (libraryDataSet.AuthorsRow)authorRow; } var book = _libraryDataSet.Books.AddBooksRow(itemRow, author, b.ISBN); book.AuthorID = author.ID; book.AuthorsRow = author; if (book.AuthorsRow == null) { return(new List <Book>(0)); } var copy = _libraryDataSet.Copies.AddCopiesRow(Guid.NewGuid().ToString(), itemRow, false); copy.ItemID = b.Id; _provider.UpdateAllData(); } return(notAdded); }
public List <Article> AddArticles(List <Article> newArticles) { var notAdded = new List <Article>(); foreach (Article article in newArticles) { DataRow author = null; bool isUnique = IsUniqueArticleInDb(article, out author); if (!isUnique) { var artRows = _libraryDataSet.Articles.Select(String.Format("Version = '{0}'", article.Version)); foreach (var dataRow in artRows) { var artRow = (libraryDataSet.ArticlesRow)dataRow; if (artRow.ItemsRow.Name != article.Name || artRow.ItemsRow.Publisher != article.Publisher || artRow.AuthorsRow.Name != article.AuthorName) { continue; } var Copy = _libraryDataSet.Copies.AddCopiesRow(Guid.NewGuid().ToString(), artRow.ItemsRow, false); Copy.ItemID = artRow.ItemsRow.ID; _provider.UpdateAllData(); } notAdded.Add(article); continue; } libraryDataSet.ItemsRow itemRow = _libraryDataSet.Items.AddItemsRow(article.Id, article.Name, article.Publisher, article.PublishedDate); libraryDataSet.AuthorsRow authorRow; if (author == null) { authorRow = _libraryDataSet.Authors.AddAuthorsRow(Guid.NewGuid().ToString(), article.AuthorName); } else { authorRow = (libraryDataSet.AuthorsRow)author; } libraryDataSet.ArticlesRow articleRow = _libraryDataSet.Articles.AddArticlesRow(itemRow, authorRow, article.Version); DataRowCollection allMagazines = _libraryDataSet.Magazines.Rows; var searchedMagazines = allMagazines.Cast <libraryDataSet.MagazinesRow>() .Where(magazine => magazine.ItemsRow.Name == article.MagazineName && magazine.IssueNumber == article.MagazineIssueNumber) .ToList(); if (searchedMagazines.Count == 0) { return(new List <Article>(0)); } foreach (var magazineRow in searchedMagazines) { _libraryDataSet.ArticlesInMagazines.AddArticlesInMagazinesRow(articleRow, magazineRow); } libraryDataSet.CopiesRow copy = _libraryDataSet.Copies.AddCopiesRow(Guid.NewGuid().ToString(), itemRow, false); copy.ItemID = article.Id; _provider.UpdateAllData(); } return(notAdded); }