コード例 #1
0
ファイル: ShowService.cs プロジェクト: DanielWojnar/Animulu
 public async Task <IEnumerable <ShowDisplay> > GetRandomAsync(int skip, int take)
 {
     try
     {
         var rating = from r in _context.Reviews
                      group r by r.ShowId into g
                      select new { ShowId = g.Key, Count = (float)g.Count(), Sum = (float)g.Sum(s => s.Value) };
         var result = await(from s in _context.Shows
                            join r in rating on s.Id equals r.ShowId into ShowGroup
                            from sg in ShowGroup.DefaultIfEmpty()
                            select new ShowDisplay
         {
             Id          = s.Id,
             Title       = s.Title,
             Description = s.Description,
             CoverImg    = s.CoverImg,
             ReleaseDate = s.ReleaseDate,
             Score       = sg == null ? 0 : (sg.Sum / sg.Count)
         }).OrderBy(s => Guid.NewGuid()).Skip(skip).Take(take).AsNoTracking().ToListAsync();
         return(result);
     }
     catch
     {
         return(new List <ShowDisplay>());
     }
 }
コード例 #2
0
ファイル: ShowService.cs プロジェクト: DanielWojnar/Animulu
        public async Task <IEnumerable <ShowDisplay> > GetSearchAsync(int skip, int take, string quote, string tag, string order)
        {
            var rating = from r in _context.Reviews
                         group r by r.ShowId into g
                         select new { ShowId = g.Key, Score = ((float)g.Sum(s => s.Value) / (float)g.Count()) };
            var query = from s in _context.Shows
                        join r in rating on s.Id equals r.ShowId into ShowGroup
                        from sg in ShowGroup.DefaultIfEmpty()
                        orderby sg.Score descending
                        select new ShowDisplay
            {
                Id          = s.Id,
                Title       = s.Title,
                Description = s.Description,
                CoverImg    = s.CoverImg,
                ReleaseDate = s.ReleaseDate,
                Score       = sg == null ? 0 : sg.Score
            };

            if (quote != "" && quote != null)
            {
                query = query.Where(s => s.Title.Contains(quote));
            }
            if (tag != "" && tag != null)
            {
                var tags    = tag.Split(' ');
                var allTags = (from ta in _context.Tags
                               where tags.Any(t => ta.Name.Replace(" ", "-") == t)
                               select ta);
                var tagsQ = from tc in _context.TagConnections
                            join at in allTags on tc.TagId equals at.Id
                            group tc.ShowId by tc.ShowId into g
                            select new { ShowId = g.Key, Count = g.Count() };
                query = from s in query
                        join t in tagsQ on s.Id equals t.ShowId
                        where t.Count == tags.Count()
                        select s;
            }
            switch (order)
            {
            case "rdate":
                query = query.OrderByDescending(s => s.ReleaseDate);
                break;

            case "mviews":
                var mostv = (from v in _context.Views
                             group v.ShowId by v.ShowId into g
                             select new { ShowId = g.Key, Views = g.Count() });
                query = from s in query
                        join t in mostv on s.Id equals t.ShowId into ShowGroup
                        from sg in ShowGroup.DefaultIfEmpty()
                        orderby sg.Views descending
                        select s;
                break;

            case "popular":
                var trending = (from v in _context.Views
                                where v.ViewDate >= DateTime.Now.AddDays(-31)
                                group v.ShowId by v.ShowId into g
                                select new { ShowId = g.Key, Views = g.Count() });
                query = from s in query
                        join t in trending on s.Id equals t.ShowId into ShowGroup
                        from sg in ShowGroup.DefaultIfEmpty()
                        orderby sg.Views descending
                        select s;
                break;

            case "rating":
                break;

            case "alph":
                query = query.OrderBy(s => s.Title);
                break;

            default:
                var deford = (from v in _context.Views
                              where v.ViewDate >= DateTime.Now.AddDays(-31)
                              group v.ShowId by v.ShowId into g
                              select new { ShowId = g.Key, Views = g.Count() });
                query = from s in query
                        join t in deford on s.Id equals t.ShowId into ShowGroup
                        from sg in ShowGroup.DefaultIfEmpty()
                        orderby sg.Views descending
                        select s;
                break;
            }
            try
            {
                var result = await query.Skip(skip).Take(take).AsNoTracking().ToListAsync();

                return(result);
            }
            catch
            {
                var result = new List <ShowDisplay>();
                return(result);
            }
        }
コード例 #3
0
        public async Task <NavPage> GetSearchAsync(string quote, string tag, string order, int pageid)
        {
            int count = 0;
            var query = from s in _context.Shows
                        select s;

            if (quote != "" && quote != null)
            {
                query = query.Where(s => s.Title.Contains(quote));
            }
            if (tag != "" && tag != null)
            {
                var tags    = tag.Split(' ');
                var allTags = (from ta in _context.Tags
                               where tags.Any(t => ta.Name.Replace(" ", "-") == t)
                               select ta);
                var tagsQ = from tc in _context.TagConnections
                            join at in allTags on tc.TagId equals at.Id
                            group tc.ShowId by tc.ShowId into g
                            select new { ShowId = g.Key, Count = g.Count() };
                query = from s in query
                        join t in tagsQ on s.Id equals t.ShowId
                        where t.Count == tags.Count()
                        select s;
            }
            switch (order)
            {
            case "rdate":
                query.OrderByDescending(s => s.ReleaseDate);
                break;

            case "mviews":
                var mostv = (from v in _context.Views
                             group v.ShowId by v.ShowId into g
                             select new { ShowId = g.Key, Views = g.Count() });
                query = from s in query
                        join t in mostv on s.Id equals t.ShowId into ShowGroup
                        from sg in ShowGroup.DefaultIfEmpty()
                        orderby sg.Views descending
                        select s;
                break;

            case "popular":
                var trending = (from v in _context.Views
                                where v.ViewDate >= DateTime.Now.AddDays(-31)
                                group v.ShowId by v.ShowId into g
                                select new { ShowId = g.Key, Views = g.Count() });
                query = from s in query
                        join t in trending on s.Id equals t.ShowId into ShowGroup
                        from sg in ShowGroup.DefaultIfEmpty()
                        orderby sg.Views descending
                        select s;
                break;

            case "rating":
                break;

            case "alph":
                query.OrderByDescending(s => s.Title);
                break;

            default:
                var deford = (from v in _context.Views
                              where v.ViewDate >= DateTime.Now.AddDays(-31)
                              group v.ShowId by v.ShowId into g
                              select new { ShowId = g.Key, Views = g.Count() });
                query = from s in query
                        join t in deford on s.Id equals t.ShowId into ShowGroup
                        from sg in ShowGroup.DefaultIfEmpty()
                        orderby sg.Views descending
                        select s;
                break;
            }
            try
            {
                count = await query.AsNoTracking().CountAsync();
            }
            catch
            {
                count = 0;
            }
            return(setValues(pageid, count));
        }