public async Task <MovieFilterServiceModel> FilteredMovies(MovieFilterServiceModel moviesModel) { MovieFilterServiceModel movieModel = new MovieFilterServiceModel(); if (moviesModel.MovieName != null) { movieModel = await SearchWithMovieName(moviesModel.MovieName, movieModel); if (movieModel.MovieCollection.Count == 0) { return(movieModel); } } if (moviesModel.Year != null) { movieModel = await SearchWithMovieYear(moviesModel.Year, movieModel); if (movieModel.MovieCollection.Count == 0) { return(movieModel); } } if (moviesModel.Genres.Any(x => x.IsSelected)) { movieModel = await SearchWithMovieGenres(moviesModel.Genres, movieModel); } return(movieModel); }
public async Task <IActionResult> Index(MovieFilterServiceModel movieFilter) { MovieFilterServiceModel movieFilterServiceModel = await this.movieService.FilteredMovies(movieFilter); List <GenreServiceModel> genres = await genreService.GetAllGenres(); if (User.Identity.IsAuthenticated) { User user = await userManager.GetUserAsync(HttpContext.User); IList <string> roles = await userManager.GetRolesAsync(user); string role = roles.SingleOrDefault(); if (role == null) { await this.userManager.AddToRoleAsync(user, GlobalConstants.RegularUser); role = GlobalConstants.RegularUser; } if (movieFilterServiceModel.MovieCollection != null) { foreach (MovieServiceModel movie in movieFilterServiceModel.MovieCollection) { MovieServiceModel movieModel = await this.movieService.FindMovie(movie.Id); movie.IsPurchased = this.movieService.IsPurchased(user.Id, movie.Id); movie.Price = movieModel.Prices.SingleOrDefault(x => x.Role.Name == role).MoviePrice; } } else { movieFilterServiceModel.MovieCollection = new List <MovieServiceModel>(); } foreach (GenreServiceModel genre in genres) { GenreServiceModel genreModel = new GenreServiceModel(); genreModel.Name = genre.Name; genreModel.Id = genre.Id; movieFilterServiceModel.Genres.Add(genreModel); } } return(View(movieFilterServiceModel)); }
public async Task <MovieFilterServiceModel> SearchWithMovieName(string name, MovieFilterServiceModel movieModel) { movieModel.MovieCollection = await this.Db.Movies .Include(p => p.Genres) .Where(d => d.Name.Contains(name)) .Select(g => new MovieServiceModel { Id = g.Id, Name = g.Name, Rating = g.Rating, PosterPath = g.PosterPath, VideoPath = g.VideoPath, TrailerPath = g.TrailerPath, Summary = g.Summary, Year = g.Year }) .ToListAsync(); return(movieModel); }
public async Task <MovieFilterServiceModel> SearchWithMovieYear(string year, MovieFilterServiceModel movieModel) { try { if (movieModel.MovieCollection.Count == 0) { movieModel.MovieCollection = await this.Db.Movies .Include(p => p.Genres) .Where(d => d.Year.Year == int.Parse(year)) .Select(g => new MovieServiceModel { Id = g.Id, Name = g.Name, Rating = g.Rating, PosterPath = g.PosterPath, VideoPath = g.VideoPath, TrailerPath = g.TrailerPath, Summary = g.Summary, Year = g.Year }) .ToListAsync(); } else { movieModel.MovieCollection = movieModel.MovieCollection .Where(d => d.Year.Year == int.Parse(year)) .ToList(); } } catch (Exception) { return(movieModel); } return(movieModel); }
public async Task <MovieFilterServiceModel> SearchWithMovieGenres(List <GenreServiceModel> genres, MovieFilterServiceModel movieModel) { List <MovieServiceModel> actualMovieList = new List <MovieServiceModel>(); foreach (GenreServiceModel genre in genres) { if (genre.IsSelected) { if (movieModel.MovieCollection.Count == 0) { List <Guid> moviesId = await this.genreService.GetAllMoviesForGenre(genre.Id); foreach (Guid movieId in moviesId) { Movie movie = await this.Db.Movies .Include(p => p.Prices) .Include(b => b.Genres) .SingleOrDefaultAsync(k => k.Id == movieId); if (movieModel.MovieCollection == null || !movieModel.MovieCollection.Any(m => m.Id == movie.Id)) { movieModel.MovieCollection.Add(new MovieServiceModel { Id = movie.Id, Name = movie.Name, Rating = movie.Rating, PosterPath = movie.PosterPath, VideoPath = movie.VideoPath, TrailerPath = movie.TrailerPath, Summary = movie.Summary, Year = movie.Year }); } } } else { foreach (MovieServiceModel movie in movieModel.MovieCollection) { List <Genre> genresList = await this.genreService.GetAllGenreForMovie(movie.Id); if (genresList.Any(g => g.Id == genre.Id) && !actualMovieList.Any(m => m.Id == movie.Id)) { actualMovieList.Add(movie); } } movieModel.MovieCollection = actualMovieList; } } } return(movieModel); }