public IHttpActionResult Post(Pokemon.Models.Pokemon pokemon)
 {
     using (ApplicationDbContext db = new ApplicationDbContext())
     {
         db.Pokemons.Add(pokemon);
         db.SaveChanges();
     }
     return Ok();
 }
 public IHttpActionResult PUT(Pokemon.Models.Pokemon pokemon)
 {
     using (ApplicationDbContext db = new ApplicationDbContext())
     {
         var currentPokemon = db.Pokemons.Find(pokemon.Id);
         currentPokemon.Name = pokemon.Name;
         currentPokemon.Type = pokemon.Type;
         db.SaveChanges();
     }
     return Ok();
 }
Exemplo n.º 3
0
        public async Task <IActionResult> Create([Bind("Id,Number,Name,url, GenderId,Generation,ColorId,HP,Attack,Special_Attack,Defense,Special_Defense,Speed")] Pokemon.Models.Pokemon pokemons, int[] poketype)
        {
            if (ModelState.IsValid)
            {
                _context.Add(pokemons);

                var check = _context.Pokemon.FirstOrDefault(w => w.Number == pokemons.Number);
                if (check == null)
                {
                    await _context.SaveChangesAsync();

                    var n = pokemons.Number;
                    if (n <= 151)
                    {
                        pokemons.Generation = 1;
                    }
                    else if (n <= 251)
                    {
                        pokemons.Generation = 2;
                    }
                    else if (n <= 386)
                    {
                        pokemons.Generation = 3;
                    }
                    else if (n <= 493)
                    {
                        pokemons.Generation = 4;
                    }
                    else if (n <= 649)
                    {
                        pokemons.Generation = 5;
                    }
                    else if (n <= 721)
                    {
                        pokemons.Generation = 6;
                    }
                    else if (n <= 809)
                    {
                        pokemons.Generation = 7;
                    }
                    else
                    {
                        pokemons.Generation = 8;
                    }

                    var number = "0";
                    if (pokemons.Number < 10)
                    {
                        number       = "00" + pokemons.Number;
                        pokemons.url = number + ".png";
                    }
                    else if (pokemons.Number < 100)
                    {
                        number       = "0" + pokemons.Number;
                        pokemons.url = number + ".png";
                    }
                    else
                    {
                        pokemons.url = pokemons.Number + ".png";
                    }

                    foreach (int pt in poketype)
                    {
                        _context.Add(new PokemonType {
                            PokeTypeId = pt, PokemonsId = pokemons.Id
                        });
                    }
                    await _context.SaveChangesAsync();
                }
                else
                {
                    //doe niets
                }

                return(RedirectToAction(nameof(Index)));
            }
            return(View(pokemons));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Number,Name,url,GenderId,Generation,ColorId,HP,Attack,Special_Attack,Defense,Special_Defense,Speed")] Pokemon.Models.Pokemon pokemons, int[] poketype)
        {
            if (id != pokemons.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    foreach (var pok in _context.PokemonType)
                    {
                        if (pok.PokemonsId == pokemons.Id)
                        {
                            _context.Update(pok);
                        }
                    }
                    foreach (int t in poketype)
                    {
                        PokemonType pok = _context.PokemonType.FirstOrDefault(x => x.PokeTypeId == t & x.PokemonsId == pokemons.Id);
                        if (pok != null)
                        {
                            _context.Update(pok);
                        }
                        else
                        {
                            _context.Add(new PokemonType {
                                PokeTypeId = t, PokemonsId = pokemons.Id
                            });
                        }
                    }

                    var n = pokemons.Number;
                    if (n <= 151)
                    {
                        pokemons.Generation = 1;
                    }
                    else if (n <= 251)
                    {
                        pokemons.Generation = 2;
                    }
                    else if (n <= 386)
                    {
                        pokemons.Generation = 3;
                    }
                    else if (n <= 493)
                    {
                        pokemons.Generation = 4;
                    }
                    else if (n <= 649)
                    {
                        pokemons.Generation = 5;
                    }
                    else if (n <= 721)
                    {
                        pokemons.Generation = 6;
                    }
                    else if (n <= 809)
                    {
                        pokemons.Generation = 7;
                    }
                    else
                    {
                        pokemons.Generation = 8;
                    }

                    var number = "0";
                    if (pokemons.Number < 10)
                    {
                        number       = "00" + pokemons.Number;
                        pokemons.url = number + ".png";
                    }
                    else if (pokemons.Number < 100)
                    {
                        number       = "0" + pokemons.Number;
                        pokemons.url = number + ".png";
                    }
                    else
                    {
                        pokemons.url = pokemons.Number + ".png";
                    }

                    _context.Update(pokemons);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!PokemonsExists(pokemons.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(pokemons));
        }