private void SaveImage(MovieViewModel movieViewModel, HttpPostedFileBase file) { if (file != null) { file.SaveAs(HttpContext.Server.MapPath("~/Posters/") + movieViewModel.Id + ".jpg"); } }
private void AddDirector(MovieViewModel movieViewModel) { movieViewModel.CastAndCrews.Add(new CastAndCrew { MovieId = movieViewModel.Id, PeopleId = movieViewModel.DirectorId, CareerId = _careerService.GetCareerByTitle("Director").Id }); }
private void GetImdbRating(MovieViewModel movieViewModel) { string url = String.Format("http://www.omdbapi.com/?i={0}", movieViewModel.ImdbId); var request = (HttpWebRequest)WebRequest.Create(url); var response = (HttpWebResponse)request.GetResponse(); string jsonResponse = ""; using (var streamReader = new StreamReader(response.GetResponseStream())) { jsonResponse = streamReader.ReadToEnd(); } var values = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonResponse); if (String.Equals(values["Response"], "True", StringComparison.OrdinalIgnoreCase)) { movieViewModel.ImdbRating = values["imdbRating"]; } else { movieViewModel.ImdbRating = "Movie not found on Imdb"; } }
//For Edit and Create Genres (POST) private void CreateOfUpdateGenresOfMovie(string[] selectedGenres, MovieViewModel movieViewModel) { if (selectedGenres == null) { movieViewModel.Genres = new List<Genre>(); return; } if (movieViewModel.Genres == null) { movieViewModel.Genres = new List<Genre>(); } var selectedGenresHs = new HashSet<string>(selectedGenres); var genresOfMovie = new HashSet<Guid>(movieViewModel.Genres.Select(c => c.Id)); foreach (var genre in _genreService.GetAll()) { if (selectedGenresHs.Contains(genre.Id.ToString())) { if (!genresOfMovie.Contains(genre.Id)) { movieViewModel.Genres.Add(genre); } } else { if (genresOfMovie.Contains(genre.Id)) { movieViewModel.Genres.Remove(genre); } } } }
//For Edit and Create Genres (GET) private List<GenresOfMovieViewModel> GetGenreList(MovieViewModel movieViewModel) { if (movieViewModel.Genres == null) { movieViewModel.Genres = new List<Genre>(); } var allGenres = _genreService.GetAll(); var genresOfMovie = new HashSet<Guid>(movieViewModel.Genres.Select(c => c.Id)); var genreList = new List<GenresOfMovieViewModel>(); foreach (var genre in allGenres) { genreList.Add(new GenresOfMovieViewModel { GenreId = genre.Id, Title = genre.Title, Choosen = genresOfMovie.Contains(genre.Id) }); } return genreList; }
//For Edit and Create Actors (POST) private void CreateOfUpdateActorsInMovie(string[] selectedActors, MovieViewModel movieViewModel) { if (selectedActors == null) { movieViewModel.CastAndCrews = new List<CastAndCrew>(); return; } if (movieViewModel.CastAndCrews == null) { movieViewModel.CastAndCrews = new List<CastAndCrew>(); } var selectedActorsHs = new HashSet<string>(selectedActors); var actorsInMovie = new HashSet<Guid>(movieViewModel.CastAndCrews.Select(c => c.PeopleId)); foreach (var actor in _peopleService.GetAll()) { var addActor = new CastAndCrew { MovieId = movieViewModel.Id, PeopleId = actor.Id, CareerId = _careerService.GetCareerByTitle("Actor").Id }; if (selectedActorsHs.Contains(actor.Id.ToString())) { if (!actorsInMovie.Contains(actor.Id)) { movieViewModel.CastAndCrews.Add(addActor); } } else { if (actorsInMovie.Contains(actor.Id)) { movieViewModel.CastAndCrews.Remove(addActor); } } } }
//For Edit and Create Actors (GET) private List<ActorsInMovieViewModel> GetActorsList(MovieViewModel movieViewModel) { if (movieViewModel.CastAndCrews == null) { movieViewModel.CastAndCrews = new List<CastAndCrew>(); } var allActors = _peopleService.GetAll(); var actorsInMovie = new HashSet<Guid>(movieViewModel.CastAndCrews.Select(c => c.PeopleId)); var actorsList = new List<ActorsInMovieViewModel>(); foreach (var actor in allActors) { actorsList.Add(new ActorsInMovieViewModel { ActorId = actor.Id, FullName = actor.FullName, Choosen = actorsInMovie.Contains(actor.Id) }); } return actorsList; }
public ActionResult CreateOrUpdate(Guid? id) { bool isCreate = !id.HasValue; MovieViewModel movieViewModel; if (isCreate) { movieViewModel = new MovieViewModel { PremiereDate = null }; } else { var movie = _movieService.GetMovieById(id); movieViewModel = Mapper.Map<Movie, MovieViewModel>(movie); movieViewModel.DirectorId = _peopleService.GetDirectorIdForMovie(movie); } var countries = _countryService.GetAll(); var directors = _peopleService.GetAll(); movieViewModel.ActorsList = GetActorsList(movieViewModel); movieViewModel.GenreList = GetGenreList(movieViewModel); movieViewModel.Directors = new SelectList(directors, "Id", "FullName"); movieViewModel.Countries = new SelectList(countries, "Id", "Title"); return View(movieViewModel); }