// Helper function which stores image in movie objects and also saves the image in specified folder private void SaveMovieImage(PublicMovie movie, HttpPostedFileBase file) { string uploads = "~/Uploads"; // Check if file directory Exists and if not create it if (!Directory.Exists(Server.MapPath(uploads))) { Directory.CreateDirectory(Server.MapPath(uploads)); } if (file != null && file.ContentLength > 0) { // Create path for Image to be uploaded string relativePath = uploads + "/" + file.FileName; string physicalPath = Server.MapPath(relativePath); // Store path in movie object movie.Image = relativePath; // If folder does not contain image, add it to the folder if (!Server.MapPath(uploads).Contains(file.FileName)) { file.SaveAs(physicalPath); } } else // file is not selected { // If no image is selected make the image to default if (String.IsNullOrEmpty(file.FileName)) { movie.Image = uploads + "/default.jpg"; } } }
public ActionResult Edit([Bind(Include = "PublicMovieId, Title, Description, Year")] PublicMovie publicMovie) { if (ModelState.IsValid) { // Get List of all usermovies with same movie id var userMovie = db.UserMovies.Where(movie => movie.PublicMovieId == publicMovie.PublicMovieId).ToList(); HttpPostedFileBase file = Request.Files["Image"]; SaveMovieImage(publicMovie, file); // Replace the image location for every instance of selected movie in UserMovie userMovie.ForEach(movie => movie.Image = publicMovie.Image); // Rename title for every instance of usermovie userMovie.ForEach(movie => movie.Title = publicMovie.Title); db.Entry(publicMovie).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(publicMovie)); }
public ActionResult AddMovie([Bind(Include = "PublicMovieId, Title, Description, Image, Year")] PublicMovie movie) { if (ModelState.IsValid) { HttpPostedFileBase file = Request.Files["Image"]; // Retrieve file SaveMovieImage(movie, file); // Add and save movie inside database db.PublicMovies.Add(movie); db.SaveChanges(); return(RedirectToAction("Index", "PublicMovies")); } return(View(movie)); }
public ActionResult DeleteConfirmed(int id) { PublicMovie publicMovie = db.PublicMovies.Find(id); // Remove from all user's lists db.UserMovies.RemoveRange(db.UserMovies.Where(movie => movie.PublicMovieId == id)); // Remove the public version of movie db.PublicMovies.Remove(publicMovie); // Remove every review made for this movie db.Reviews.RemoveRange(db.Reviews.Where(review => review.PublicMovieId == id)); db.SaveChanges(); return(RedirectToAction("Index")); }
// Get movie object to delete public ActionResult Delete(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } PublicMovie movie = db.PublicMovies.Find(id); if (movie == null) { return(HttpNotFound()); } return(View(movie)); }
// Retrieve data from UserMovie and PublicMovie to store in a ViewModel public ActionResult MovieInfo(int?id) { if (id == null) { return(new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest)); } // createUserList(); // Create user if one does not already exist PublicMovie publicMovie = db.PublicMovies.Find(id); // Get selected movie if (publicMovie == null) { return(HttpNotFound()); } // Create movie model to store current movie MovieDetailViewModel movieDetailViewModel = new MovieDetailViewModel() { Movie = publicMovie }; // Get logged in user var currUser = db.UserLists.FirstOrDefault(user => user.User.UserName == User.Identity.Name); // If current user is logged in and contains a movie with title of selected movie update the database if (currUser != null && currUser.WatchList.Where(movie => movie.Title == publicMovie.Title).Any()) { movieDetailViewModel.Status = currUser.WatchList.FirstOrDefault(movie => movie.Title == publicMovie.Title).Status; movieDetailViewModel.UserMovieId = currUser.UserListId; } movieDetailViewModel.ReviewsList.AddRange(db.Reviews.Where(review => review.PublicMovieId == publicMovie.PublicMovieId).Take(3)); // Get review for current movie return(View(movieDetailViewModel)); }