示例#1
0
        /// <summary>
        /// Retrieve from database and set the IsFavorite and HasBeenSeen properties of each movie in params, 
        /// </summary>
        /// <param name="movies">All movies to compute</param>
        public async Task ComputeMovieHistoryAsync(IEnumerable<MovieShort> movies)
        {
            await Task.Run(async () =>
            {
                var watch = Stopwatch.StartNew();

                using (var context = new ApplicationDbContext())
                {
                    await context.MovieHistory.LoadAsync();
                    var history = await context.MovieHistory.FirstOrDefaultAsync();
                    if (history == null)
                    {
                        await CreateMovieHistoryAsync();
                        history = await context.MovieHistory.FirstOrDefaultAsync();
                    }

                    foreach (var movie in movies.ToList())
                    {
                        var entityMovie = history.MoviesShort.FirstOrDefault(p => p.MovieId == movie.Id);
                        if (entityMovie == null) continue;
                        movie.IsFavorite = entityMovie.IsFavorite;
                        movie.HasBeenSeen = entityMovie.HasBeenSeen;
                    }
                }

                watch.Stop();
                var elapsedMs = watch.ElapsedMilliseconds;
                Logger.Debug(
                    $"ComputeMovieHistoryAsync in {elapsedMs} milliseconds.");
            });
        }
        /// <summary>
        /// Scaffold Settings table on database if empty
        /// </summary>
        public async Task CreateApplicationSettingsAsync()
        {
            var watch = Stopwatch.StartNew();

            var englishLanguage = new Entity.Localization.Language
            {
                LocalizedName = "English",
                EnglishName = "English",
                Culture = "en",
                IsCurrentLanguage = true
            };

            var frenchLanguage = new Entity.Localization.Language
            {
                LocalizedName = "Français",
                EnglishName = "French",
                Culture = "fr",
                IsCurrentLanguage = false
            };

            using (var context = new ApplicationDbContext())
            {
                await context.Settings.LoadAsync();
                var settings = await context.Settings.FirstOrDefaultAsync();
                if (settings == null)
                {
                    context.Settings.AddOrUpdate(new Entity.Application.Settings
                    {
                        CreatedOn = DateTime.Now,
                        Languages = new List<Entity.Localization.Language>
                        {
                            englishLanguage,
                            frenchLanguage
                        }
                    });
                }
                else if (settings.Languages == null)
                {
                    settings.Languages = new List<Entity.Localization.Language>
                    {
                        englishLanguage,
                        frenchLanguage
                    };
                }

                await context.SaveChangesAsync();
            }

            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;
            Logger.Debug(
                "CreateApplicationSettings in {0} milliseconds.", elapsedMs);
        }
示例#3
0
        /// <summary>
        /// Get all available languages from the database
        /// </summary>
        /// <returns>All available languages</returns>
        public async Task<ICollection<ILanguage>> GetAvailableLanguagesAsync()
        {
            ICollection<ILanguage> availableLanguages = null;
            await Task.Run(async () =>
            {
                var watch = Stopwatch.StartNew();
                using (var context = new ApplicationDbContext())
                {
                    await context.Settings.LoadAsync();
                    var applicationSettings = await context.Settings.FirstOrDefaultAsync();
                    if (applicationSettings == null)
                    {
                        await ApplicationService.CreateApplicationSettingsAsync();
                        applicationSettings = await context.Settings.FirstOrDefaultAsync();
                    }

                    var languages = applicationSettings.Languages;
                    availableLanguages = new List<ILanguage>();
                    foreach (var language in languages)
                    {
                        switch (language.Culture)
                        {
                            case "en":
                                availableLanguages.Add(new EnglishLanguage());
                                break;
                            case "fr":
                                availableLanguages.Add(new FrenchLanguage());
                                break;
                            default:
                                availableLanguages.Add(new EnglishLanguage());
                                break;
                        }
                    }
                }

                watch.Stop();
                var elapsedMs = watch.ElapsedMilliseconds;
                Logger.Debug(
                    "GetAvailableLanguagesAsync in {0} milliseconds.", elapsedMs);
            });

            return availableLanguages;
        }
        /// <summary>
        /// Get the favorites movies
        /// </summary>
        /// <param name="genre">The genre of the movies</param>
        /// <param name="ratingFilter">Used to filter by rating</param>
        /// <returns>Favorites movies</returns>
        public async Task<IEnumerable<MovieShort>> GetFavoritesMoviesAsync(MovieGenre genre, double ratingFilter)
        {
            var watch = Stopwatch.StartNew();

            var movies = new List<MovieShort>();

            try
            {
                using (var context = new ApplicationDbContext())
                {
                    var movieHistory = await context.MovieHistory.FirstOrDefaultAsync();
                    if (genre != null)
                    {
                        movies.AddRange(movieHistory.MoviesShort.Where(
                            p =>
                                p.IsFavorite && p.Genres.Any(g => g.Name == genre.EnglishName) &&
                                p.Rating >= ratingFilter)
                            .Select(MovieShortFromEntityToModel));
                    }
                    else
                    {
                        movies.AddRange(movieHistory.MoviesShort.Where(
                            p => p.IsFavorite)
                            .Select(MovieShortFromEntityToModel));
                    }
                }
            }
            catch (Exception exception)
            {
                Logger.Error(
                    $"GetFavoritesMoviesIdAsync: {exception.Message}");
            }
            finally
            {
                watch.Stop();
                var elapsedMs = watch.ElapsedMilliseconds;
                Logger.Debug(
                    $"GetFavoritesMoviesIdAsync in {elapsedMs} milliseconds.");
            }

            return movies;
        }
        /// <summary>
        /// Retrieve from database and set the IsFavorite and HasBeenSeen properties of each movie in params, 
        /// </summary>
        /// <param name="movies">All movies to compute</param>
        public async Task ComputeMovieHistoryAsync(IEnumerable<MovieShort> movies)
        {
            var watch = Stopwatch.StartNew();

            try
            {
                using (var context = new ApplicationDbContext())
                {
                    var history = await context.MovieHistory.FirstOrDefaultAsync();
                    if (history == null)
                    {
                        await CreateMovieHistoryAsync();
                        history = await context.MovieHistory.FirstOrDefaultAsync();
                    }

                    foreach (var movie in movies)
                    {
                        var entityMovie = history.MoviesShort.FirstOrDefault(p => p.MovieId == movie.Id);
                        if (entityMovie == null) continue;
                        movie.IsFavorite = entityMovie.IsFavorite;
                        movie.HasBeenSeen = entityMovie.HasBeenSeen;
                    }
                }
            }
            catch (Exception exception)
            {
                Logger.Error(
                    $"ComputeMovieHistoryAsync: {exception.Message}");
            }
            finally
            {
                watch.Stop();
                var elapsedMs = watch.ElapsedMilliseconds;
                Logger.Debug(
                    $"ComputeMovieHistoryAsync in {elapsedMs} milliseconds.");
            }
        }
示例#6
0
        /// <summary>
        /// Get the current language of the application
        /// </summary>
        /// <param name="language">Language to set</param>
        public async Task SetCurrentLanguageAsync(ILanguage language)
        {
            await Task.Run(async () =>
            {
                var watch = Stopwatch.StartNew();

                using (var context = new ApplicationDbContext())
                {
                    await context.Settings.LoadAsync();
                    var applicationSettings = await context.Settings.FirstOrDefaultAsync();
                    if (applicationSettings == null)
                    {
                        await ApplicationService.CreateApplicationSettingsAsync();
                        applicationSettings = await context.Settings.FirstOrDefaultAsync();
                    }

                    var currentLanguage = applicationSettings.Languages.First(a => a.Culture == language.Culture);
                    currentLanguage.IsCurrentLanguage = true;
                    foreach (var lang in applicationSettings.Languages.Where(a => a.Culture != language.Culture))
                    {
                        lang.IsCurrentLanguage = false;
                    }

                    context.Settings.AddOrUpdate(applicationSettings);
                    await context.SaveChangesAsync();
                    ChangeLanguage(language);
                }

                watch.Stop();
                var elapsedMs = watch.ElapsedMilliseconds;
                Logger.Debug(
                    "SetCurrentLanguageAsync ({0}) in {1} milliseconds.", language.LocalizedName, elapsedMs);
            });
        }
示例#7
0
        /// <summary>
        /// Get the current language of the application
        /// </summary>
        /// <returns>Current language</returns>
        public async Task<ILanguage> GetCurrentLanguageAsync()
        {
            ILanguage currentLanguage = null;
            await Task.Run(async () =>
            {
                var watch = Stopwatch.StartNew();
                using (var context = new ApplicationDbContext())
                {
                    await context.Settings.LoadAsync();
                    var applicationSettings = await context.Settings.FirstOrDefaultAsync();
                    if (applicationSettings == null)
                    {
                        await ApplicationService.CreateApplicationSettingsAsync();
                        applicationSettings = await context.Settings.FirstOrDefaultAsync();
                    }

                    var language = applicationSettings.Languages.FirstOrDefault(a => a.IsCurrentLanguage);
                    if (language != null)
                    {
                        switch (language.Culture)
                        {
                            case "en":
                                currentLanguage = new EnglishLanguage();
                                break;
                            case "fr":
                                currentLanguage = new FrenchLanguage();
                                break;
                            default:
                                currentLanguage = new EnglishLanguage();
                                break;
                        }
                    }
                }

                watch.Stop();
                var elapsedMs = watch.ElapsedMilliseconds;
                Logger.Debug(
                    "GetCurrentLanguageAsync in {0} milliseconds.", elapsedMs);
            });

            return currentLanguage;
        }
示例#8
0
        /// <summary>
        /// Get the favorites movies
        /// </summary>
        /// <returns>Favorites movies</returns>
        public async Task<IEnumerable<MovieShort>> GetFavoritesMoviesAsync(MovieGenre genre = null)
        {
            var watch = Stopwatch.StartNew();

            var movies = new List<MovieShort>();
            await Task.Run(async () =>
            {
                using (var context = new ApplicationDbContext())
                {
                    await context.MovieHistory.LoadAsync();
                    var movieHistory = await context.MovieHistory.FirstOrDefaultAsync();
                    if (genre != null)
                    {
                        movies.AddRange(movieHistory.MoviesShort.Where(
                            p => p.IsFavorite && p.Genres.Any(g => g.Name == genre.EnglishName))
                            .Select(MovieShortFromEntityToModel));
                    }
                    else
                    {
                        movies.AddRange(movieHistory.MoviesShort.Where(
                            p => p.IsFavorite)
                            .Select(MovieShortFromEntityToModel));
                    }
                }
            });

            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;
            Logger.Debug(
                $"GetFavoritesMoviesIdAsync in {elapsedMs} milliseconds.");
            return movies;
        }
示例#9
0
        /// <summary>
        /// Scaffold UserData Table on database if empty
        /// </summary>
        private static async Task CreateMovieHistoryAsync()
        {
            using (var context = new ApplicationDbContext())
            {
                var watch = Stopwatch.StartNew();

                await context.MovieHistory.LoadAsync();
                var userData = await context.MovieHistory.FirstOrDefaultAsync();
                if (userData == null)
                {
                    context.MovieHistory.AddOrUpdate(new MovieHistory
                    {
                        Created = DateTime.Now,
                        MoviesShort = new List<Entity.Movie.MovieShort>(),
                        MoviesFull = new List<Entity.Movie.MovieFull>()
                    });

                    await context.SaveChangesAsync();
                }

                watch.Stop();
                var elapsedMs = watch.ElapsedMilliseconds;
                Logger.Debug(
                    $"CreateMovieHistoryAsync in {elapsedMs} milliseconds.");
            }
        }
示例#10
0
        /// <summary>
        /// Set a movie as seen
        /// </summary>
        /// <param name="movie">Seen movie</param>
        public async Task SetHasBeenSeenMovieAsync(MovieFull movie)
        {
            await Task.Run(async () =>
            {
                var watch = Stopwatch.StartNew();

                using (var context = new ApplicationDbContext())
                {
                    await context.MovieHistory.LoadAsync();
                    var movieHistory = await context.MovieHistory.FirstOrDefaultAsync();
                    if (movieHistory == null)
                    {
                        await CreateMovieHistoryAsync();
                        movieHistory = await context.MovieHistory.FirstOrDefaultAsync();
                    }

                    if (movieHistory.MoviesFull == null)
                    {
                        movieHistory.MoviesFull = new List<Entity.Movie.MovieFull>
                        {
                            MovieFullFromModelToEntity(movie)
                        };

                        context.MovieHistory.AddOrUpdate(movieHistory);
                    }
                    else
                    {
                        var movieFull = movieHistory.MoviesFull.FirstOrDefault(p => p.MovieId == movie.Id);
                        if (movieFull == null)
                        {
                            movieHistory.MoviesFull.Add(MovieFullFromModelToEntity(movie));
                        }
                        else
                        {
                            movieFull.HasBeenSeen = movie.HasBeenSeen;
                        }
                    }

                    await context.SaveChangesAsync();
                }

                watch.Stop();
                var elapsedMs = watch.ElapsedMilliseconds;
                Logger.Debug(
                    $"SetHasBeenSeenMovieAsync ({movie.ImdbCode}) in {elapsedMs} milliseconds.");
            });
        }
        /// <summary>
        /// Set the movie as favorite
        /// </summary>
        /// <param name="movie">Favorite movie</param>
        public async Task SetFavoriteMovieAsync(MovieShort movie)
        {
            var watch = Stopwatch.StartNew();

            try
            {
                using (var context = new ApplicationDbContext())
                {
                    var movieHistory = await context.MovieHistory.FirstOrDefaultAsync();
                    if (movieHistory == null)
                    {
                        await CreateMovieHistoryAsync();
                        movieHistory = await context.MovieHistory.FirstOrDefaultAsync();
                    }

                    if (movieHistory.MoviesShort == null)
                    {
                        movieHistory.MoviesShort = new List<Entity.Movie.MovieShort>
                        {
                            MovieShortFromModelToEntity(movie)
                        };

                        context.MovieHistory.AddOrUpdate(movieHistory);
                    }
                    else
                    {
                        var movieShort = movieHistory.MoviesShort.FirstOrDefault(p => p.MovieId == movie.Id);
                        if (movieShort == null)
                        {
                            movieHistory.MoviesShort.Add(MovieShortFromModelToEntity(movie));
                        }
                        else
                        {
                            movieShort.IsFavorite = movie.IsFavorite;
                        }
                    }

                    await context.SaveChangesAsync();
                }
            }
            catch (Exception exception)
            {
                Logger.Error(
                    $"SetFavoriteMovieAsync: {exception.Message}");
            }
            finally
            {
                watch.Stop();
                var elapsedMs = watch.ElapsedMilliseconds;
                Logger.Debug(
                    $"SetFavoriteMovieAsync ({movie.ImdbCode}) in {elapsedMs} milliseconds.");
            }
        }
示例#12
0
        /// <summary>
        /// Get the favorites movies
        /// </summary>
        /// <returns>Favorites movies</returns>
        public async Task<IEnumerable<MovieShort>> GetFavoritesMoviesAsync()
        {
            var watch = Stopwatch.StartNew();

            var movies = new List<MovieShort>();
            await Task.Run(async () =>
            {
                using (var context = new ApplicationDbContext())
                {
                    await context.MovieHistory.LoadAsync();
                    var movieHistory = await context.MovieHistory.FirstOrDefaultAsync();
                    foreach (var movie in movieHistory.MoviesShort.Where(p => p.IsFavorite))
                    {
                        movies.Add(MovieShortFromEntityToModel(movie));
                    }
                }
            });

            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;
            Logger.Debug(
                $"GetFavoritesMoviesIdAsync in {elapsedMs} milliseconds.");
            return movies;
        }
示例#13
0
        /// <summary>
        /// Set the current language of the application
        /// </summary>
        /// <param name="language">Language to set</param>
        public async Task SetCurrentLanguageAsync(ILanguage language)
        {
            if (language == null) throw new ArgumentNullException(nameof(language));
            var watch = Stopwatch.StartNew();

            using (var context = new ApplicationDbContext())
            {
                var applicationSettings = await context.Settings.FirstOrDefaultAsync();
                if (applicationSettings == null)
                {
                    await _settingsService.CreateApplicationSettingsAsync();
                    applicationSettings = await context.Settings.FirstOrDefaultAsync();
                }

                var currentLanguage = applicationSettings.Languages.First(a => a.Culture == language.Culture);
                currentLanguage.IsCurrentLanguage = true;
                foreach (var lang in applicationSettings.Languages.Where(a => a.Culture != language.Culture))
                    lang.IsCurrentLanguage = false;

                context.Settings.AddOrUpdate(applicationSettings);
                await context.SaveChangesAsync();
                ChangeLanguage(language);
            }

            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;
            Logger.Debug(
                $"SetCurrentLanguageAsync ({language.LocalizedName}) in {elapsedMs} milliseconds.");
        }