public void SetUserSuggestions(List <Genre> newgenres) // Store the Movie Genres suggestions for User { int connecteduserid = int.Parse(HttpContext.Session.GetString("UserId")); // Get from session the user id that connected User tempUser = _context.User.Where(u => u.Id == connecteduserid).FirstOrDefault(); var usergenre = _context.UserGenre.Where(ug => ug.UserId == connecteduserid); // Get the genres related to user foreach (Genre g in newgenres) // Loop to update/add the genres to user suggestions { if (usergenre.Any(us => us.Genre == g)) // If genre is exist { usergenre.Where(us => us.Genre == g).First().Weight += 1; } else // genre doesn't exist { UserGenre userGenre = new UserGenre(); userGenre.GenreId = g.Id; userGenre.Genre = g; userGenre.UserId = tempUser.Id; userGenre.User = tempUser; userGenre.Weight = 1; if (g.UserGenre == null) { g.UserGenre = new List <UserGenre>(); } g.UserGenre.Add(userGenre); if (tempUser.UserGenre == null) { tempUser.UserGenre = new List <UserGenre>(); } tempUser.UserGenre.Add(userGenre); _context.Add(userGenre); } } _context.SaveChanges(); }
public ActionResult Create(Actress actress) { if (ModelState.IsValid) { db.Actresses.Add(actress); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(actress)); }
public ActionResult Create(Studio studio) { if (ModelState.IsValid) { db.Studios.Add(studio); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(studio)); }
public ActionResult Create(Director director) { if (ModelState.IsValid) { db.Directors.Add(director); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(director)); }
public ActionResult Create(Movie movie) { if (ModelState.IsValid) { db.Movies.Add(movie); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.ActorId = new SelectList(db.Actors, "Id", "Name", movie.ActorId); ViewBag.ActressId = new SelectList(db.Actresses, "Id", "Name", movie.ActressId); ViewBag.DirectorId = new SelectList(db.Directors, "Id", "Name", movie.DirectorId); ViewBag.StudioId = new SelectList(db.Studios, "Id", "Name", movie.StudioId); return(View(movie)); }
public static void Initialize(MovieStoreContext context) { CreateUsersIfNotExists(context); CreateMoviesIfNotExists(context); context.SaveChanges(); }
public IActionResult Register(Users u) { if (db.Users.Contains(u)) { ViewBag.Error = "User already Exist"; return(View()); } else { db.Users.Add(u); db.SaveChanges(); return(RedirectToAction("Index", "Home")); } }
protected void Button1_Click(object sender, EventArgs e) { using (var DbContex = new MovieStoreContext()) { DbContex.Movies.Add(new Movie() { Genre = new Genre() { Category = "Drama" }, Title = "Hello World" }); DbContex.SaveChanges(); } }
public MoviesController(MovieStoreContext context) { db = context; if (context.Movies.Count() == 0) { foreach (var movie in MovieDataStore.Movies) { context.Movies.Add(movie); foreach (var actor in movie.Cast) { context.Actors.Add(actor); } } context.SaveChanges(); } }
public void EditMovies(string movies, int genreId) // ADD or REMOVE movies from genres { var oldMovies = _context.MovieGenre.Include(mg => mg.Movie).Where(mg => mg.GenreId == genreId).ToList(); // Get the genres related to movie var genre = _context.Genre.Include(m => m.MovieGenre).Where(m => m.Id == genreId).FirstOrDefault(); var newMovies = movies.Split(","); // Split the selected genres to array foreach (string movie in newMovies) // Check if there is any new genres to add the movie { if (!oldMovies.Any(mg => mg.Movie.Name == movie)) // Check if the selected genres already connected to the movie { var newMovie = _context.Movie.Include(g => g.MovieGenre).Where(m => m.Name == movie).FirstOrDefault(); // Find the missing genre and add to a list MovieGenre movieGenre = new MovieGenre() { MovieId = newMovie.Id, Movie = newMovie, GenreId = genre.Id, Genre = genre, }; if (newMovie.MovieGenre == null) { newMovie.MovieGenre = new List <MovieGenre>(); } newMovie.MovieGenre.Add(movieGenre); if (genre.MovieGenre == null) { genre.MovieGenre = new List <MovieGenre>(); } genre.MovieGenre.Add(movieGenre); _context.Add(movieGenre); } } foreach (MovieGenre movie in oldMovies) // Check if there is any old genres to removed from the movie genres { if (!newMovies.Any(mg => mg == movie.Movie.Name)) { var removedMovie = _context.Movie.Include(g => g.MovieGenre).Where(g => g.Name == movie.Movie.Name).FirstOrDefault(); // Find the missing actor and add to a list removedMovie.MovieGenre.Remove(movie); genre.MovieGenre.Remove(movie); _context.MovieGenre.Remove(movie); } } _context.SaveChanges(); }
public void EditMovies(string movies, int actorId) // ADD or REMOVE movies from actor { var oldMovies = _context.MovieActor.Include(ma => ma.Movie).Where(ma => ma.ActorId == actorId).ToList(); // Get the actors related to movie var actor = _context.Actor.Include(m => m.MovieActor).Where(a => a.Id == actorId).FirstOrDefault(); var newMovies = movies.Split(","); // Split the selected movies to array foreach (string movie in newMovies) // Check if there is any new movies to add the actor { if (!oldMovies.Any(mg => mg.Movie.Name == movie)) // Check if the selected actors already connected to the movie { var newMovie = _context.Movie.Include(g => g.MovieActor).Where(m => m.Name == movie).FirstOrDefault(); // Find the missing actor and add to a list MovieActor movieGenre = new MovieActor() { MovieId = newMovie.Id, Movie = newMovie, ActorId = actor.Id, Actor = actor, }; if (newMovie.MovieActor == null) { newMovie.MovieActor = new List <MovieActor>(); } newMovie.MovieActor.Add(movieGenre); if (actor.MovieActor == null) { actor.MovieActor = new List <MovieActor>(); } actor.MovieActor.Add(movieGenre); _context.Add(movieGenre); } } foreach (MovieActor movie in oldMovies) // Check if there is any old actors to removed from the movie actors { if (!newMovies.Any(mg => mg == movie.Movie.Name)) { var removedMovie = _context.Movie.Include(g => g.MovieActor).Where(g => g.Name == movie.Movie.Name).FirstOrDefault(); // Find the missing actor and add to a list removedMovie.MovieActor.Remove(movie); actor.MovieActor.Remove(movie); _context.MovieActor.Remove(movie); } } _context.SaveChanges(); }
public void AddMovie_UnitTest() { //Arrange : var genre = new Genre { Category = "Action" }; var movie = new Movie { Genre = genre, Title = "Avatar" }; Movie movieTest; //Act using (var DbStore = new MovieStoreContext()) { movieTest = DbStore.Movies.Add(movie); DbStore.SaveChanges(); } //assert : Assert.IsTrue(movieTest.Title == movie.Title); }
public IActionResult Post([FromBody] Movie movie) { db.Movies.Add(movie); db.SaveChanges(); return(Created(movie)); }
public IActionResult Post([FromBody] Movie movie) { db.Movies.Add(movie); db.SaveChanges(); return(Created($"\\api\\movies\\{movie.Id}", movie)); }
public IActionResult Post([FromBody] Movie movie) { db.Movies.Add(movie); db.SaveChanges(); return(CreatedAtRoute("GetMovie", new { id = movie.Id }, movie)); }