Пример #1
0
        /// <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;
        }
Пример #2
0
        /// <summary>
        /// Search movies by criteria
        /// </summary>
        /// <param name="criteria">Criteria used for search</param>
        /// <param name="page">Page to return</param>
        /// <param name="limit">The maximum number of movies to return</param>
        /// <param name="genre">The genre to filter</param>
        /// <param name="ratingFilter">Used to filter by rating</param>
        /// <param name="ct">Cancellation token</param>
        /// <returns>Searched movies and the number of movies found</returns>
        public async Task<Tuple<IEnumerable<MovieShort>, int>> SearchMoviesAsync(string criteria,
            int page,
            int limit,
            MovieGenre genre,
            double ratingFilter,
            CancellationToken ct)
        {
            var watch = Stopwatch.StartNew();

            var wrapper = new WrapperMovieShort();

            if (limit < 1 || limit > 50)
                limit = 20;

            if (page < 1)
                page = 1;

            var restClient = new RestClient(Constants.YtsApiEndpoint);
            var request = new RestRequest("/{segment}", Method.GET);
            request.AddUrlSegment("segment", "list_movies.json");
            request.AddParameter("limit", limit);
            request.AddParameter("page", page);
            if (genre != null) request.AddParameter("genre", genre.EnglishName);
            request.AddParameter("minimum_rating", ratingFilter);
            request.AddParameter("query_term", criteria);

            try
            {
                var response = await restClient.ExecuteGetTaskAsync<WrapperMovieShort>(request, ct);
                if (response.ErrorException != null)
                    throw response.ErrorException;

                wrapper = response.Data;
            }
            catch (Exception exception) when (exception is TaskCanceledException)
            {
                Logger.Debug(
                    "SearchMoviesAsync cancelled.");
            }
            catch (Exception exception)
            {
                Logger.Error(
                    $"SearchMoviesAsync: {exception.Message}");
                throw;
            }
            finally
            {
                watch.Stop();
                var elapsedMs = watch.ElapsedMilliseconds;
                Logger.Debug(
                    $"SearchMoviesAsync ({criteria}, {page}, {limit}) in {elapsedMs} milliseconds.");
            }

            var movies = GetMoviesListFromWrapper(wrapper) ?? new List<MovieShort>();
            var nbMovies = wrapper?.Data?.MovieCount ?? 0;

            return new Tuple<IEnumerable<MovieShort>, int>(movies, nbMovies);
        }