public Task <List <PopularMoviesViewModel> > GetWatchHistoryUser(string email, int nAmount = 50)
        {
            return(Task.Factory.StartNew(() =>
            {
                using (var context = FLETNIXContext.ContextFactory())
                {
                    var watchHistoryUser = (from w in context.Watchhistory
                                            where w.CustomerMailAddress == email
                                            from review in context.MovieReview
                                            .Where(r => r.MovieId == w.MovieId).Where(r => r.CustomerMailAddress == email).DefaultIfEmpty()
                                            join m in context.Movie on w.MovieId equals m.MovieId
                                            group m by new { m, w, review }
                                            into g
                                            select new PopularMoviesViewModel
                    {
                        Movie = g.Key.m,
                        WatchDate = g.Key.w.WatchDate,
                        Review = g.Key.review
                    }).AsNoTracking()
                                           .Take(nAmount).OrderByDescending(p => p.WatchDate).ToList();

                    /* _context.Watchhistory.Where(h => h.CustomerMailAddress == User.Identity.Name)
                     * .Include(m => m.MovieId).ToList();*/
                    //context.Database.CloseConnection();
                    return watchHistoryUser;
                }
            }));
        }
        public Task <List <PopularMoviesViewModel> > GetLatestMoviesAdded()
        {
            return(Task.Factory.StartNew(() =>
            {
                using (var context = FLETNIXContext.ContextFactory())
                {
                    var latestmovies = (from m in context.Movie select new PopularMoviesViewModel {
                        Movie = m
                    }).OrderByDescending(m => m.Movie.PublicationYear).Take(50)
                                       .AsNoTracking().ToList();

                    return latestmovies;
                }
            }));
        }
        public Task <List <PopularMoviesViewModel> > GetMostPopularMoviesOfLastNDays(int nDays, int nAmount = 50)
        {
            return(Task.Factory.StartNew(() =>
            {
                if (nDays > 0 && nDays < 30)
                {
                    using (var context = FLETNIXContext.ContextFactory())
                    {
                        /*var MostPopularMoviesOfLastNDays = (from w in context.Watchhistory
                         *      where w.WatchDate >= Convert.ToDateTime(DateTime.Now).AddDays(-nDays)
                         *      join m in context.Movie on w.MovieId equals m.MovieId
                         *      group m by new {m}
                         *      into g
                         *      orderby g.Count() descending
                         *      select new PopularMoviesViewModel
                         *      {
                         *          Movie = g.Key.m,
                         *          TimesViewed = g.Count()
                         *      }).Take(nAmount).AsNoTracking()
                         * .ToList();
                         * //context.Database.CloseConnection();
                         * return MostPopularMoviesOfLastNDays;*/

                        var MostPopularMoviesOfLastNDays = (context.Watchhistory.Join(context.Movie, w => w.MovieId, m => m.MovieId, (w, m) => new { w, m })
                                                            .Where(t => t.w.WatchDate >= Convert.ToDateTime(DateTime.Now).AddDays(-nDays))
                                                            .GroupBy(t => new { t.m }, t => t.m)
                                                            .OrderByDescending(g => g.Count())
                                                            .Select(g => new PopularMoviesViewModel
                        {
                            Movie = g.Key.m,
                            TimesViewed = g.Count()
                        })).Take(10).ToList();

                        return MostPopularMoviesOfLastNDays;
                    }
                }
                else
                {
                    return new List <PopularMoviesViewModel>();
                }
            }));
        }
        public Task <List <PopularMoviesViewModel> > GetMostPopularMoviesOfAllTime(int nAmount = 50)
        {
            return(Task.Factory.StartNew(() =>
            {
                using (var context = FLETNIXContext.ContextFactory())
                {
                    var MostPopularOfAllTime = (
                        from w in context.Watchhistory
                        join m in context.Movie on w.MovieId equals m.MovieId
                        group m by new { m }
                        into g
                        orderby g.Count() descending
                        select new PopularMoviesViewModel
                    {
                        Movie = g.Key.m,
                        TimesViewed = g.Count()
                    }).AsNoTracking().Take(nAmount).ToList();

                    //context.Database.CloseConnection();
                    return MostPopularOfAllTime;
                }
            }));
        }
 public FletnixRepository(FLETNIXContext context, ILogger <FLETNIXContext> logger, ICache cache)
 {
     _cache   = cache;
     _context = context;
     _logger  = logger;
 }