public static List<Models.Movie> GetDatabaseMovies(Titles results) { var netflixids = results.Select((m) => m.Id + (m.SeasonId != "" ? ";" + m.SeasonId : "")); using (FilmTroveContext ftc = new FilmTroveContext()) { ///1) find the matching records from the database var matchedmovies = ftc.Movies.Where(m => netflixids.Contains(m.Netflix.Id)); ///2) find the records that don't have a match ///select the ids and get the netflix Ids that aren't in the FT database var ftnfids = matchedmovies.Select(m => m.Netflix.Id); var netflixidsunmatched = netflixids.Where(m => !ftnfids.Contains(m)); //Int32 count = 0; foreach (String nid in netflixidsunmatched) { ///create FT database records for each of these with the movies basic information for now FilmTrove.Models.Movie newmovie = ftc.Movies.Create(); FlixSharp.Holders.Title netflixmovie = results.Find(nid); FillBasicTitle(newmovie, netflixmovie); var dbgenreslocal = ftc.Genres.Local.Where(g => netflixmovie.Genres.Contains(g.Name)); var dbgenres = ftc.Genres.Where(g => netflixmovie.Genres.Contains(g.Name)); HashSet<Genre> genres = new HashSet<Genre>(); genres.AddRange(dbgenres); genres.AddRange(dbgenreslocal); var genrenames = genres.Select(g => g.Name); var missinggenres = netflixmovie.Genres.Where(g => !genrenames.Contains(g)); foreach (String genre in missinggenres) { Genre g = new Genre() { Name = genre }; genres.Add(g); ftc.Genres.Add(g); } //newmovie.Genres = netflixmovie.Genres; foreach (Genre g in genres) { MovieGenre gi = ftc.GenreItems.Create(); gi.Genre = g; gi.Movie = newmovie; ftc.GenreItems.Add(gi); } ftc.Movies.Add(newmovie); } //try //{ //count = ftc.SaveChanges(); //} //catch (Exception ex) //{ // ///need to add some sort of logging? //} //if (count > 0) if (matchedmovies.Count() < results.Count()) matchedmovies = ftc.Movies.Where(m => netflixids.Contains(m.Netflix.Id)); //else // return matchedmovies.ToList(); return results.Select(m => matchedmovies.First(f => f.Netflix.Id == (m.Id + (m.SeasonId != "" ? ";" + m.SeasonId : "")))).ToList(); } }
public static List<Models.Person> GetDatabasePeople(People results) { var netflixids = results.Select(p => p.Id); using (FilmTroveContext ftc = new FilmTroveContext()) { ///1) find the matching records from the database var matchedpeople = ftc.People.Where(m => netflixids.Contains(m.Netflix.Id)); ///2) find the records that don't have a match ///select the ids and get the netflix Ids that aren't in the FT database var ftnfids = matchedpeople.Select(m => m.Netflix.Id); var netflixidsunmatched = netflixids.Where(m => !ftnfids.Contains(m)); //Int32 count = 0; foreach (String nid in netflixidsunmatched) { ///create FT database records for each of these with the movies basic information for now FilmTrove.Models.Person newperson = ftc.People.Create(); FlixSharp.Holders.Person netflixperson = results.Find(nid); FillBasicPerson(newperson, netflixperson); //newperson.Name = netflixperson.Name; //newperson.Bio = netflixperson.Bio; //newperson.Netflix = new NetflixPersonInfo(); //newperson.Netflix.Id = netflixperson.Id; //newperson.Netflix.IdUrl = netflixperson.IdUrl; //newperson.Netflix.Url = netflixperson.NetflixSiteUrl; ftc.People.Add(newperson); } try { //count = ftc.SaveChanges(); } catch (Exception) { throw; ///need to add some sort of logging? } //if (count > 0) if (matchedpeople.Count() < results.Count()) matchedpeople = ftc.People.Where(m => netflixids.Contains(m.Netflix.Id)); //else // return matchedpeople.ToList(); return results.Select(p => matchedpeople.First(f => f.Netflix.Id == p.Id)).ToList(); } }