public TorrentItem GetSingleTorrent(int torrentID, int?userId) { using (var db = new DbTorronto()) { var item = db.Torrent .LoadWith(t => t.Movie) .FirstOrDefault(torrent => torrent.ID == torrentID && Sql2.IsNullOrFalse(torrent.Movie.IsCopyrighted)); var subscription = db.TorrentUser .FirstOrDefault(tu => tu.TorrentID == torrentID && tu.UserID == userId); return(new TorrentItem { Self = item, IsSubscribed = subscription?.IsSubscribed == true, IsRss = subscription?.AddedRss != null }); } }
public IEnumerable <RssItem> GetPersonalFeed(string userHash) { using (var db = new DbTorronto()) { var user = db.User .FirstOrDefault(u => Sql.Like(Sql2.MD5(Sql.ConvertTo <string> .From(u.Identifier)), userHash)); if (user == null) { return(Enumerable.Empty <RssItem>()); } var filter = db.TorrentUser .Where(tu => tu.UserID == user.ID && tu.AddedRss != null && Sql2.IsNullOrFalse(tu.Torrent.Movie.IsCopyrighted)); var orderedByAdded = filter .OrderByDescending(x => x.AddedRss) .Select(x => new RssItem { Title = TorrentTitle(x.Torrent.Movie.Title, x.Torrent.Title), Link = TorrentUrl(x.Torrent.InfoHash), Description = x.Torrent.Movie.Description, PubDate = x.AddedRss.GetValueOrDefault() }) .Take(20) .ToList(); var orderedByUpdated = filter .OrderByDescending(x => x.Torrent.Updated) .Select(x => new RssItem { Title = TorrentTitle(x.Torrent.Movie.Title, x.Torrent.Title), Link = TorrentUrl(x.Torrent.InfoHash), Description = x.Torrent.Movie.Description, PubDate = x.Torrent.Updated }) .Take(20) .ToList(); var items = orderedByAdded .Union(orderedByUpdated) .GroupBy(x => x.Link) .Select(g => g.OrderByDescending(x => x.PubDate).First()) .OrderByDescending(x => x.PubDate) .Take(20) .ToList(); return(items); } }
public MoviePagination GetMovies(int?userId, MovieSearchParams search, PaginationParams pageParams) { using (var db = new DbTorronto()) { var filter = db.Movie .NoCopyrighted() .SelectMany(m => db.MovieUser.Where(x => x.MovieID == m.ID && x.UserID == userId).DefaultIfEmpty(), (movie, movieUser) => new { M = movie, MuWaitList = movieUser.IsWaitlist, MuUserWatched = movieUser.IsWatched, MuDontWant = movieUser.IsDontWant, WlDate = movieUser.Created, MuMark = movieUser.Mark, }); if (!string.IsNullOrEmpty(search.Search)) { if (_isSphinxEnabled) { var movieIds = _searchService.SearchMovieIds(search); filter = filter.Where(x => movieIds.Contains(x.M.ID)); } else { filter = filter.Where(x => x.M.Title.Contains(search.Search) || x.M.OriginalTitle.Contains(search.Search)); } } if (search.WaitList) { filter = filter.Where(x => x.MuWaitList); } if (search.KinopoiskID > 0) { filter = filter.Where(x => x.M.KinopoiskID == search.KinopoiskID); } if (search.SystemList) { var candidates = from mu in db.MovieUser join mr in db.MovieRecommendation on mu.MovieID equals mr.MovieID from omu in db.MovieUser.Where(mm => mm.MovieID == mr.OtherMovieID && mm.UserID == userId).DefaultIfEmpty() where mu.UserID == userId && mu.Mark == 10 && Sql2.IsNullOrFalse(omu.IsDontWant) && Sql2.IsNullOrFalse(omu.IsWaitlist) && Sql2.IsNullOrFalse(omu.IsWatched) orderby mr.Position select(int?) mr.OtherMovieID; var rmovies = candidates .Take(50) .ToArray(); if (rmovies.Length > 0) { filter = filter.Where(x => rmovies.Contains(x.M.ID)); } else { filter = filter.Where(x => (x.M.Status == MovieStatus.ComingSoon || x.M.Status == MovieStatus.RecentPremiere) && Sql2.IsNullOrFalse(x.MuUserWatched) && Sql2.IsNullOrFalse(x.MuWaitList) && Sql2.IsNullOrFalse(x.MuDontWant) ); } } if (search.MovieStatus != MovieStatus.Unknown) { filter = filter.Where(x => x.M.Status == search.MovieStatus); } List <Person> actors = null; if (!string.IsNullOrEmpty(search.Actors)) { var actorIds = search.Actors .Split(',') .Select(x => Convert.ToInt32(x)) .Cast <int?>() .ToArray(); actors = db.Person .Where(p => actorIds.Contains(p.ID)) .ToList(); var movieIds = db.MoviePerson .Where(mp => actorIds.Contains(mp.PersonID)) .Select(mp => mp.MovieID) .Cast <int?>() .ToArray(); filter = filter.Where(x => movieIds.Contains(x.M.ID)); } switch (pageParams.Order) { case "wldate": filter = filter.OrderByDescending(x => x.WlDate); break; case "rkp": filter = filter.OrderByDescending(f => f.M.RatingKinopoisk); break; case "rimdb": filter = filter.OrderByDescending(f => f.M.RatingImdb); break; case "ruser": filter = filter .Where(x => x.MuMark != null) .OrderByDescending(f => f.MuMark) .ThenByDescending(f => f.M.RatingKinopoisk); break; case "quality": filter = filter .OrderByDescending(f => f.M.BestVideoQuality) .ThenByDescending(f => f.M.RatingKinopoisk); break; case "added": filter = filter .OrderByDescending(f => f.M.Created) .ThenByDescending(f => f.M.ID); break; case "topweek": // ReSharper disable ConditionIsAlwaysTrueOrFalse filter = filter .Where(x => x.M.MovieTopWeek.TorrentCount != null) .Where(x => Sql2.IsNullOrFalse(x.MuUserWatched) && Sql2.IsNullOrFalse(x.MuWaitList) && Sql2.IsNullOrFalse(x.MuDontWant)) .OrderByDescending(f => f.M.MovieTopWeek.TorrentCount); // ReSharper restore ConditionIsAlwaysTrueOrFalse break; default: filter = filter.OrderBy(t => t.M.Title); break; } var count = pageParams.NoCount ? 0 : filter.Count(); var movies = filter .Skip(pageParams.SkipCount) .Take(pageParams.PageSize) .Select(t => new MovieItem { Self = new Movie { ID = t.M.ID, Title = t.M.Title, OriginalTitle = t.M.OriginalTitle, ImdbID = t.M.ImdbID, KinopoiskID = t.M.KinopoiskID, RatingImdb = t.M.RatingImdb, RatingKinopoisk = t.M.RatingKinopoisk, ReleaseDate = t.M.ReleaseDate, Status = t.M.Status, BestVideoQuality = t.M.BestVideoQuality }, InWaitList = t.MuWaitList, Mark = t.MuMark, IsWatched = t.MuUserWatched, IsDontWant = t.MuDontWant }) .ToList(); return(new MoviePagination(movies) { PageSize = pageParams.PageSize, TotalItems = count, Actors = actors }); } }