コード例 #1
0
        public static void InsertMultiplePokemons()
        {
            var pokemon1 = new Pokemon
            {
                Name      = "Eevee",
                Legendary = false,
                SessionId = 1
            };

            var pokemon2 = new Pokemon
            {
                Name      = "Onix",
                Legendary = false,
                SessionId = 1
            };

            using (var context = new PokemonContext())
            {
                context.Database.Log = Console.WriteLine;
                context.Pokemons.AddRange(new List <Pokemon> {
                    pokemon1, pokemon2
                });
                context.SaveChanges();
            }
        }
コード例 #2
0
        public static ApplicationUserManager Create(IdentityFactoryOptions <ApplicationUserManager> options, IOwinContext context)
        {
            PokemonContext         db      = context.Get <PokemonContext>();
            ApplicationUserManager manager = new ApplicationUserManager(new UserStore <User>(db));

            return(manager);
        }
コード例 #3
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.EnvironmentName == "Development")
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseStatusCodePagesWithReExecute("/api/error");
            app.UseExceptionHandler("/api/error");
            app.UseMvc();

            var opts = new DbContextOptionsBuilder <PokemonContext>()
                       .UseSqlite(_inMemorySqlite)
                       .Options;

            //TODO: Move to service class. Error handling, logging.
            var path  = Path.Combine(env.WebRootPath, "pokemon");
            var files = Directory.GetFiles(path);

            using (var context = new PokemonContext(opts))
            {
                // Create the schema in the database
                var mapper = GetMapper();
                context.Database.EnsureCreated();
                foreach (var file in files)
                {
                    var jsonString = File.ReadAllText(file);
                    var pokemonDto = JsonConvert.DeserializeObject <PokemonDto>(jsonString);
                    var pokemon    = mapper.Map(pokemonDto, new Core.Entities.Pokemon());
                    context.Pokemon.Add(mapper.Map(pokemonDto, pokemon));
                }

                context.SaveChanges();
            }
        }
コード例 #4
0
        public PokemonQuery(IPokemonRepository pokemonRepository)
        {
            Field <ListGraphType <PokemonType> >(
                "AllPokemons",
                resolve: context => pokemonRepository.GetPokemons()
                );

            Field <PokemonType>(
                "PokemonById",
                arguments: new QueryArguments(new QueryArgument <IntGraphType> {
                Name = "id"
            }),
                resolve: context => pokemonRepository.GetPokemonById((context.GetArgument <int>("id")))
                );

            Field <PaginationType>(
                "PokemonPagination",
                arguments: new QueryArguments(
                    new QueryArgument <IntGraphType> {
                Name = "first", DefaultValue = 0
            },
                    new QueryArgument <IntGraphType> {
                Name = "after", DefaultValue = 0
            },
                    new QueryArgument <IntGraphType> {
                Name = "countPerPage", DefaultValue = 0
            },
                    new QueryArgument <IntGraphType> {
                Name = "currentPage", DefaultValue = 0
            },
                    new QueryArgument <StringGraphType> {
                Name = "sortBy"
            }
                    ),
                resolve: context =>
            {
                int totalCount;
                int totalPage;
                var temp = context.GetArgument <int>("countPerPage");
                using (var ctx = new PokemonContext())
                {
                    totalCount = ctx.PokemonData.Count();
                    if (context.GetArgument <int>("countPerPage") != 0 && context.GetArgument <int>("first") != 0)
                    {
                        totalPage = (int)Math.Ceiling((double)context.GetArgument <int>("first") / temp);
                    }
                    else if (context.GetArgument <int>("countPerPage") != 0)
                    {
                        totalPage = (int)Math.Ceiling((double)ctx.PokemonData.Count() / temp);
                    }
                    else
                    {
                        totalPage = 0;
                    }
                }
                return(new Pagination(totalCount, context.GetArgument <int>("first"), context.GetArgument <int>("after"), context.GetArgument <int>("countPerPage"), context.GetArgument <int>("currentPage"), totalPage, context.GetArgument <string>("sortBy")));
            }
                );
        }
コード例 #5
0
        /// <summary>
        /// Deletes the specified pokemon.
        /// </summary>
        /// <returns>The deleted Pokemon</returns>
        public async Task <Pokemon> RemovePokemon([Service] PokemonContext pokemonContext, int id)
        {
            var pokemon = pokemonContext.Pokemon.Find(id);

            pokemonContext.Pokemon.Remove(pokemon);

            await pokemonContext.SaveChangesAsync();

            return(pokemon);
        }
コード例 #6
0
 public static void ProjectionQuery()
 {
     using (var contex = new PokemonContext())
     {
         contex.Database.Log = Console.WriteLine;
         var pokemon = contex.Pokemons
                       .Select(n => new { n.Name, n.Legendary })
                       .ToList();
     }
 }
コード例 #7
0
ファイル: PokemonClient.cs プロジェクト: charanpasham/Pokemon
 public PokemonClient(IConfiguration configuration, PokemonContext pokemonContext)
 {
     _context      = pokemonContext;
     PokemonApiUrl = configuration["PokemonAPI"];
     _httpClient   = new HttpClient
     {
         BaseAddress = new Uri(PokemonApiUrl)
     };
     _httpClient.DefaultRequestHeaders.Accept.Clear();
     _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
 }
コード例 #8
0
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, PokemonContext context)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseGraphiQl();
            app.UseMvc();
            context.Database.Migrate();
        }
コード例 #9
0
        /// <summary>
        /// Updates the pokemon with the specified id
        /// </summary>
        /// <param name="pokemonContext">The pokemon DB context.</param>
        /// <returns>All pokemon from the context.</returns>
        public async Task <Pokemon> UpdatePokemon([Service] PokemonContext pokemonContext, int id, string name, string type)
        {
            var pokemon = pokemonContext.Pokemon.Find(id);

            pokemon.name = name;
            pokemon.type = type;

            await pokemonContext.SaveChangesAsync();

            pokemon = pokemonContext.Pokemon.Find(id);//retrieve the newly created pokemon
            return(pokemon);
        }
コード例 #10
0
        public static void SimplePokemonQueries()
        {
            using (var contex = new PokemonContext())
            {
                var pokemons = contex.Pokemons.
                               Where(n => n.Name == "Eevee")
                               .OrderBy(n => n.Name);

                foreach (var pokemon in pokemons)
                {
                    Console.WriteLine(pokemon.Name);
                }
            }
        }
コード例 #11
0
        public static void InsertSession()
        {
            var session = new Session
            {
                SessionName = SessionNameEnum.Hoenn
            };

            using (var context = new PokemonContext())
            {
                context.Database.Log = Console.WriteLine;
                context.Sessions.Add(session);
                context.SaveChanges();
            }
        }
コード例 #12
0
        /// <summary>
        /// Adds a new pokemon.
        /// </summary>
        /// <returns>A newly created pokemon.</returns>
        public async Task <Pokemon> Pokemon([Service] PokemonContext pokemonContext, string name, string type)
        {
            var pokemon = new Pokemon()
            {
                id   = pokemonContext.Pokemon.Select(p => p.id).Max() + 1,
                name = name,
                type = type
            };

            pokemonContext.Pokemon.Add(pokemon);

            await pokemonContext.SaveChangesAsync();

            return(pokemon);
        }
コード例 #13
0
ファイル: Startup.cs プロジェクト: Joerit/CA2018-REST
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, PokemonContext pokeContext)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseCors(builder => builder
                        .WithOrigins("http://localhost")
                        .AllowAnyHeader());

            app.UseMvc();

            PokeDBInitializer.Initialize(pokeContext);
        }
コード例 #14
0
        public static void SimplePokemonGraphQuery()
        {
            using (var contex = new PokemonContext())
            {
                /*
                 * var pokemons = contex.Pokemons.Include(n => n.Sessions);
                 */

                var pokemon = contex.Pokemons
                              .FirstOrDefault(n => n.Name.StartsWith("Eevee"));

                /*
                 * context.Entry(pokemon).Collection(n => n.Sessions.Count());
                 */
            }
        }
コード例 #15
0
        public static void InsertPokemon(string name, bool legendary, string types)
        {
            var pokemon = new Pokemon
            {
                Name       = name,
                Legendary  = legendary,
                TypeString = types
            };

            using (var context = new PokemonContext())
            {
                context.Database.Log = Console.WriteLine;
                context.Pokemons.Add(pokemon);
                context.SaveChanges();
            }
        }
コード例 #16
0
     public PaginationQuery(IPokemonRepository pokemonRepository)
     {
         Field<PaginationType>(
             "PokemonPagination",
             arguments: new QueryArguments(new QueryArgument<IntGraphType> { Name = "first" }), new QueryArguments(new QueryArgument<IntGraphType> { Name = "last" }),
             resolve: context =>
                 {
                     int totalCount;
                     using (var ctx = new PokemonContext())
                     {
                         totalCount = ctx.PokemonData.Count();
                     }
                     return new Pagination(totalCount, context.GetArgument<int>("first"), context.GetArgument<int>("last"));
                 }
             );
 }
コード例 #17
0
        private static async Task <Pokedata> CreatePokemon(Task <string> fromUrl)
        {
            Pokedata pokeTask = await fromUrl.ContinueWith(s =>
            {
                PokemonContext db = new PokemonContext();
                JToken p          = JToken.Parse(s.Result);
                Pokedata poke     = new Pokedata
                {
                    PokemonId    = (int)p["id"],
                    DefaultImage = (string)p["sprites"]["front_default"],
                    Name         = (string)p["name"],
                    Height       = (int)p["height"],
                    Weight       = (int)p["weight"],

                    /*
                     * Moves = new JArray(p["moves"].Children()).Select(m => new Move  // May want to collect this from DBSet directly to collect existing entity. May cause duplication.
                     * {
                     *  MoveId = Utility.URLIndex((string)m["move"]["url"], true),
                     *  Name = (string)m["move"]["name"]
                     * }).ToList(),
                     *
                     * Abilities = new JArray(p["abilities"].Children()).Select(a => new Ability
                     * {
                     *  AbilityId = Utility.URLIndex((string)a["ability"]["url"], true),
                     *  Name = (string)a["ability"]["name"]
                     * }).ToList(),
                     */

                    Moves = new JArray(p["moves"].Children()).Select(m =>
                                                                     (Move)DBAttribute(db.Moves, m["move"])
                                                                     ).ToList(),
                    Abilities = new JArray(p["abilities"].Children()).Select(a =>
                                                                             (Ability)DBAttribute(db.Abilities, a["ability"])
                                                                             ).ToList(),
                };

                db.Dispose();
                return(poke);
            }, TaskScheduler.Default);

            return(pokeTask);
        }
コード例 #18
0
        private PokemonContext AddTypesToPokemon(PokemonContext context)
        {
            foreach (var pokemon in context.Pokemon)
            {
                IQueryable <PokemonType> pokemonTypesQuery = from pokeType in context.PokemonType
                                                             where pokeType.PokemonId == pokemon.Id
                                                             select pokeType;

                List <PokemonType> pokemonTypes = pokemonTypesQuery.ToList();
                foreach (PokemonType pokemonType in pokemonTypes)
                {
                    pokemonType.Type = (from type in context.Type
                                        where type.Id == pokemonType.TypeId
                                        select type).Single();
                    if (null == pokemon.Types)
                    {
                        pokemon.Types = new List <Models.Type>();
                    }
                    pokemon.Types.Add(pokemonType.Type);
                }
            }
            return(context);
        }
コード例 #19
0
 public ExploreController(PokemonContext context)
 {
     pokemonContext = context;
     random         = new Random();
 }
コード例 #20
0
 public HomeController(ILogger <HomeController> logger, PokemonContext context)
 {
     _logger  = logger;
     _context = context;
 }
コード例 #21
0
 public PokemonController(PokemonContext context, IHostingEnvironment hostEnvironment)
 {
     _context         = context;
     _hostEnvironment = hostEnvironment;
 }
コード例 #22
0
 public HomeController(PokemonContext pokemonContext)
 {
     _context = pokemonContext;
 }
コード例 #23
0
 public AccountController(PokemonContext context)
 {
     pokemonContext = context;
 }
コード例 #24
0
 public PokemonRepository(PokemonContext pokemonContext)
 {
     this.pokemonContext = pokemonContext;
 }
コード例 #25
0
 public HomeController(PokemonContext context, UserContext userContext, ILogger <HomeController> logger)
 {
     _pokemonContext = AddTypesToPokemon(context);
     _userContext    = userContext;
     _logger         = logger;
 }
コード例 #26
0
 public PokemonController(PokemonContext c)
 {
     _context = c;
 }
コード例 #27
0
 public UserRepository(PokemonContext context)
 {
     this.context = context;
 }
コード例 #28
0
 public EntrenadorController(PokemonContext c)
 {
     _context = c;
 }
コード例 #29
0
 public ChatListViewComponent(PokemonContext context)
 {
     _pokemonContext = context;
 }
コード例 #30
0
 public PokemonController(PokemonContext context)
 {
     _context = context;
 }