public static EditMovieViewModel MapFromMovieToEditViewModel(Movie movie) { Mapper.CreateMap<Movie, EditMovieViewModel>() .ForMember(dest => dest.Actors, opt => opt.Ignore()); EditMovieViewModel viewModel = Mapper.Map<EditMovieViewModel>(movie); return viewModel; }
public static SearchMovieList MapFromMovieToSearchMovieList(Movie movie, long facebookId, string oAuthToken) { Mapper.CreateMap<Movie, SearchMovieList>() .ForMember(dest => dest.ImagePath, opt => opt.MapFrom(src => src.Images.FirstOrDefault(i => i.TypeId == (int)ImageKind.Thumbnail).Path)) .ForMember(dest => dest.UrlToMovieDetails, opt => opt.MapFrom(src => string.Format("/Home/Movie/{0}?facebookId={1}&oAuthToken={2}", src.Id, facebookId, oAuthToken))); SearchMovieList viewModel = Mapper.Map<SearchMovieList>(movie); return viewModel; }
public static IndexMovieList MapFromMovieToIndexMovieList(Movie movie) { Mapper.CreateMap<Movie, IndexMovieList>() .ForMember(dest => dest.CoverImagePath, opt => opt.MapFrom(src => src.Images.FirstOrDefault(i => i.TypeId == (int)ImageKind.Cover).Path)) .ForMember(dest => dest.GenreName, opt => opt.MapFrom(src => src.Genre.Name)); IndexMovieList viewModel = Mapper.Map<IndexMovieList>(movie); return viewModel; }
public Movie UpdateMovie(Movie movie) { Movie dbMovie = new Movie(); try { dbMovie = unitOfWork.MoviesRepository.GetById(movie.Id); dbMovie.DirectorName = movie.DirectorName; dbMovie.Description = movie.Description; dbMovie.ProducerName = movie.ProducerName; dbMovie.Title = movie.Title; dbMovie.YoutubeLink = movie.YoutubeLink; unitOfWork.MoviesRepository.Update(dbMovie); unitOfWork.Save(); } catch (Exception) { throw; } return dbMovie; }
public Movie GetMovie(int id) { Movie movie = new Movie(); try { if (id <= 0) throw new ArgumentNullException(string.Format("Class name: {0}, Method name: {1}, Parameters: {2}", this.GetType().Name, MethodBase.GetCurrentMethod().Name, MethodBase.GetCurrentMethod().GetParameters()), "One of these parameters is empty or has no valid value"); movie = unitOfWork.MoviesRepository.GetById(id); } catch (Exception) { throw; } return movie; }
static void Main(string[] args) { Console.WriteLine("This is the start of hell!"); Console.WriteLine("Timer is set"); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); try { var result = string.Empty; int counter = 1; for (char c = 'A'; c <= 'Z'; c++) { var moviesSearchUrl = string.Format("http://api.rottentomatoes.com/api/public/v1.0/movies.json?q={0}&page_limit=50&page={1}&apikey={2}", c, counter, WebServiceCredentials.apiKey); var webRequest = WebRequest.Create(moviesSearchUrl); webRequest.Timeout = 120000; if (c == 'Z') { counter++; c = 'A'; } using (var response = webRequest.GetResponse() as HttpWebResponse) { if (response.StatusCode == HttpStatusCode.OK) { var receiveStream = response.GetResponseStream(); if (receiveStream != null) { var stream = new StreamReader(receiveStream); result = stream.ReadToEnd(); } } JObject json = JObject.Parse(result); JArray jMovies = (JArray)json["movies"]; using (var context = new MovieDatabaseDataContext()) { foreach (var item in jMovies) { JObject mObject = (JObject)item; if ((string)mObject["synopsis"] == string.Empty || (string)mObject["synopsis"] == string.Empty) { continue; } string currentMovieTitle = (string)mObject["title"]; if (!context.Movies.Any(m => m.Title == currentMovieTitle)) { AdditionalMovieData additionalData = ServiceHelper.GetAdditionalMovieData((string)mObject["id"]); if (string.IsNullOrEmpty(additionalData.DirectorName) || string.IsNullOrEmpty(additionalData.Genre) || string.IsNullOrEmpty(additionalData.ProducerName) || mObject["critics_consensus"] == null) { continue; } Movie newMovie = new Movie() { Title = currentMovieTitle, ReleaseDate = (DateTime)mObject["release_dates"].ToObject<ReleaseDate>().Theater, DurationTime = ServiceHelper.ConvertStringToTimeSpan((string)mObject["runtime"]), Description = (string)mObject["synopsis"], DirectorName = additionalData.DirectorName, ProducerName = additionalData.ProducerName, GenreId = ServiceHelper.GetGenreIdFromName(additionalData.Genre), YoutubeLink = ServiceHelper.GetYoutubeLink(currentMovieTitle) }; if (newMovie.GenreId != 0) { context.Movies.Add(newMovie); context.SaveChanges(); JArray jActors = (JArray)mObject["abridged_cast"]; List<Actor> movieActors = new List<Actor>(); foreach (var actor in jActors) { JObject aObject = (JObject)actor; string actorName = (string)aObject["name"]; Actor dbActor = context.Actors.FirstOrDefault(a => a.Name == actorName); if (dbActor == null) { Actor newActor = new Actor(); newActor.Birthday = DateTime.Now; newActor.Name = actorName; newActor.Biography = string.Empty; newActor.Birthplace = string.Empty; newActor.Movies.Add(newMovie); context.Actors.Add(newActor); } else { if (dbActor.Movies.Any(m => m.Title == currentMovieTitle)) { dbActor.Movies.Add(newMovie); context.Entry(dbActor).State = EntityState.Modified; context.SaveChanges(); } } } List<Image> images = ServiceHelper.GetAllNeededImages(newMovie.Id, mObject["posters"].ToObject<Posters>(), (string)mObject["title"]); images.ForEach(n => context.Images.Add(n)); context.SaveChanges(); } } } } } } using (var context = new MovieDatabaseDataContext()) { int defaultUsersAmount = int.MaxValue; Random randomMovie = new Random(); int minimumUserIdValue = (int)context.Users.OrderByDescending(u => u.FacebookId).FirstOrDefault().FacebookId + 1; for (int i = minimumUserIdValue; i < defaultUsersAmount; i++) { int movieId = context.Movies.OrderBy(x => Guid.NewGuid()).Take(1).Select(x => x.Id).First(); KeyValuePair<string, int> randomCommentAndRating = ServiceHelper.GetRandomComment(randomMovie); User user = new User() { FacebookId = i, AddedDate = DateTime.Now, LastVisitDate = DateTime.Now, Name = string.Format("Default User {0}", i), ProfileImagePath = "/Content/Images/Other/avatar-blank.jpg" }; context.Users.Add(user); Rating rating = new Rating() { MovieId = movieId, StarsAmount = randomCommentAndRating.Value, UserId = user.FacebookId }; context.Ratings.Add(rating); Comment comment = new Comment() { MovieId = movieId, Description = randomCommentAndRating.Key, UserId = user.FacebookId, RatingId = rating.Id }; context.Comments.Add(comment); context.SaveChanges(); } } stopwatch.Stop(); Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed); Console.WriteLine("Click some key on keyboard to exit..."); Console.ReadKey(); } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } }