Example #1
0
        /// <summary>
        /// Get List of Movie by search params
        /// </summary>
        /// <param name="q">match title</param>
        /// <param name="genre">match genre</param>
        /// <param name="year">match year</param>
        /// <param name="rating">match rating</param>
        /// <param name="actorId">match Actor ID</param>
        /// <param name="offset">page offset</param>
        /// <param name="limit">page size</param>
        /// <returns>List of Movie</returns>
        public Task <IEnumerable <Movie> > GetMoviesAsync(string q, string genre, int year = 0, double rating = 0.0, string actorId = "", int offset = 0, int limit = 100)
        {
            List <Movie> res  = new List <Movie>();
            int          skip = 0;
            bool         add  = false;

            // Use the year or Genre search if possible
            if ((year > 0 || !string.IsNullOrWhiteSpace(genre)) &&
                !(year > 0 && !string.IsNullOrWhiteSpace(genre)) &&
                string.IsNullOrWhiteSpace(q) &&
                rating == 0 &&
                string.IsNullOrWhiteSpace(actorId) &&
                offset == 0)
            {
                // search by year only
                if (year > 0)
                {
                    if (YearIndex.ContainsKey(year))
                    {
                        foreach (Movie m in YearIndex[year])
                        {
                            res.Add(m);

                            if (res.Count >= limit)
                            {
                                break;
                            }
                        }
                    }
                }

                // search by Genre only
                else if (!string.IsNullOrWhiteSpace(genre))
                {
                    genre = genre.ToLowerInvariant().Trim();

                    if (GenreIndex.ContainsKey(genre))
                    {
                        foreach (Movie m in GenreIndex[genre])
                        {
                            if (res.Count < limit)
                            {
                                res.Add(m);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
            }

            // search by other and/or multiple criteria
            else
            {
                foreach (Movie m in Movies)
                {
                    if ((string.IsNullOrWhiteSpace(q) || m.TextSearch.Contains(q, StringComparison.OrdinalIgnoreCase)) &&
                        (string.IsNullOrWhiteSpace(genre) || m.Genres.Contains(genre, StringComparer.OrdinalIgnoreCase)) &&
                        (year < 1 || m.Year == year) &&
                        (rating <= 0 || m.Rating >= rating))
                    {
                        add = true;

                        if (!string.IsNullOrWhiteSpace(actorId))
                        {
                            add = false;

                            actorId = actorId.Trim().ToLowerInvariant();

                            foreach (Role a in m.Roles)
                            {
                                if (a.ActorId == actorId)
                                {
                                    add = true;
                                    break;
                                }
                            }
                        }

                        if (add)
                        {
                            if (skip >= offset)
                            {
                                res.Add(m);

                                if (res.Count >= limit)
                                {
                                    break;
                                }
                            }
                            else
                            {
                                skip++;
                            }
                        }
                    }
                }
            }

            return(Task <IEnumerable <Movie> > .Factory.StartNew(() => { return res; }));
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="InMemoryDal"/> class.
        /// </summary>
        public InMemoryDal()
        {
            JsonSerializerOptions settings = new JsonSerializerOptions
            {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
            };

            if (Actors?.Count == null)
            {
                // load the data from the json file
                Actors = JsonSerializer.Deserialize <List <Actor> >(File.ReadAllText("data/actors.json"), settings);

                // sort by Name
                Actors.Sort(Actor.NameCompare);
            }

            if (ActorsIndex.Count == 0)
            {
                // Loads an O(1) dictionary for retrieving by ID
                // Could also use a binary search to reduce memory usage
                foreach (Actor a in Actors)
                {
                    ActorsIndex.Add(a.ActorId, a);
                }
            }

            if (Movies?.Count == null)
            {
                // load the data from the json file
                Movies = JsonSerializer.Deserialize <List <Movie> >(File.ReadAllText("data/movies.json"), settings);

                // sort by Title
                Movies.Sort(Movie.TitleCompare);
            }

            string ge;

            if (MoviesIndex.Count == 0)
            {
                foreach (Movie m in Movies)
                {
                    // Loads an O(1) dictionary for retrieving by ID
                    // Could also use a binary search to reduce memory usage
                    MoviesIndex.Add(m.MovieId, m);

                    // Add to by year dictionary
                    if (!YearIndex.ContainsKey(m.Year))
                    {
                        YearIndex.Add(m.Year, new List <Movie>());
                    }

                    YearIndex[m.Year].Add(m);

                    // Add to by Genre dictionary
                    foreach (string g in m.Genres)
                    {
                        ge = g.ToLowerInvariant().Trim();

                        if (!GenreIndex.ContainsKey(ge))
                        {
                            GenreIndex.Add(ge, new List <Movie>());
                        }

                        GenreIndex[ge].Add(m);
                    }
                }
            }

            if (Genres.Count == 0)
            {
                // load the data from the json file
                List <Genre> list = JsonSerializer.Deserialize <List <Genre> >(File.ReadAllText("data/genres.json"), settings);

                // Convert Genre object to List<string> per API spec
                foreach (Genre g in list)
                {
                    Genres.Add(g.Name);
                }

                Genres.Sort();
            }
        }
Example #3
0
        public InMemoryDal()
        {
            JsonSerializerSettings settings = new JsonSerializerSettings
            {
                ContractResolver = new DefaultContractResolver {
                    NamingStrategy = new CamelCaseNamingStrategy()
                },
            };

            // load the data from the json files
            Actors = JsonConvert.DeserializeObject <List <Actor> >(File.ReadAllText("data/actors.json"), settings);

            // sort by Name
            Actors.Sort(Actor.NameCompare);

            // Loads an O(1) dictionary for retrieving by ID
            // Could also use a binary search to reduce memory usage
            foreach (Actor a in Actors)
            {
                ActorsIndex.Add(a.ActorId, a);
            }

            Movies = JsonConvert.DeserializeObject <List <Movie> >(File.ReadAllText("data/movies.json"), settings);

            // sort by Title
            Movies.Sort(Movie.TitleCompare);

            string ge;

            foreach (Movie m in Movies)
            {
                // Loads an O(1) dictionary for retrieving by ID
                // Could also use a binary search to reduce memory usage
                MoviesIndex.Add(m.MovieId, m);

                // Create a dictionary by year
                if (!YearIndex.ContainsKey(m.Year))
                {
                    YearIndex.Add(m.Year, new List <Movie>());
                }

                YearIndex[m.Year].Add(m);

                // Create a dictionary by Genre
                foreach (string g in m.Genres)
                {
                    ge = g.ToLowerInvariant().Trim();

                    if (!GenreIndex.ContainsKey(ge))
                    {
                        GenreIndex.Add(ge, new List <Movie>());
                    }

                    GenreIndex[ge].Add(m);
                }
            }

            List <dynamic> list = JsonConvert.DeserializeObject <List <dynamic> >(File.ReadAllText("data/genres.json"), settings);

            // Convert Genre object to List<string> per API spec
            Genres = new List <string>();

            foreach (dynamic g in list)
            {
                Genres.Add(g["genre"].Value);
            }

            Genres.Sort();
        }