public ActionResult Edit(int id, DetailMovieEditView movieView, FormCollection collection) { var request = new AddUpdateMovieRequest { Id = id, Name = movieView.Movie.Name, CertificationId = collection["CertificationId"] != "" ? Convert.ToInt32(collection["CertificationId"]) : 0, Released = movieView.Movie.Released, RunTime = movieView.Movie.RunTime, GenresId = collection["GenreId"] != "" ? Convert.ToInt32(collection["GenreId"]) : 0 }; var response = _movieService.AddUpdateMovie(request); if (response.Successful) return RedirectToAction("Details", new {id = response.Id}); return View(response); }
/// <summary> /// Add/Update movie /// </summary> /// <param name="request"></param> /// <returns></returns> public AddUpdateMovieResponse AddUpdateMovie(AddUpdateMovieRequest request) { Movie movie = _movieRepository.FindBy(request.Id) ?? new Movie(); movie.Name = request.Name; movie.CertificationId = request.CertificationId > 0 ? request.CertificationId : null; movie.Released = request.Released; movie.RunTime = request.RunTime; if (request.GenresId != 0 && !IsGenreInList(movie.Genres, request.GenresId)) { Genre genre = _genreRepository.FindBy(request.GenresId); if (movie.Genres == null) movie.Genres = new List<Genre>(); movie.Genres.Add(genre); } var response = new AddUpdateMovieResponse(); try { _movieRepository.Save(movie); _uow.Commit(); response.Successful = true; response.Id = movie.Id; response.Movie = movie.ConvertToMovieDetailView(); } catch { response.Successful = false; response.Message = "An Error occurred while trying to save."; response.Id = movie.Id; response.Movie = movie.ConvertToMovieDetailView(); } return response; }