Example #1
0
        public DbBookModel GetBook(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();
                var bookChapters = db.Chapters.Where(c => c.BookId == bookId).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;

                    //List<BookSection> chapterSections = dbChapter.Sections.OrderBy(s => s.SectionOrder).ToList();
                    var chapterSections = db.Sections.Where(s => s.BookId == bookId && s.ChapterId == dbChapter.Id).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();
                        var subSections = db.SubSections.Where(ss => ss.BookId == bookId &&
                                                               ss.ChapterId == dbSection.ChapterId &&
                                                               ss.SectionId == dbSection.Id)
                                          .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);
        }
Example #2
0
        public string UpdateChapter(ChapterModel chapterModel)
        {
            string success = "";

            using (var db = new BookDbContext())
            {
                var chapter = db.Chapters.Where(c => c.Id == chapterModel.Id).FirstOrDefault();
                if (chapter == null)
                {
                    success = "not found";
                }
                else
                {
                    chapter.ChapterTitle = chapterModel.ChapterTitle;
                    chapter.ChapterOrder = chapterModel.ChapterOrder;
                    chapter.Preface      = chapterModel.Preface;
                    db.SaveChanges();
                    success = "ok";
                }
            }
            return(success);
        }