public void CreateBookItem(BookItemViewModel bookView, ApplicationDbContext db, HttpPostedFileBase cover, HttpPostedFileBase tableOfContents) { BookItem bookItem = new BookItem() { Title = bookView.Title, ISBN = bookView.ISBN, Descryption = bookView.Descryption, Publisher = bookView.Publisher, ReleaseDate = bookView.ReleaseDate, Category = db.Categories.Find(bookView.CategoryID), Number = bookView.Number, AddDate = DateTime.Now }; db.Set <BookItem>().AddOrUpdate(bookItem); db.SaveChanges(); var book = db.BookItems.FirstOrDefault(x => x.ISBN == bookItem.ISBN); db.Categories.Find(bookView.CategoryID).BookItem.Add(book); foreach (var authorId in bookView.SelectedAuthors) { var author = db.Authors.Single(a => a.AuthorID == authorId); var ag = new AuthorGroup() { Author = author, BookItem = book }; db.Set <AuthorGroup>().AddOrUpdate(ag); } db.SaveChanges(); foreach (var labelId in bookView.SelectedLabels) { var label = db.Labels.Single(l => l.LabelID == labelId); var labelgroup = new LabelGroup() { Label = label, BookItem = book, }; db.Set <LabelGroup>().AddOrUpdate(labelgroup); } db.SaveChanges(); if (cover != null) { AddAttachments_displayable(db, bookItem.BookItemID, cover, FileType.Cover); } if (tableOfContents != null) { AddAttachments_displayable(db, bookItem.BookItemID, tableOfContents, FileType.TableOfContents); } foreach (var file in bookView.FileList) { AddAttachments(db, bookItem.BookItemID, file, FileType.Attachment); } }
private void SeedAuthorGroups(ApplicationDbContext context) { //dodanie do AuthorGroup for (int i = 1; i < 4; i++) { var ag = new AuthorGroup() { AuthorGroupID = i, AuthorID = i, BookItemID = i }; context.Set <AuthorGroup>().AddOrUpdate(ag); } context.SaveChanges(); }
public BookItem EditBookItem(BookEditViewModel bookView, ApplicationDbContext db, HttpPostedFileBase cover, HttpPostedFileBase tableOfContents) { var existingBook = db.BookItems.Single(x => x.BookItemID == bookView.BookItemViewModel.ID); existingBook.Title = bookView.BookItemViewModel.Title; existingBook.ISBN = bookView.BookItemViewModel.ISBN; existingBook.Descryption = bookView.BookItemViewModel.Descryption; existingBook.Publisher = bookView.BookItemViewModel.Publisher; existingBook.Category = db.Categories.Find(bookView.BookItemViewModel.CategoryID); existingBook.Number = bookView.BookItemViewModel.Number; //Update AuthorGroup Table List <int> oldAuthorGroupID = db.Authors.Where(a => a.AuthorGroups.Any(g => g.BookItem.BookItemID == existingBook.BookItemID)).Select(x => x.AuthorID).ToList(); var authorDiffAdd = bookView.BookItemViewModel.SelectedAuthors.Except(oldAuthorGroupID); var authorDiffDel = oldAuthorGroupID.Except(bookView.BookItemViewModel.SelectedAuthors); foreach (var arg in authorDiffDel) { AuthorGroup delete = db.AuthorGroups.Single(x => x.Author.AuthorID == arg && x.BookItem.BookItemID == bookView.BookItemViewModel.ID); db.AuthorGroups.Remove(delete); } db.SaveChanges(); foreach (var arg in authorDiffAdd) { AuthorGroup add = new AuthorGroup() { Author = db.Authors.Find(arg), BookItem = existingBook }; db.AuthorGroups.Add(add); } db.SaveChanges(); //Update LabelGroupTable List <int> oldLabelGroupID = db.Labels.Where(a => a.LabelGroups.Any(g => g.BookItem.BookItemID == existingBook.BookItemID)).Select(x => x.LabelID).ToList(); var labelDiffAdd = bookView.BookItemViewModel.SelectedLabels.Except(oldLabelGroupID); var labelDiffDel = oldLabelGroupID.Except(bookView.BookItemViewModel.SelectedLabels); foreach (var arg in labelDiffDel) { LabelGroup delete = db.LabelGroups.Single(x => x.Label.LabelID == arg && x.BookItem.BookItemID == bookView.BookItemViewModel.ID); db.LabelGroups.Remove(delete); } db.SaveChanges(); foreach (var arg in labelDiffAdd) { LabelGroup add = new LabelGroup() { Label = db.Labels.Find(arg), BookItem = existingBook }; db.LabelGroups.Add(add); } db.SaveChanges(); if (cover != null) { DeleteCover_Table(db, bookView.BookItemViewModel.ID, FileType.Cover); AddAttachments_displayable(db, bookView.BookItemViewModel.ID, cover, FileType.Cover); } if (tableOfContents != null) { DeleteCover_Table(db, bookView.BookItemViewModel.ID, FileType.TableOfContents); AddAttachments_displayable(db, bookView.BookItemViewModel.ID, tableOfContents, FileType.TableOfContents); } if (bookView.Cover == null && cover == null) { DeleteCover_Table(db, bookView.BookItemViewModel.ID, FileType.Cover); } if (bookView.Table == null && tableOfContents == null) { DeleteCover_Table(db, bookView.BookItemViewModel.ID, FileType.TableOfContents); } DeleteOldFile(db, bookView.BookItemViewModel.ID, bookView.OldFiles); if (bookView.BookItemViewModel.FileList != null) { foreach (var file in bookView.BookItemViewModel.FileList) { AddAttachments(db, bookView.BookItemViewModel.ID, file, FileType.Attachment); } } return(existingBook); }