Esempio n. 1
0
        /// <summary>
        /// Returns an IQueryable of items of type T.
        /// </summary>
        /// <param name="predicate">A predicate to limit the items being returned.</param>
        /// <param name="includeProperties">An expression of additional properties to eager load. For example: x => x.SomeCollection, x => x.SomeOtherCollection.</param>
        /// <returns>An IEnumerable of the requested type T.</returns>
        public IEnumerable <T> FindAll(Expression <Func <T, bool> > predicate, params Expression <Func <T, object> >[] includeProperties)
        {
            IQueryable <T> items = DataContextFactory.GetDataContext().Set <T>();

            if (includeProperties != null)
            {
                foreach (var includeProperty in includeProperties)
                {
                    items = items.Include(includeProperty);
                }
            }
            return(items.Where(predicate));
        }
Esempio n. 2
0
        /// <summary>
        /// Returns an IQueryable of all items of type T.
        /// </summary>
        /// <param name="includeProperties">An expression of additional properties to eager load. For example: x => x.SomeCollection, x => x.SomeOtherCollection.</param>
        /// <returns>An IQueryable of the requested type T.</returns>
        public IQueryable <Movie> FindAll(params Expression <Func <Movie, object> >[] includeProperties)
        {
            IQueryable <Movie> items = DataContextFactory.GetDataContext().Movie;

            if (includeProperties != null)
            {
                foreach (var includeProperty in includeProperties)
                {
                    items = items.Include(includeProperty);
                }
            }
            return(items);
        }
Esempio n. 3
0
        /// <summary>
        /// Returns an IQueryable of all items of type T.
        /// </summary>
        /// <param name="includeProperties">An expression of additional properties to eager load. For example: x => x.SomeCollection, x => x.SomeOtherCollection.</param>
        /// <returns>An IQueryable of the requested type T.</returns>
        public virtual IQueryable <T> FindAll(params Expression <Func <T, object> >[] includeProperties)
        {
            IQueryable <T> items = DataContextFactory.GetDataContext().Set <T>();

            if (includeProperties != null)
            {
                foreach (var includeProperty in includeProperties)
                {
                    items = items.Include(includeProperty);
                }
            }
            return(items);
        }
Esempio n. 4
0
        //public async Task<ICollection<Movie>> GetMovieByTitle(string title)
        //{
        //    IQueryable<Movie> items = DataContextFactory.GetDataContext().Set<Movie>();
        //    var t = DataContextFactory.GetDataContext().Movie.Select(m => m.Title.Contains(title));
        //}

        //public async Task<ICollection<Movie>> GetMovieByTitleAPI(string title)
        //{
        //using (httpClient)
        //{
        //    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json");

        //    using (var response = await httpClient.GetAsync("search/movie?query=" + title + "&api_key=" + apiKey))
        //    {
        //        var responseDataStr = await response.Content.ReadAsStringAsync();
        //        //responseDataStr = responseDataStr.Remove(responseDataStr.IndexOf("\"keywords\":{"), 12);
        //        //responseDataStr = responseDataStr.Remove(responseDataStr.Length - 1, 1);

        //        var movies = JObject.Parse(responseDataStr)["results"]
        //            .ToObject<ICollection<Movie>>();

        //        //var movie = JsonConvert.DeserializeObject<Movie>(responseDataStr);

        //        //if (movie.Status == null)
        //        //    movie.Status = new Status { Name = "Released" };

        //        return movies;
        //    }
        //}
        //}

        public async Task <Movie> FindById(int id)
        {
            var t = await DataContextFactory.GetDataContext().Movie.FindAsync(id);

            if (t == null)
            {
                return(await new TheMovieDBRepository().FindMovieById(id));
            }
            else
            {
                return(t);
            }
        }
        private async Task AddPerson(Person person, List <string> altNames)
        {
            using (new EFUnitOfWorkFactory().Create())
            {
                var context = DataContextFactory.GetDataContext();

                context.Person.AddOrUpdate(p => p.Id, person);

                foreach (var an in altNames)
                {
                    if (await context.AlternativeName.FindAsync(person.Id, an) == null)
                    {
                        context.AlternativeName.Add(new AlternativeName {
                            PersonId = person.Id, Name = an
                        });
                    }
                }
            }
        }
        public async Task <Person> FindPersonById(int id)
        {
            string requestUri = "person/" + id + "?api_key=" + apiKey + "&append_to_response=external_ids";
            var    data       = await GetData(requestUri) as JObject;

            if (!CheckStatus(data))
            {
                return(null);
            }

            JsonSerializerSettings jss = new JsonSerializerSettings
            {
            };

            ValidateDate(ref data, "birthday", "deathday");
            var person  = JsonConvert.DeserializeObject <Person>(JsonConvert.SerializeObject(data));
            var altName = JsonConvert.DeserializeObject <List <string> >(JsonConvert.SerializeObject(data["also_known_as"]));

            await AddPerson(person, altName);

            return(DataContextFactory.GetDataContext().Person.Find(person.Id));
        }
        public async Task AddMovieAsync(Movie entity, List <Genre> genres, List <Keyword> keywords, List <Credit> castCredit, List <Credit> crewCredit)
        {
            //Add persons first because they need API calls to populate and it will kill the context
            var context = DataContextFactory.GetDataContext();

            foreach (var cr in crewCredit)
            {
                var person = await context.Person.FindAsync(cr.PersonId);

                if (person == null)
                {
                    await FindPersonById(cr.PersonId);
                }
            }
            foreach (var cr in castCredit)
            {
                var person = await context.Person.FindAsync(cr.PersonId);

                if (person == null)
                {
                    await FindPersonById(cr.PersonId);
                }
            }

            using (new EFUnitOfWorkFactory().Create())
            {
                context = DataContextFactory.GetDataContext();
                context.Movie.AddOrUpdate(m => m.Id, entity);

                foreach (var gen in genres)
                {
                    context.Genre.AddOrUpdate(g => g.Id, gen);
                    context.MovieGenre.AddOrUpdate(new MovieGenre {
                        MovieId = entity.Id, GenreId = gen.Id
                    });
                }

                foreach (var key in keywords)
                {
                    context.Keyword.AddOrUpdate(k => k.Id, key);
                    context.MovieKeyword.AddOrUpdate(new MovieKeyword {
                        MovieId = entity.Id, KeywordId = key.Id
                    });
                }

                foreach (var cr in crewCredit)
                {
                    var dept = await context.Department.FindAsync(cr.DepartmentName);

                    if (dept == null)
                    {
                        dept = new Department {
                            Name = cr.DepartmentName
                        };
                        context.Department.AddOrUpdate(d => d.Name, dept);
                    }
                    var job = await context.Job.FindAsync(cr.JobName);

                    if (job == null)
                    {
                        context.Job.AddOrUpdate(j => j.Name, new Job {
                            Name = cr.JobName, Department = dept, DepartmentName = dept.Name
                        });
                    }
                    cr.MovieId    = entity.Id;
                    cr.CreditType = CreditType.Crew;
                    context.Credit.AddOrUpdate(c => c.CreditId, cr);
                }

                foreach (var ca in castCredit)
                {
                    ca.DepartmentName = "Actors";
                    var dept = await context.Department.FindAsync(ca.DepartmentName) as Department;

                    if (dept == null)
                    {
                        dept = new Department {
                            Name = ca.DepartmentName
                        };
                        context.Department.AddOrUpdate(d => d.Name, dept);
                    }
                    ca.JobName = "Actor";
                    var job = await context.Job.FindAsync(ca.JobName) as Job;

                    if (job == null)
                    {
                        context.Job.AddOrUpdate(j => j.Name, new Job {
                            Name = ca.JobName, Department = dept, DepartmentName = dept.Name
                        });
                    }
                    ca.MovieId    = entity.Id;
                    ca.CreditType = CreditType.Cast;
                    context.Credit.AddOrUpdate(c => c.CreditId, ca);
                }
            }
        }
Esempio n. 8
0
 /// <summary>
 /// Removes an entity from the underlying DbContext.
 /// </summary>
 /// <param name="entity">The entity that should be removed.</param>
 public virtual void Remove(T entity)
 {
     DataContextFactory.GetDataContext().Set <T>().Remove(entity);
 }
Esempio n. 9
0
 /// <summary>
 /// Adds an entity to the underlying DbContext.
 /// </summary>
 /// <param name="entity">The entity that should be added.</param>
 public virtual void Add(T entity)
 {
     DataContextFactory.GetDataContext().Set <T>().Add(entity);
 }
Esempio n. 10
0
 /// <summary>
 /// Removes an entity from the underlying DbContext.
 /// </summary>
 /// <param name="entity">The entity that should be removed.</param>
 public void Remove(Movie entity)
 {
     DataContextFactory.GetDataContext().Set <Movie>().Remove(entity);
 }
Esempio n. 11
0
 public void Undo()
 {
     DataContextFactory.Clear();
 }
Esempio n. 12
0
 public void Dispose()
 {
     DataContextFactory.GetDataContext().SaveChanges();
 }
Esempio n. 13
0
 /// <summary>
 /// Adds an entity to the underlying DbContext.
 /// </summary>
 /// <param name="entity">The entity that should be added.</param>
 public virtual void Add(User entity)
 {
     DataContextFactory.GetDataContext().Set <User>().Add(entity);
 }