public void UpdateMovie(Movie movie) { using (MovieDBContext db = new MovieDBContext()) { var updMovie = db.Movies.Include("Actors") .Where(c => c.Id == movie.Id) .FirstOrDefault(); updMovie.Title = movie.Title; updMovie.Description = movie.Description; updMovie.Cover = movie.Cover; updMovie.Duration = movie.Duration; updMovie.Year = movie.Year; IEnumerable <Actor> AddedActors = movie.Actors.Where(actor => !updMovie.Actors.Contains(actor)); IEnumerable <Actor> RemovedActors = new List <Actor>(updMovie.Actors.Where(actor => !movie.Actors.Contains(actor))); foreach (Actor a in AddedActors) { updMovie.Actors.Add(db.Actors.Find(a.Id)); } foreach (Actor a in RemovedActors) { updMovie.Actors.Remove(db.Actors.Find(a.Id)); } db.SaveChanges(); } }
public void RecordMovie(Movie movie) { using (MovieDBContext db = new MovieDBContext()) { movie.Actors = movie.Actors.Select(a => a = db.Actors.Find(a.Id)).ToList(); db.Movies.Add(movie); db.SaveChanges(); } }
public void RemoveMovie(Movie movie) { using (MovieDBContext db = new MovieDBContext()) { Movie delMovie = db.Movies .Include("Actors") .Where(item => item.Id == movie.Id) .FirstOrDefault(); db.Movies.Remove(delMovie); db.SaveChanges(); } }
public void RecordPerson(IPerson person) { using (MovieDBContext db = new MovieDBContext()) { person.Movies = person.Movies.Select(m => m = db.Movies.Find(m.Id)).ToList(); if (person is Actor) { db.Actors.Add((Actor)person); } else if (person is Director) { db.Directors.Add((Director)person); } db.SaveChanges(); } }
public void UpdatePerson(IPerson person) { using (MovieDBContext db = new MovieDBContext()) { IPerson updActor = null; if (person is Actor) { updActor = db.Actors .Where(c => c.Id == person.Id) .FirstOrDefault(); } else if (person is Director) { updActor = db.Directors .Where(c => c.Id == person.Id) .FirstOrDefault(); } //updActor = db.Actors // .Where(c => c.Id == person.Id) // .FirstOrDefault(); updActor.Photo = person.Photo; updActor.Name = person.Name; updActor.Surname = person.Surname; updActor.Patronymic = person.Patronymic; updActor.Country = person.Country; updActor.Birth_Date = person.Birth_Date; IEnumerable <Movie> AddedMovies = person.Movies.Where(movie => !updActor.Movies.Contains(movie)); IEnumerable <Movie> RemovedMovies = new List <Movie>(updActor.Movies.Where(movie => !person.Movies.Contains(movie))); foreach (Movie m in AddedMovies) { updActor.Movies.Add(db.Movies.Find(m.Id)); } foreach (Movie m in RemovedMovies) { updActor.Movies.Remove(db.Movies.Find(m.Id)); } db.SaveChanges(); } }
public void RemovePerson(IPerson person) { using (MovieDBContext db = new MovieDBContext()) { if (person is Actor) { Actor delActor = db.Actors .Include("Movies") .Where(a => a.Id == person.Id) .FirstOrDefault(); db.Actors.Remove(delActor); } else if (person is Director) { Director delActor = db.Directors .Include("Movies") .Where(a => a.Id == person.Id) .FirstOrDefault(); db.Directors.Remove(delActor); } db.SaveChanges(); } }