Exemplo n.º 1
0
        /// <summary>
        /// Return a random video from the database matching specified conditions.
        /// </summary>
        /// <param name="data">The list of videos in which to search; generally Entities.Media.</param>
        /// <param name="settings">An object containing various search settings.</param>
        /// <param name="count">The quantity of results to return.</param>
        /// <param name="maintainCurrent">If specified, the currently selected video will be kept.</param>
        /// <returns>The randomly selected video, or null if no matches are found.</returns>
        public static List <Media> SelectVideo(IQueryable <Media> data, SearchSettings settings, int count, Media maintainCurrent)
        {
            if (settings == null)
            {
                settings = new SearchSettings();
            }
            if (settings.ExcludeVideos == null)
            {
                settings.ExcludeVideos = new List <Guid>();
            }

            // Exclude currently selected video so that it doesn't get randomly re-reselected.
            if (maintainCurrent != null)
            {
                settings.ExcludeVideos = new List <Guid>(settings.ExcludeVideos);
                settings.ExcludeVideos.Add(maintainCurrent.MediaId);
                count--;
            }

            var Query = (from v in data
                         where (settings.AllowDownloading || v.FileName != null) &&
                         (v.FileName != null || v.DownloadUrl != "") &&
                         (v.Length == null || ((v.EndPos != null ? v.EndPos.Value : v.Length.Value) - (v.StartPos != null ? v.StartPos.Value : 0)) <= 12 * 60) && // Don't get videos longer than 12 minutes
                         !settings.ExcludeVideos.Contains(v.MediaId)                                                                                              // Don't repeat same videos
                         select v);

            // Apply search filters.
            Query = SearchVideoAccess.ApplySearchFilters(Query, settings, null);

            // Return random result
            int          TotalFound = 0;
            List <Guid>  ResultId   = SelectRandomId(Query, count, out TotalFound);
            List <Media> Result     = new List <Media>();

            if (maintainCurrent != null)
            {
                Result.Add(maintainCurrent);
            }
            settings.TotalFound = TotalFound;
            if (TotalFound > 0 && ResultId.Count() > 0)
            {
                if (TotalFound <= count)
                {
                    Result.AddRange(Query.Take(count).ToList());
                }
                else
                {
                    Result.AddRange(data.Where(v => ResultId.Contains(v.MediaId)).ToList());
                }
            }
            return(Result);
        }