public MovieModel Create(Movie movie) { StringBuilder sbArtists = new StringBuilder(); var movieModel = new MovieModel() { Id = movie.Id, Title = movie.Title, ImagePath = movie.ImagePath, Genre = movie.Genre, ReleaseDate = movie.ReleaseDate, Tags = movie.Tags }; if (movie.Artists != null) { movieModel.Hero = movie.GetArtistName(ArtistRole.Hero); movieModel.Heroin = movie.GetArtistName(ArtistRole.Heroin); movieModel.CharacterArtists = movie.GetArtistName(ArtistRole.CharacterArtist); movieModel.Director = movie.GetArtistName(ArtistRole.Director); movieModel.MusicDirector = movie.GetArtistName(ArtistRole.MusicDirector); movieModel.Producer = movie.GetArtistName(ArtistRole.Producer); } if (movie.Reviews != null) { movieModel.Reviews = new List<ReviewModel>(); movie.Reviews.ToList().ForEach(mr => { movieModel.Reviews.Add(Create(mr)); }); } return movieModel; }
public Movie Parse(Movie movie, MovieModel movieModel, IUnitOfWork _unitOfWork) { movie.Title = movieModel.Title; movie.Genre = movieModel.Genre; movie.ReleaseDate = movieModel.ReleaseDate; movie.ImagePath = movieModel.ImagePath; movie.Tags = movieModel.Tags; movie.Artists = ParseArtists(movie, movieModel, _unitOfWork); return movie; }
public static void SaveMovie(this IRepository<Movie> movieRepository, Movie movie) { if (movie.State == ObjectState.Added) { movieRepository.Insert(movie); } else if(movie.State == ObjectState.Modified) { movieRepository.Update(movie); } }
private ICollection<MovieArtist> ParseArtists(Movie movie, MovieModel movieModel, IUnitOfWork _unitOfWork) { MovieArtist movieArtist; ArtistRole[] roles = { ArtistRole.Hero, ArtistRole.Heroin, ArtistRole.Director, ArtistRole.Producer, ArtistRole.MusicDirector }; string artistName; if (movie.Artists == null) { movie.Artists = new List<MovieArtist>(); } foreach (var role in roles) { artistName = movieModel.GetNameByRole(role); if (!string.IsNullOrEmpty(artistName)) { var artist = _unitOfWork.Repository<Artist>().Query().Filter(a => string.Equals(artistName, a.Name)).FirstOrDefault(); if (artist == null) { artist = new Artist() { Name = artistName, PrimaryRole = role, State = ObjectState.Added }; } if (string.IsNullOrEmpty(movie.GetArtistName(role))) { movieArtist = new MovieArtist() { Artist = artist, Movie = movie, Role = role, State = ObjectState.Added }; movie.Artists.Add(movieArtist); } else if (movie.GetArtistName(role) != artistName) { movieArtist = movie.Artists.Where(ma => ma.Role == role).FirstOrDefault(); movieArtist.Artist = artist; movieArtist.State = ObjectState.Modified; } } else if (!string.IsNullOrEmpty(movie.GetArtistName(role))) { movieArtist = movie.Artists.Where(ma => ma.Role == role).FirstOrDefault(); movieArtist.State = ObjectState.Deleted; } } return movie.Artists; }
public void Seed() { SeedUsers(); if (_context.Reviewers.Count() == 0) { _context.Reviewers.Add(new Reviewer() { Name = "Reviewer1", SiteUrl = "http://www.google.com", State = ObjectState.Added }); _context.Reviewers.Add(new Reviewer() { Name = "Reviewer2", SiteUrl = "http://www.google.com", State = ObjectState.Added }); _context.SaveChanges(); } if (_context.Movies.Count() == 0) { for (int i = 1; i <= 100; i++) { Random random = new Random(); var genreArray = Enum.GetValues(typeof(Genre)); var movieArtists = new List<MovieArtist>(); var movieReviews = new List<MovieReview>(); Movie movie = new Movie() { Title = "Movie" + i, Genre = (Genre)genreArray.GetValue(random.Next(1, genreArray.Length)), ImagePath = "default.jpg", ReleaseDate = DateTime.Now.AddMonths(i * -1), Language = Language.Telugu, State = ObjectState.Added }; foreach (ArtistRole role in Enum.GetValues(typeof(ArtistRole))) { movieArtists.Add(new MovieArtist() { Movie = movie, Artist = new Artist() { Name = role.ToString() + i, PrimaryRole = role, State = ObjectState.Added }, Role = role, State = ObjectState.Added }); } foreach (var item in _context.Reviewers) { movieReviews.Add(new MovieReview() { Movie = movie, TagLine = "tag line goes here", Rating = random.Next(1, 5), Reviewer = item, Review = "review goes here", ReviewedDate = DateTime.Now.AddMonths(i * -1), State = ObjectState.Added }); } _context.Movies.Add(movie); movieArtists.ForEach(ma => _context.MovieArtists.Add(ma)); movieReviews.ForEach(mr => _context.MovieReviews.Add(mr)); } _context.SaveChanges(); } }