Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using IMDB_Data.DataContext;
  6. using IMDB_Web.Areas.StandardUser.Models;
  7. using IMDB_Web.Areas.StandardUser.Repository;
  8. using IMDB_Web.HELPER;
  9. using Microsoft.AspNetCore.Hosting.Internal;
  10. using Microsoft.AspNetCore.Http;
  11. using Microsoft.AspNetCore.Mvc;
  12. using Microsoft.EntityFrameworkCore;
  13.  
  14. namespace IMDB_Web.Areas.StandardUser.Controllers
  15. {
  16.     public class TestController : Controller
  17.     {
  18.         private readonly MyAppDbContext _context;
  19.         private readonly IMovies _movies;
  20.         public const int RecordsPerPage = 10;
  21.  
  22.         public TestController(MyAppDbContext context,
  23.             IMovies movies)
  24.         {
  25.             _context = context;
  26.             _movies = movies;
  27.             ViewBag.RecordsPerPage = RecordsPerPage;
  28.         }
  29.  
  30.  
  31.         public IActionResult Index(int? pageNum)
  32.         {
  33.             pageNum = pageNum ?? 0;
  34.             ViewBag.IsEndOfRecords = false;
  35.             if (Request.IsAjaxRequest())
  36.             {
  37.                 var moviesShows = GetRecordsForPage(pageNum.Value);
  38.                 ViewBag.IsEndOfRecords =
  39.                     (moviesShows.Any()) && ((pageNum.Value * RecordsPerPage) >= moviesShows.Last().Key);
  40.                 return PartialView("_moviesShowsRow", moviesShows);
  41.             }
  42.             else
  43.             {
  44.             LoadAllToSession();
  45.                 ViewBag.moviesShows = GetRecordsForPage(pageNum.Value);
  46.                 return View("IndexTest");
  47.             }
  48.             return View();
  49.         }
  50.         public void LoadAllToSession()
  51.         {
  52.             var movies = _context.Movies.Where(q => q.IsShow == false).Select(x => new MoviesShowsListViewModel.Row
  53.             {
  54.                 MovieId = x.MovieId,
  55.                 Title = x.Title,
  56.                 Duration = x.Duration,
  57.                 ReleaseDate = x.ReleaseDate.ToShortDateString(),
  58.                 Description = x.Description,
  59.                 Image = x.CoverImage,
  60.                 AvgRating = _context.MoviesRatingses
  61.                     .Include(a => a.Rating)
  62.                     .Where(a => a.MovieId == x.MovieId)
  63.                     .Average(a => a.Rating.Score)
  64.             }).OrderByDescending(a => a.AvgRating).ToList();
  65.  
  66.             int mIndex = 1;
  67.             HttpContext.Session.Set("MoviesShows", movies.ToDictionary(x => mIndex++, x => x));
  68.            ViewBag.TotalNumberMoviesShows = movies.Count();
  69.         }
  70.         public Dictionary<int, MoviesShowsListViewModel> GetRecordsForPage(int pageNum)
  71.         {
  72.             var model = HttpContext.Session.Get<MoviesShowsListViewModel>
  73.                 ("MoviesShows").Rows.ToList();
  74.             /*
  75.             Dictionary<int, MoviesShowsListViewModel> movies
  76.                 = model as Dictionary<int, MoviesShowsListViewModel>);
  77.                 */
  78.  
  79.             int from = (pageNum * RecordsPerPage);
  80.             int to = from + RecordsPerPage;
  81.             return movies
  82.                 .Where(x => x.Key > from && x.Key <= to)
  83.                 .OrderBy(x => x.Key)
  84.                 .ToDictionary(x => x.Key, x => x.Value);
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement