public List <tNovel> GetNovels() { using (var db = new NovelDbContext()) { return(db.tNovels.ToList()); } }
public tNovel GetNovel(int id) { using (var db = new NovelDbContext()) { return(db.tNovels.Where(s => s.ID == id).FirstOrDefault()); } }
public List <tChapter> GetChaptersByNovelId(int novelId) { using (var db = new NovelDbContext()) { return(db.tChapters.Where(s => s.NovelID == novelId).ToList()); } }
public tChapter GetChapter(int id) { using (var db = new NovelDbContext()) { var chapter = db.tChapters.Where(s => s.ID == id).FirstOrDefault(); if (chapter == null) { return(null); } var novel = db.tNovels.FirstOrDefault(s => s.ID == chapter.NovelID); var source = db.tSources.FirstOrDefault(s => s.ID == novel.SourceID); if (string.IsNullOrEmpty(chapter.Body)) { // 从资源服务器获取小说内容 // 保存到数据库 var body = new HtmlHelper(new Uri(chapter.SourceUrl)).GetSingleInnerTextByXPath(source.ChapterBodyXpath); chapter.Body = body; db.SaveChanges(); } return(chapter); } }
public tSource GetSource(int id) { using (var db = new NovelDbContext()) { return(db.tSources.Where(s => s.ID == id).FirstOrDefault()); } }
public void AddNovel(tNovel novel) { using (var db = new NovelDbContext()) { db.tNovels.Add(novel); db.SaveChanges(); } }
public void AddSource(tSource source) { using (var db = new NovelDbContext()) { db.tSources.Add(source); db.SaveChanges(); } }
private static SqliteConnection CreateDatabaseAndGetConnection() { var connection = new SqliteConnection("Data Source=:memory:"); connection.Open(); var options = new DbContextOptionsBuilder <NovelDbContext>() .UseSqlite(connection) .Options; using var context = new NovelDbContext(options); context.GetService <IRelationalDatabaseCreator>().CreateTables(); return(connection); }
public Repository(NovelDbContext context) { this.context = context; this.models = context.Set <TEntity>(); }
public RepositoryFactory(NovelDbContext context) { this.context = context; }
/// <summary> /// 从小说服务器下载章节列表,保存到本地 /// </summary> /// <param name="novelId"></param> /// <returns></returns> public void DownloadChapters(int novelId) { var novel = new NovelManager().GetNovel(novelId); var chapterNodes = GetChaptersHtmlFromNovelSource(novelId); var chapters = chapterNodes.Select(s => new tChapter() { Body = null, ID = 0, NovelID = novelId, NextChapterID = null, PrevChapterID = null, SourceUrl = s.GetAttributeValue("href", "#").ToString(), Title = s.InnerText, Sort = 0 }).ToList(); int?nextChapterId = 0; int?prevChapterId = 0; using (var db = new NovelDbContext()) { db.tChapters.RemoveRange(db.tChapters.Where(s => s.NovelID == novelId)); int pagesize = 30; int pageindex = 0; while (pagesize * pageindex < chapters.Count()) { var items = chapters.Skip(pageindex * pagesize).Take(pagesize).ToList(); db.tChapters.AddRange(items); db.SaveChanges(); pageindex++; } for (int i = 0; i < chapters.Count(); i++) { if (i != 0) { prevChapterId = chapters.ElementAt(i - 1).ID; } else { prevChapterId = null; } if (i != chapters.Count() - 1) { nextChapterId = chapters.ElementAt(i + 1).ID; } else { nextChapterId = null; } chapters.ElementAt(i).NextChapterID = nextChapterId; chapters.ElementAt(i).PrevChapterID = prevChapterId; if (i % pagesize == pagesize - 1) { db.SaveChanges(); } } db.SaveChanges(); } }