Example #1
0
        public async Task <Abstraction.Model.Movie> Merge(Abstraction.Model.Movie movie)
        {
            var movieNew = await movieStore.Merge(movie);

            await ResolveLocations(movieNew);
            await CreateSearchEntries(movieNew);

            return(movieNew);
        }
Example #2
0
        public async Task CanFind()
        {
            (await MovieService.Find(Guid.NewGuid())).Should().BeNull();

            var movie = new Abstraction.Model.Movie
            {
                Title               = Guid.NewGuid().ToString(),
                ReleaseYear         = 2018,
                Actors              = new List <Actor>(),
                Directors           = new List <Director>(),
                Distributors        = new List <Distributor>(),
                FilmingLocations    = new List <FilmingLocation>(),
                ProductionCompanies = new List <ProductionCompany>(),
                Writers             = new List <Writer>()
            };
            var merged = await MovieService.Merge(movie);

            (await MovieService.Find(merged.Key)).Should().NotBeNull();
        }
Example #3
0
        private Task CreateSearchEntries(Abstraction.Model.Movie movie)
        {
            var items = new List <SearchItem>
            {
                new SearchItem {
                    Text = movie.Title, Type = SearchItemType.Movie
                }
            };

            foreach (var actor in movie.Actors)
            {
                items.Add(new SearchItem {
                    Text = actor.FullName, Type = SearchItemType.Person
                });
            }
            foreach (var director in movie.Directors)
            {
                items.Add(new SearchItem {
                    Text = director.FullName, Type = SearchItemType.Person
                });
            }
            foreach (var writer in movie.Writers)
            {
                items.Add(new SearchItem {
                    Text = writer.FullName, Type = SearchItemType.Person
                });
            }

            foreach (var distributor in movie.Distributors)
            {
                items.Add(new SearchItem {
                    Text = distributor.Name, Type = SearchItemType.Organization
                });
            }
            foreach (var company in movie.ProductionCompanies)
            {
                items.Add(new SearchItem {
                    Text = company.Name, Type = SearchItemType.Organization
                });
            }

            return(searchService.Merge(movie.Key, items));
        }
Example #4
0
        public async Task CanFindSpecificMovieLocations()
        {
            var movie = new Abstraction.Model.Movie
            {
                Title            = Guid.NewGuid().ToString(),
                ReleaseYear      = 2018,
                Actors           = new List <Actor>(),
                Directors        = new List <Director>(),
                Distributors     = new List <Distributor>(),
                FilmingLocations = new[]
                {
                    new FilmingLocation {
                        Key = Guid.NewGuid(), AddressKey = (await GeocodingService.Geocode("Test address 1")).Key, FunFact = "Fun fact 1"
                    },
                    new FilmingLocation {
                        Key = Guid.NewGuid(), AddressKey = (await GeocodingService.Geocode("Test address 2")).Key, FunFact = "Fun fact 2"
                    }
                },
                ProductionCompanies = new List <ProductionCompany>(),
                Writers             = new List <Writer>()
            };

            var merged = await MovieService.Merge(movie);

            var locations = await FilmingLocationService.Find();

            var movieLocations = locations.Where(e => e.MovieKey == merged.Key).ToList();

            movieLocations.Should().HaveCount(2);
            movieLocations.Select(e => e.Key).Should().BeEquivalentTo(movie.FilmingLocations.Select(e => e.Key));
            movieLocations.Select(e => e.MovieKey).All(key => key != default(Guid)).Should().BeTrue();
            movieLocations.Select(e => e.MovieKey).Should().BeEquivalentTo(merged.FilmingLocations.Select(e => e.MovieKey));
            movieLocations.Select(e => e.AddressKey).Should().BeEquivalentTo(movie.FilmingLocations.Select(e => e.AddressKey));
            movieLocations.Select(e => e.Latitude).Should().BeEquivalentTo(new[] { 1.0, 1.0 });
            movieLocations.Select(e => e.Longitude).Should().BeEquivalentTo(new[] { 1.0, 1.0 });
            movieLocations.Select(e => e.FormattedAddress).Should().BeEquivalentTo(new[] { "Test address 1", "Test address 2" });
            movieLocations.Select(e => e.FunFact).Should().BeEquivalentTo(new[] { "Fun fact 1", "Fun fact 2" });
        }
Example #5
0
        public async Task CanMerge()
        {
            var movie = new Abstraction.Model.Movie
            {
                Title       = Guid.NewGuid().ToString(),
                ReleaseYear = 2018,
                Actors      = new[]
                {
                    new Actor {
                        FullName = "Test actor 1"
                    },
                    new Actor {
                        FullName = "Test actor 2"
                    }
                },
                Directors = new[]
                {
                    new Director {
                        FullName = "Test director 1"
                    },
                    new Director {
                        FullName = "Test director 2"
                    }
                },
                Distributors = new[]
                {
                    new Distributor {
                        Name = "Test distributor 1"
                    },
                    new Distributor {
                        Name = "Test distributor 2"
                    }
                },
                FilmingLocations = new[]
                {
                    new FilmingLocation {
                        Key = Guid.NewGuid(), AddressKey = (await GeocodingService.Geocode("Test address 1")).Key, FunFact = "Fun fact 1"
                    },
                    new FilmingLocation {
                        Key = Guid.NewGuid(), AddressKey = (await GeocodingService.Geocode("Test address 2")).Key, FunFact = "Fun fact 2"
                    }
                },
                ProductionCompanies = new[]
                {
                    new ProductionCompany {
                        Name = "Test production company 1"
                    },
                    new ProductionCompany {
                        Name = "Test production company 2"
                    }
                },
                Writers = new[]
                {
                    new Writer {
                        FullName = "Test writer 1"
                    },
                    new Writer {
                        FullName = "Test writer 2"
                    }
                }
            };

            var merged = await MovieService.Merge(movie);

            var found = await MovieService.Find(merged.Key);

            foreach (var m in new[] { merged, found })
            {
                m.Should().NotBeNull();
                m.Title.Should().Be(movie.Title);
                m.ReleaseYear.Should().Be(movie.ReleaseYear);

                m.Actors.Should().HaveCount(2);
                m.Actors.Select(e => e.Key).All(key => key != default(Guid)).Should().BeTrue();
                m.Actors.Select(e => e.FullName).Should().BeEquivalentTo(new[] { "Test actor 1", "Test actor 2" });

                m.Directors.Should().HaveCount(2);
                m.Directors.Select(e => e.Key).All(key => key != default(Guid)).Should().BeTrue();
                m.Directors.Select(e => e.FullName).Should().BeEquivalentTo(new[] { "Test director 1", "Test director 2" });

                m.Distributors.Should().HaveCount(2);
                m.Distributors.Select(e => e.Key).All(key => key != default(Guid)).Should().BeTrue();
                m.Distributors.Select(e => e.Name).Should().BeEquivalentTo(new[] { "Test distributor 1", "Test distributor 2" });

                m.FilmingLocations.Should().HaveCount(2);
                m.FilmingLocations.Select(e => e.Key).Should().BeEquivalentTo(movie.FilmingLocations.Select(e => e.Key));
                m.FilmingLocations.Select(e => e.MovieKey).All(key => key != default(Guid)).Should().BeTrue();
                m.FilmingLocations.Select(e => e.MovieKey).Should().BeEquivalentTo(merged.FilmingLocations.Select(e => e.MovieKey));
                m.FilmingLocations.Select(e => e.AddressKey).Should().BeEquivalentTo(movie.FilmingLocations.Select(e => e.AddressKey));
                m.FilmingLocations.Select(e => e.Latitude).Should().BeEquivalentTo(new[] { 1.0, 1.0 });
                m.FilmingLocations.Select(e => e.Longitude).Should().BeEquivalentTo(new[] { 1.0, 1.0 });
                m.FilmingLocations.Select(e => e.FormattedAddress).Should().BeEquivalentTo(new[] { "Test address 1", "Test address 2" });
                m.FilmingLocations.Select(e => e.FunFact).Should().BeEquivalentTo(new[] { "Fun fact 1", "Fun fact 2" });

                m.ProductionCompanies.Should().HaveCount(2);
                m.ProductionCompanies.Select(e => e.Key).All(key => key != default(Guid)).Should().BeTrue();
                m.ProductionCompanies.Select(e => e.Name).Should().BeEquivalentTo(new[] { "Test production company 1", "Test production company 2" });

                m.Writers.Should().HaveCount(2);
                m.Writers.Select(e => e.Key).All(key => key != default(Guid)).Should().BeTrue();
                m.Writers.Select(e => e.FullName).Should().BeEquivalentTo(new[] { "Test writer 1", "Test writer 2" });
            }
        }
Example #6
0
 private async Task ResolveLocations(Abstraction.Model.Movie movie)
 {
     await filmingLocationService.ResolveLocations(movie.FilmingLocations);
 }
Example #7
0
        public async Task <Abstraction.Model.Movie> Merge(Abstraction.Model.Movie movieNew)
        {
            var movieOld = await movieQuery.FirstOrDefaultAsync(e => e.Title == movieNew.Title && e.ReleaseYear == movieNew.ReleaseYear);

            if (movieOld == null)
            {
                movieOld = new Abstraction.Model.Movie
                {
                    Key                 = Guid.NewGuid(),
                    Title               = movieNew.Title,
                    ReleaseYear         = movieNew.ReleaseYear,
                    Actors              = new List <Actor>(),
                    Directors           = new List <Director>(),
                    Distributors        = new List <Distributor>(),
                    FilmingLocations    = new List <FilmingLocation>(),
                    ProductionCompanies = new List <ProductionCompany>(),
                    Writers             = new List <Writer>()
                };

                db.Insert(new Entity.Movie
                {
                    Key         = movieOld.Key,
                    Title       = movieOld.Title,
                    ReleaseYear = movieOld.ReleaseYear
                });
            }

            if (movieNew.Actors.Any())
            {
                var names          = movieNew.Actors.Select(e => e.FullName);
                var existingActors = await db.Actors.Where(e => names.Contains(e.FullName)).ToListAsync();

                foreach (var name in names)
                {
                    if (movieOld.Actors.Any(e => e.FullName == name))
                    {
                        continue;
                    }

                    var entity = existingActors.FirstOrDefault(e => e.FullName == name);

                    if (entity == null)
                    {
                        entity = new Actor
                        {
                            Key      = Guid.NewGuid(),
                            FullName = name
                        };
                        db.Insert(entity);
                    }

                    db.Insert(new Entity.MovieActor {
                        MovieKey = movieOld.Key, ActorKey = entity.Key
                    });
                }
            }

            if (movieNew.Directors.Any())
            {
                var names             = movieNew.Directors.Select(e => e.FullName);
                var existingDirectors = await db.Directors.Where(e => names.Contains(e.FullName)).ToListAsync();

                foreach (var name in names)
                {
                    if (movieOld.Directors.Any(e => e.FullName == name))
                    {
                        continue;
                    }

                    var entity = existingDirectors.FirstOrDefault(e => e.FullName == name);

                    if (entity == null)
                    {
                        entity = new Director
                        {
                            Key      = Guid.NewGuid(),
                            FullName = name
                        };
                        db.Insert(entity);
                    }

                    db.Insert(new Entity.MovieDirector {
                        MovieKey = movieOld.Key, DirectorKey = entity.Key
                    });
                }
            }

            if (movieNew.Distributors.Any())
            {
                var names = movieNew.Distributors.Select(e => e.Name);
                var existingDistributors = await db.Distributors.Where(e => names.Contains(e.Name)).ToListAsync();

                foreach (var name in names)
                {
                    if (movieOld.Distributors.Any(e => e.Name == name))
                    {
                        continue;
                    }

                    var entity = existingDistributors.FirstOrDefault(e => e.Name == name);

                    if (entity == null)
                    {
                        entity = new Distributor
                        {
                            Key  = Guid.NewGuid(),
                            Name = name
                        };
                        db.Insert(entity);
                    }

                    db.Insert(new Entity.MovieDistributor {
                        MovieKey = movieOld.Key, DistributorKey = entity.Key
                    });
                }
            }

            if (movieNew.FilmingLocations.Any())
            {
                // Addresses are in a geocoding module and they have to be pre-filled by the callee.
                if (movieNew.FilmingLocations.All(e => e.AddressKey == default(Guid)))
                {
                    throw new ArgumentException("The addresses don't have keys set.", nameof(movieNew.FilmingLocations));
                }

                foreach (var location in movieNew.FilmingLocations)
                {
                    if (movieOld.FilmingLocations.Any(e => e.AddressKey == location.AddressKey))
                    {
                        continue;
                    }

                    db.Insert(new Entity.FilmingLocation
                    {
                        Key        = location.Key,
                        MovieKey   = movieOld.Key,
                        AddressKey = location.AddressKey,
                        FunFact    = location.FunFact
                    });
                }
            }

            if (movieNew.ProductionCompanies.Any())
            {
                var names             = movieNew.ProductionCompanies.Select(e => e.Name);
                var existingCompanies = await db.ProductionCompanies.Where(e => names.Contains(e.Name)).ToListAsync();

                foreach (var name in names)
                {
                    if (movieOld.ProductionCompanies.Any(e => e.Name == name))
                    {
                        continue;
                    }

                    var entity = existingCompanies.FirstOrDefault(e => e.Name == name);

                    if (entity == null)
                    {
                        entity = new ProductionCompany
                        {
                            Key  = Guid.NewGuid(),
                            Name = name
                        };
                        db.Insert(entity);
                    }

                    db.Insert(new Entity.MovieProductionCompany {
                        MovieKey = movieOld.Key, ProductionCompanyKey = entity.Key
                    });
                }
            }

            if (movieNew.Writers.Any())
            {
                var names           = movieNew.Writers.Select(e => e.FullName);
                var existingWriters = await db.Writers.Where(e => names.Contains(e.FullName)).ToListAsync();

                foreach (var name in names)
                {
                    if (movieOld.Writers.Any(e => e.FullName == name))
                    {
                        continue;
                    }

                    var entity = existingWriters.FirstOrDefault(e => e.FullName == name);

                    if (entity == null)
                    {
                        entity = new Writer
                        {
                            Key      = Guid.NewGuid(),
                            FullName = name
                        };
                        db.Insert(entity);
                    }

                    db.Insert(new Entity.MovieWriter {
                        MovieKey = movieOld.Key, WriterKey = entity.Key
                    });
                }
            }

            await db.Commit();

            return(await Find(movieOld.Key));
        }