public string Put(DbBookModel bookModel) { string success = ""; try { using (var db = new BookDbContext()) { var dbBook = db.Books.Where(b => b.Id == bookModel.Id).FirstOrDefault(); if (dbBook == null) { success = "not found"; } else { dbBook.BookTitle = bookModel.BookTitle; dbBook.Author = bookModel.Author; dbBook.Introduction = bookModel.Introduction; dbBook.Preface = bookModel.Preface; db.SaveChanges(); success = "ok"; } } } catch (Exception ex) { success = Helpers.ErrorDetails(ex); } return(success); }
public DbBookModel Get(int bookId) { DbBookModel bookModel = new DbBookModel(); ChapterModel chapterModel = null; BookSectionModel sectionModel = null; using (var db = new BookDbContext()) { var dbBook = db.Books.Where(b => b.Id == bookId).FirstOrDefault(); bookModel.Id = dbBook.Id; bookModel.BookTitle = dbBook.BookTitle; bookModel.Introduction = dbBook.Introduction; bookModel.Preface = dbBook.Preface; var bookChapters = dbBook.Chapters.OrderBy(c => c.ChapterOrder).ToList(); foreach (BookChapter dbChapter in bookChapters) { chapterModel = new ChapterModel(); chapterModel.Id = dbChapter.Id; chapterModel.ChapterTitle = dbChapter.ChapterTitle; chapterModel.ChapterOrder = dbChapter.ChapterOrder; chapterModel.Preface = dbChapter.Preface; var chapterSections = dbChapter.Sections.OrderBy(s => s.SectionOrder).ToList(); foreach (BookSection dbSection in chapterSections) { sectionModel = new BookSectionModel(); sectionModel.Id = dbSection.Id; sectionModel.SectionTitle = dbSection.SectionTitle; sectionModel.SectionOrder = dbSection.SectionOrder; //sectionModel.SectionContents = dbSection.SectionContents; var subSections = dbSection.SubSections.OrderBy(ss => ss.SubSectionOrder).ToList(); foreach (SubSection dbSubSection in subSections) { sectionModel.SubSections.Add(new SubSectionModel() { Id = dbSubSection.Id, SubSectionContents = dbSubSection.SubSectionContents, SubSectionTitle = dbSubSection.SubSectionTitle, SubSectionOrder = dbSubSection.SubSectionOrder }); } chapterModel.Sections.Add(sectionModel); } bookModel.Chapters.Add(chapterModel); } bookModel.success = "ok"; } return(bookModel); }
public string Post(DbBookModel bookModel) { string success = ""; using (var db = new BookDbContext()) { var dbBook = new DataContext.Book(); dbBook.BookTitle = bookModel.BookTitle; dbBook.Author = bookModel.Author; dbBook.Introduction = bookModel.Introduction; dbBook.Preface = bookModel.Preface; db.Books.Add(dbBook); db.SaveChanges(); success = "ok"; } return(success); }