public bool AddRating(string review, int starsAmount, int movieId, long facebookId) { Comment comment; Rating rating; bool status = false; try { if (!string.IsNullOrEmpty(review)) { rating = new Rating() { MovieId = movieId, StarsAmount = starsAmount, UserId = facebookId }; unitOfWork.RatingsRepository.Insert(rating); comment = new Comment() { MovieId = movieId, Description = review, UserId = facebookId, RatingId = rating.Id }; unitOfWork.CommentsRepository.Insert(comment); } unitOfWork.Save(); status = true; } catch (Exception) { throw; } return status; }
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; } }