Exemplo n.º 1
0
        public List <Movie> AddMovies(List <Movie> newMovies)
        {
            newMovies.ForEach(m => Ensure.That(m, () => m).IsNotNull());

            newMovies.ForEach(m =>
            {
                MoviePathState defaultState = MoviePathState.Static;
                if (!_configService.PathsDefaultStatic)
                {
                    defaultState = MoviePathState.Dynamic;
                }
                if (string.IsNullOrWhiteSpace(m.Path))
                {
                    var folderName = _fileNameBuilder.GetMovieFolder(m);
                    m.Path         = Path.Combine(m.RootFolderPath, folderName);
                    m.PathState    = defaultState;
                }
                else
                {
                    m.PathState = defaultState == MoviePathState.Dynamic ? MoviePathState.StaticOnce : MoviePathState.Static;
                }

                m.CleanTitle = m.Title.CleanSeriesTitle();
                m.SortTitle  = MovieTitleNormalizer.Normalize(m.Title, m.TmdbId);
                m.Added      = DateTime.UtcNow;
            });

            var existingMovies      = GetAllMovies();
            var potentialMovieCount = newMovies.Count;

            newMovies = newMovies.DistinctBy(movie => movie.TmdbId).ToList();                                                       // Ensure we don't add the same movie twice

            newMovies = newMovies.ExceptBy(n => n.TmdbId, existingMovies, e => e.TmdbId, EqualityComparer <int> .Default).ToList(); // Ensure we don't add a movie that already exists

            _movieRepository.InsertMany(newMovies);

            _logger.Debug("Adding {0} movies, {1} duplicates detected and skipped", newMovies.Count, potentialMovieCount - newMovies.Count);

            newMovies.ForEach(m =>
            {
                _eventAggregator.PublishEvent(new MovieAddedEvent(m));
            });

            return(newMovies);
        }
Exemplo n.º 2
0
        private Movie SetPropertiesAndValidate(Movie newMovie)
        {
            if (string.IsNullOrWhiteSpace(newMovie.Path))
            {
                var folderName = _fileNameBuilder.GetMovieFolder(newMovie);
                newMovie.Path = Path.Combine(newMovie.RootFolderPath, folderName);
            }

            newMovie.CleanTitle = newMovie.Title.CleanMovieTitle();
            newMovie.SortTitle  = MovieTitleNormalizer.Normalize(newMovie.Title, newMovie.TmdbId);
            newMovie.Added      = DateTime.UtcNow;

            var validationResult = _addMovieValidator.Validate(newMovie);

            if (!validationResult.IsValid)
            {
                throw new ValidationException(validationResult.Errors);
            }

            return(newMovie);
        }