コード例 #1
0
ファイル: MovieController.cs プロジェクト: vikhalv/butv
        public IActionResult Details(string sename)
        {
            var urlRecord = _urlRecordService.GetBySlug(sename);

            if (urlRecord == null || urlRecord.EntityName != "MovieItem")
            {
                return(RedirectToAction("Error", "Home"));
            }

            var movie = _movieService.GetMovieById(urlRecord.EntityId);

            if (movie == null)
            {
                return(RedirectToAction("Error", "Home"));
            }

            var model = _movieModelFactory.PrepareMovieModel(movie, 400, true, true);

            model.Description = movie.Description;
            int categoryId = ViewData["categoryId"] != null ? (int)ViewData["categoryId"] : 0;

            if (categoryId == 0)
            {
                var mc = _movieCategoryService.GetMovieCategoriesById(movie.Id);
                if (mc != null)
                {
                    model.CategoryId = categoryId = mc.FirstOrDefault().CategoryId;
                }
                else
                {
                    return(RedirectToAction("Error", "Home"));
                }
            }
            var category = _categoryService.GetCategoryById(categoryId);

            var tagParts = movie.Tags.Split(',');

            foreach (var item in tagParts)
            {
                model.Tags.Add(item.Trim());
                var tagFound = _tagService.GetByName(item.Trim());
                if (tagFound == null)
                {
                    var newTag = new Tags()
                    {
                        Hit  = 0,
                        Name = item.Trim()
                    };
                    _tagService.InsertTags(newTag);
                }
                else
                {
                    tagFound.Hit += 1;
                    _tagService.SaveTags(tagFound);
                }
            }
            var genreParts = movie.Genres.Split(',');

            foreach (var item in genreParts)
            {
                model.Genres.Add(item.Trim());
            }
            var directors = _diretorService.GetDirectorsByMovieId(movie.Id);
            var dirs      = new Dictionary <string, string>();

            foreach (var d in directors)
            {
                if (d != null)
                {
                    dirs.Add(d.Director.Name, d.Director.GetSeName());
                }
            }
            model.Directors = dirs;
            var countries    = new Dictionary <string, string>();
            var countryParts = movie.Country.Split(',');

            foreach (var c in countryParts)
            {
                countries.Add(c, ""); // need to have sename
            }
            model.Countries = countries;

            var sources  = LoadMovieEpisodes(model.Id);
            var gSources = sources.GroupBy(g => g.Source);

            if (model.HasEpisode)
            {
                model.MovieSources = new List <MovieEpisodeModel>();
                int count = 1;
                foreach (var item in gSources)
                {
                    var movieEpisode = new MovieEpisodeModel()
                    {
                        Source   = item.Key,
                        Episodes = item.ToList()
                    };
                    model.MovieSources.Add(movieEpisode);
                }
                foreach (var source in model.MovieSources.OrderBy(o => o.DisplayOrder))
                {
                    source.DisplaySource = "Server " + count++;
                }
                var mvEpisode = model.MovieSources.FirstOrDefault();
                var lastEp    = mvEpisode.Episodes.LastOrDefault();
                lastEp.Default      = true;
                model.PlayingSource = lastEp;
            }
            else
            {
                foreach (var item in gSources)
                {
                    if (item.Count() == 1)
                    {
                        var found = item.FirstOrDefault();
                        found.Default = true;
                    }
                    else
                    {
                        var found = item.FirstOrDefault(f => f.Quality.Contains("HD") && f.Name.Contains("VietSub"));
                        if (found != null)
                        {
                            found.Default = true;                // Vietsub is the top priority.
                        }
                        else
                        {
                            foreach (var s in item)
                            {
                                if (s.Quality.Contains("HD"))
                                {
                                    s.Default = true;
                                    break;
                                }
                            }
                        }
                    }
                    var movieEpisode = new MovieEpisodeModel()
                    {
                        Source   = item.Key,
                        Episodes = item.ToList()
                    };

                    model.MovieSources.Add(movieEpisode);
                }
                int count = 1;
                foreach (var source in model.MovieSources)
                {
                    if (source.Source.Equals(nameof(SourcePriority.bilutv)))
                    {
                        source.DisplayOrder = (int)SourcePriority.bilutv;
                    }

                    if (source.Source.Equals(nameof(SourcePriority.phimmoi)))
                    {
                        source.DisplayOrder = (int)SourcePriority.phimmoi;
                    }

                    if (source.Source.Equals(nameof(SourcePriority.phimbathu)))
                    {
                        source.DisplayOrder = (int)SourcePriority.phimbathu;
                    }
                }
                foreach (var source in model.MovieSources.OrderBy(o => o.DisplayOrder))
                {
                    source.DisplaySource = "Server " + count++;
                }


                model.MovieSources = model.MovieSources.OrderBy(o => o.DisplayOrder).ToList();
                if (model.MovieSources.Any())
                {
                    var episode = model.MovieSources.FirstOrDefault();
                    if (episode != null && episode.Episodes.Any())
                    {
                        model.PlayingSource = model.MovieSources.FirstOrDefault().Episodes.FirstOrDefault(f => f.Default);
                    }
                    else
                    {
                        model.PlayingSource = new SubMovieItemModel();
                    }
                }
            }

            string breadcrumbCacheKey = string.Format(ModelCacheEventConsumer.MOVIE_BREADCRUMB_KEY, movie.Id);

            model.Breadcrumb = _cacheManager.Get(breadcrumbCacheKey, () =>
            {
                var breadcrumbModel = new MovieBreadcrumbModel
                {
                    MovieId     = movie.Id,
                    MovieName   = movie.Name,
                    MovieSeName = movie.GetSeName()
                };
                breadcrumbModel.CategoryBreadcrumb = category.GetCategoryBreadCrumb(_categoryService)
                                                     .Select(catBr => new CategorySimpleModel
                {
                    Id     = catBr.Id,
                    Name   = catBr.Name,
                    SeName = catBr.GetSeName()
                })
                                                     .ToList();
                return(breadcrumbModel);
            });

            movie.View += 1;
            _movieService.SaveMovie(movie);
            return(View(model));
        }