コード例 #1
0
ファイル: InMemoryDal.cs プロジェクト: retaildevcrews/ngsa
        /// <summary>
        /// Get a single actor by ID
        /// </summary>
        /// <param name="actorId">ID</param>
        /// <returns>Actor object</returns>
        public Actor GetActor(string actorId)
        {
            if (ActorsIndex.ContainsKey(actorId))
            {
                return(ActorsIndex[actorId]);
            }

            throw new CosmosException("Not Found", System.Net.HttpStatusCode.NotFound, 404, string.Empty, 0);
        }
コード例 #2
0
ファイル: InMemoryDal.cs プロジェクト: retaildevcrews/ngsa
        /// <summary>
        /// Get a single actor by ID
        /// </summary>
        /// <param name="actorId">ID</param>
        /// <returns>Actor object</returns>
        public async Task <Actor> GetActorAsync(string actorId)
        {
            return(await Task.Run(() =>
            {
                if (ActorsIndex.ContainsKey(actorId))
                {
                    return ActorsIndex[actorId];
                }

                throw new CosmosException("Not Found", System.Net.HttpStatusCode.NotFound, 404, string.Empty, 0);
            }).ConfigureAwait(false));
        }
コード例 #3
0
ファイル: InMemoryDal.cs プロジェクト: retaildevcrews/ngsa
        /// <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();
            }
        }
コード例 #4
0
ファイル: InMemoryDal.cs プロジェクト: retaildevcrews/ngsa
        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();
        }