Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using IMDB_Data.DataContext;
- using IMDB_Web.Areas.StandardUser.Models;
- using IMDB_Web.Areas.StandardUser.Repository;
- using IMDB_Web.HELPER;
- using Microsoft.AspNetCore.Hosting.Internal;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.EntityFrameworkCore;
- namespace IMDB_Web.Areas.StandardUser.Controllers
- {
- public class TestController : Controller
- {
- private readonly MyAppDbContext _context;
- private readonly IMovies _movies;
- public const int RecordsPerPage = 10;
- public TestController(MyAppDbContext context,
- IMovies movies)
- {
- _context = context;
- _movies = movies;
- ViewBag.RecordsPerPage = RecordsPerPage;
- }
- public IActionResult Index(int? pageNum)
- {
- pageNum = pageNum ?? 0;
- ViewBag.IsEndOfRecords = false;
- if (Request.IsAjaxRequest())
- {
- var moviesShows = GetRecordsForPage(pageNum.Value);
- ViewBag.IsEndOfRecords =
- (moviesShows.Any()) && ((pageNum.Value * RecordsPerPage) >= moviesShows.Last().Key);
- return PartialView("_moviesShowsRow", moviesShows);
- }
- else
- {
- LoadAllToSession();
- ViewBag.moviesShows = GetRecordsForPage(pageNum.Value);
- return View("IndexTest");
- }
- return View();
- }
- public void LoadAllToSession()
- {
- var movies = _context.Movies.Where(q => q.IsShow == false).Select(x => new MoviesShowsListViewModel.Row
- {
- MovieId = x.MovieId,
- Title = x.Title,
- Duration = x.Duration,
- ReleaseDate = x.ReleaseDate.ToShortDateString(),
- Description = x.Description,
- Image = x.CoverImage,
- AvgRating = _context.MoviesRatingses
- .Include(a => a.Rating)
- .Where(a => a.MovieId == x.MovieId)
- .Average(a => a.Rating.Score)
- }).OrderByDescending(a => a.AvgRating).ToList();
- int mIndex = 1;
- HttpContext.Session.Set("MoviesShows", movies.ToDictionary(x => mIndex++, x => x));
- ViewBag.TotalNumberMoviesShows = movies.Count();
- }
- public Dictionary<int, MoviesShowsListViewModel> GetRecordsForPage(int pageNum)
- {
- var model = HttpContext.Session.Get<MoviesShowsListViewModel>
- ("MoviesShows").Rows.ToList();
- /*
- Dictionary<int, MoviesShowsListViewModel> movies
- = model as Dictionary<int, MoviesShowsListViewModel>);
- */
- int from = (pageNum * RecordsPerPage);
- int to = from + RecordsPerPage;
- return movies
- .Where(x => x.Key > from && x.Key <= to)
- .OrderBy(x => x.Key)
- .ToDictionary(x => x.Key, x => x.Value);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement