public void SetJson(string json) { IEnumerable <GetBookResponseModel> deserRes = JsonConvert.DeserializeObject <IEnumerable <GetBookResponseModel> >(json); if (deserRes == null) { return; } foreach (GetBookResponseModel book in deserRes) { IEnumerable <GetAuthorInBookResponseModel> aInBs = book.Authors; IEnumerable <PublishedBook> publishedBooks = book.PublishedBooks; Book clearBook = mapper.Map <Book>(book); int bookId = db.Books.Create(clearBook); Book newBook = db.Books.Get(bookId); foreach (GetAuthorInBookResponseModel ainb in aInBs) { var authorId = db.Authors.Create(ainb.Author); ainb.Author = db.Authors.GetAuthor(authorId); var newainb = new AuthorInBook { Author_Id = authorId, Book_Id = bookId }; db.AuthorsInBooks.Create(newainb); } foreach (PublishedBook publishedBook in publishedBooks) { publishedBook.Book = newBook; publishedBook.Book_Id = bookId; db.PublishedBooks.Create(publishedBook); } } db.Save(); }
private void UpdateRepository(BookUpdateViewModel bookView, List <int> authorId) { List <AuthorInBook> authorInBookReposytory = _authorInBookRepository.GetBook(bookView.Id).ToList(); if (authorInBookReposytory.Count == authorId.Count) { // количество авторов равно, изменение таблицы не требуется } if (authorInBookReposytory.Count > authorId.Count)//требуется удаление { var removedList = new List <AuthorInBook>(authorInBookReposytory); foreach (int id in authorId) { AuthorInBook authorInBookRemove = removedList.Find(x => x.Author.Id == id); removedList.Remove(authorInBookRemove); } _authorInBookRepository.Delete(removedList); } if (authorInBookReposytory.Count < authorId.Count) { Book bookAdd = _bookRepository.Get(bookView.Id); var addList = new List <AuthorInBook>(); foreach (AuthorInBook idAuthorInBook in authorInBookReposytory) { int id = authorId.Find(x => x == idAuthorInBook.Author.Id); authorId.Remove(id); } foreach (int id in authorId) { var author = _authorRepository.Get(id); addList.Add(new AuthorInBook() { Book = bookAdd, Author = author }); } _authorInBookRepository.Create(addList); } }
private void InitAuthorInBook() { var ainb1 = new AuthorInBook { Author = context.Authors.Local.ElementAtOrDefault(0), Book = context.Books.Local.ElementAtOrDefault(0) }; var ainb2 = new AuthorInBook { Author = context.Authors.Local.ElementAtOrDefault(3), Book = context.Books.Local.ElementAtOrDefault(2) }; var ainb3 = new AuthorInBook { Author = context.Authors.Local.ElementAtOrDefault(1), Book = context.Books.Local.ElementAtOrDefault(1) }; context.AuthorsInBooks.Add(ainb1); context.AuthorsInBooks.Add(ainb2); context.AuthorsInBooks.Add(ainb3); }
public void Update(AuthorInBook ainb) { AuthorInBook current = context.AuthorsInBooks.Find(ainb.Id); context.Entry(current).CurrentValues.SetValues(ainb); }
public void Delete(int ainbId) { AuthorInBook ainb = Get(ainbId); context.AuthorsInBooks.Remove(ainb); }
public int Create(AuthorInBook ainb) { context.AuthorsInBooks.Add(ainb); context.SaveChanges(); return(ainb.Id); }