Beispiel #1
0
 public IList<WebTVSearchResult> SearchResultsByRange(string text, int start, int end, WebTVSearchResultType? type = null)
 {
     return Search(text, type).TakeRange(start, end).ToList();
 }
Beispiel #2
0
        public IList<WebTVSearchResult> Search(string text, WebTVSearchResultType? type = null)
        {
            if (String.IsNullOrWhiteSpace(text))
            {
                return new List<WebTVSearchResult>();
            }

            text = text.ToLower();
            IEnumerable<WebTVSearchResult> result = new List<WebTVSearchResult>();

            if (type == null || type == WebTVSearchResultType.TVChannel || type == WebTVSearchResultType.RadioChannel)
            {
                result = result.Concat(Channel.ListAll()
                    .Where(channel => channel.DisplayName.Contains(text, StringComparison.CurrentCultureIgnoreCase))
                    .Select(channel => new WebTVSearchResult()
                    {
                        Id = channel.IdChannel.ToString(),
                        Score = 50 + (int)Math.Round((decimal)text.Length / channel.DisplayName.Length * 50),
                        Title = channel.DisplayName,
                        Type = channel.IsTv ? WebTVSearchResultType.TVChannel : WebTVSearchResultType.RadioChannel
                    })
                    .Where(r => type == null || r.Type == type));
            }

            if (type == null || type == WebTVSearchResultType.TVGroup)
            {
                result = result.Concat(ChannelGroup.ListAll()
                    .Where(group => group.GroupName.Contains(text, StringComparison.CurrentCultureIgnoreCase))
                    .Select(group => new WebTVSearchResult()
                    {
                        Id = group.IdGroup.ToString(),
                        Score = 50 + (int)Math.Round((decimal)text.Length / group.GroupName.Length * 50),
                        Title = group.GroupName,
                        Type = WebTVSearchResultType.TVGroup,
                    }));
            }

            if (type == null || type == WebTVSearchResultType.RadioGroup)
            {
                result = result.Concat(RadioChannelGroup.ListAll()
                    .Where(group => group.GroupName.Contains(text, StringComparison.CurrentCultureIgnoreCase))
                    .Select(group => new WebTVSearchResult()
                    {
                        Id = group.IdGroup.ToString(),
                        Score = 50 + (int)Math.Round((decimal)text.Length / group.GroupName.Length * 50),
                        Title = group.GroupName,
                        Type = WebTVSearchResultType.RadioGroup
                    }));
            }

            if (type == null || type == WebTVSearchResultType.Recording)
            {
                result = result.Concat(Recording.ListAll()
                    .Where(rec => rec.Title.Contains(text, StringComparison.CurrentCultureIgnoreCase))
                    .Select(rec => new WebTVSearchResult()
                    {
                        Id = rec.IdRecording.ToString(),
                        Score = 50 + (int)Math.Round((decimal)text.Length / rec.Title.Length * 50),
                        Title = rec.Title,
                        Type = WebTVSearchResultType.Recording,
                        StartTime = rec.StartTime,
                        EndTime = rec.EndTime,
                        ChannelName = GetChannelDisplayName(rec.IdChannel)
                    }));
            }

            if (type == null || type == WebTVSearchResultType.Schedule)
            {
                result = result.Concat(Schedule.ListAll()
                    .Where(schedule => schedule.ProgramName.Contains(text, StringComparison.CurrentCultureIgnoreCase))
                    .Select(schedule => new WebTVSearchResult()
                    {
                        Id = schedule.IdSchedule.ToString(),
                        Score = 50 + (int)Math.Round((decimal)text.Length / schedule.ProgramName.Length * 50),
                        Title = schedule.ProgramName,
                        Type = WebTVSearchResultType.Schedule,
                        ChannelName = GetChannelDisplayName(schedule.IdChannel)
                    }));
            }

            if (type == null || type == WebTVSearchResultType.Program)
            {
                result = result.Concat(_tvBusiness.SearchPrograms("%" + text + "%")
                    .Select(program => new WebTVSearchResult()
                    {
                        Id = program.IdProgram.ToString(),
                        Score = (int)Math.Round(((decimal)text.Length / program.Title.Length * 50) - Math.Abs((decimal)(program.StartTime - DateTime.Now).TotalDays)),
                        Title = program.Title,
                        Type = WebTVSearchResultType.Program,
                        StartTime = program.StartTime,
                        EndTime = program.EndTime,
                        ChannelName = GetChannelDisplayName(program.IdChannel)
                    }));
            }

            return result.OrderByDescending(x => x.Score).ToList();
        }