public IList <BookSectionModel> Get(int chapterId) { var sections = new List <BookSectionModel>(); BookSectionModel section = null; using (var db = new BookDbContext()) { var dbSections = db.Sections.Where(s => s.ChapterId == chapterId).ToList(); foreach (BookSection dbSection in dbSections) { section = new BookSectionModel(); section.Id = dbSection.Id; section.SectionTitle = dbSection.SectionTitle; section.SectionOrder = dbSection.SectionOrder; section.SectionContents = dbSection.SectionContents; foreach (SubSection dbSubSection in dbSection.SubSections) { section.SubSections.Add(new SubSectionModel() { Id = dbSubSection.Id, SubSectionContents = dbSubSection.SubSectionContents, SubSectionTitle = dbSubSection.SubSectionTitle, SubSectionOrder = dbSubSection.SubSectionOrder }); } sections.Add(section); } } return(sections); }
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 int Post(BookSectionModel bookSectionModel) { int success = 0; using (var db = new BookDbContext()) { var bookSection = new BookSection() { ChapterId = bookSectionModel.ChapterId, SectionTitle = bookSectionModel.SectionTitle, SectionOrder = bookSectionModel.SectionOrder, SectionContents = bookSectionModel.SectionContents }; db.Sections.Add(bookSection); db.SaveChanges(); success = bookSection.Id; } return(success); }
public BookSectionModel Patch(int sectionId) { var sectionModel = new BookSectionModel(); using (var db = new BookDbContext()) { BookSection dbSection = db.Sections.Where(s => s.Id == sectionId).FirstOrDefault(); if (dbSection != null) { sectionModel.Id = dbSection.Id; sectionModel.ChapterOrder = dbSection.Chapter.ChapterOrder; sectionModel.ChapterTitle = dbSection.Chapter.ChapterTitle; sectionModel.SectionTitle = dbSection.SectionTitle; sectionModel.SectionOrder = dbSection.SectionOrder; sectionModel.SectionContents = dbSection.SectionContents; sectionModel.success = "ok"; } } return(sectionModel); }
public string Put(BookSectionModel bookSectionModel) { string success = ""; using (var db = new BookDbContext()) { var bookSection = db.Sections.Where(c => c.Id == bookSectionModel.Id).FirstOrDefault(); if (bookSection == null) { success = "not found"; } else { bookSection.SectionTitle = bookSectionModel.SectionTitle; bookSection.SectionOrder = bookSectionModel.SectionOrder; bookSection.SectionContents = bookSectionModel.SectionContents; db.SaveChanges(); success = "ok"; } } return(success); }