public IActionResult SearchByName(string pokemon) { string poke = TextCleaner.NormalInput(pokemon); PokemonRoot p = new PokemonRoot(); if (poke == null) { TempData["error"] = "Please enter a valid entry"; return(RedirectToAction("Index")); } try { p = pk.GetPokemon(poke); } catch (Exception e) { TempData["error"] = "Please enter a valid entry"; return(RedirectToAction("Index")); } TempData.Remove("moveerror"); TempData.Remove("error"); return(View(p)); }
//public IActionResult Add() //{ // return View(); //} public IActionResult Add(string pokemon) { string poke = pokemon.Trim().ToLower(); PokemonRoot p = pk.GetPokemon(poke);//Allows addition of other properties to the SQL table FavPokemon favPokemon = new FavPokemon(); favPokemon.Image = p.sprites.front_default; favPokemon.Type1 = p.types[0].type.name; if (p.types.Length < 2) { favPokemon.Type2 = ""; } else { favPokemon.Type2 = ", " + p.types[1].type.name; } string url = $@"https://pokeapi.co/api/v2/pokemon/{pokemon}/"; List <FavPokemon> favPokeList = _PokemonDB.FavPokemons.Where(x => x.UserId == User.FindFirst(ClaimTypes.NameIdentifier).Value).ToList(); if (favPokeList.Any(x => pokemon == x.Name)) { TempData["faverror"] = "This pokemon is already in your favorites"; return(RedirectToAction("Index")); } else { favPokemon.Name = pokemon; favPokemon.Url = url; favPokemon.UserId = User.FindFirst(ClaimTypes.NameIdentifier).Value; TempData.Remove("faverror"); _PokemonDB.FavPokemons.Add(favPokemon); _PokemonDB.SaveChanges(); return(RedirectToAction("Index")); } }