public NovelDetail GetNovel(NovelCriteria criteria) { using (var uow = UnitOfWorkFactory.Create <NovelContext>()) { var service = new NovelService(uow); var detail = service.Get(criteria); // author var qTag = service.View <Tag>().All(); var qConnector = service.View <Connector>().All(); var qAuthor = service.View <Author>().All(); var authors = qConnector.Where(w => w.ConnectorType == R.ConnectorType.NOVEL_AUTHOR) .Where(w => w.SourceID == detail.ID).Select(s => s.TargetID).ToList(); detail.Authors = qAuthor.Where(w => authors.Contains(w.ID)).ToList(); // chapter var qChapter = service.View <Chapter>().All(); qChapter = qChapter.Where(w => w.NovelID == detail.ID); detail.Chapters = qChapter.ToList(); // glossary detail.Glossaries = service.View <Glossary>().Where(w => w.SourceTable == R.SourceTable.NOVEL && w.SourceID == detail.ID).ToList(); // summarize detail.Summarize = service.View <Summarize>().Where(w => w.SourceTable == R.SourceTable.NOVEL && w.SourceID == detail.ID).SingleOrDefault() ?? new Summarize(); return(detail); } }
public PagedList <NovelGrid> Search(SearchModel <NovelCriteria> searchModel) { using (var uow = UnitOfWorkFactory.Create <NovelContext>()) { var service = new NovelService(uow); var results = service.Search(searchModel); var novelIDs = results.Data.Select(s => s.ID).ToList(); // latest chapters var chapters = service.View <Chapter>().Where(w => novelIDs.Contains(w.NovelID)).ToList(); // connectors var connectors = service.View <Connector>().Where(w => w.IsDeleted == false).Where(w => novelIDs.Contains(w.SourceID)).ToList(); // tags var tagTypes = new[] { R.TagType.CATEGORY, R.TagType.GENRE }; var tags = service.View <Tag>().Where(w => w.IsDeleted == false && tagTypes.Contains(w.TagType)).ToList(); // groups var groupIDs = connectors.Where(w => w.ConnectorType == R.ConnectorType.NOVEL_GROUP).Select(s => s.TargetID).ToList(); var groups = service.View <Group>().Where(w => groupIDs.Contains(w.ID)).ToList(); // user lists var userLists = service.View <UserList>().Where(w => w.IsDeleted == false && w.UserID == searchModel.Criteria.ByUserID).ToList(); // vote var userVotedSeriesIDs = service.View <UserVote>().Where(w => w.SourceTable == R.SourceTable.NOVEL && novelIDs.Contains(w.SourceID) && w.UserID == searchModel.Criteria.ByUserID) .Select(s => new { SeriesID = s.SourceID, Vote = s.Vote }).ToList(); // rate var userQualityRatedSeriesIDs = service.View <UserRate>().Where(w => w.SourceTable == R.SourceTable.NOVEL && novelIDs.Contains(w.SourceID) && w.UserID == searchModel.Criteria.ByUserID) .Select(s => new { SeriesID = s.SourceID, Rate = s.Rate }).ToList(); var connectorTypes = new[] { R.ConnectorType.NOVEL_TAGCATEGORY, R.ConnectorType.NOVEL_TAGGENRE }; // convert to PagedList<TranslationSceneGrid> results.Data = results.Data.Select(s => { s.Chapters = chapters.Where(w => w.NovelID == s.ID).ToList(); s.Groups = connectors.Where(w => w.ConnectorType == R.ConnectorType.NOVEL_GROUP && w.SourceID == s.ID) .Join(groups, c => c.TargetID, g => g.ID, (c, g) => g).ToList(); s.UserLists = userLists; s.Connectors = connectors.Where(w => w.SourceID == s.ID).ToList(); s.Tags = tags.Where(w => connectors.Any(w2 => connectorTypes.Contains(w2.ConnectorType) && w2.SourceID == s.ID && w2.TargetID == w.ID)).ToList(); s.Voted = userVotedSeriesIDs.Where(w => w.SeriesID == s.ID).Select(s2 => s2.Vote).SingleOrDefault(); s.QualityRated = userQualityRatedSeriesIDs.Where(w => w.SeriesID == s.ID).Select(s2 => s2.Rate).SingleOrDefault(); return(s); }).ToList(); return(results); } }