public int Update(UpdatePokemonDto dto) { var pokemon = _dbContext.Pokemon.FirstOrDefault(o => o.PokemonID == dto.PokemonID); if (pokemon == null) { throw new KeyNotFoundException($"Pokemon with ID: {dto.PokemonID} does not exist."); } if (dto.SpeciesID == null) { pokemon.PokemonName = dto.PokemonName; _dbContext.SaveChanges(); return(pokemon.PokemonID); } else { if (_dbContext.PokemonSpecies.Any(o => o.SpeciesID == dto.SpeciesID.Value)) { pokemon.PokemonName = dto.PokemonName; pokemon.SpeciesID = dto.SpeciesID.Value; _dbContext.SaveChanges(); return(pokemon.PokemonID); } throw new KeyNotFoundException($"Species with ID: {dto.SpeciesID} does not exist."); } }
public async Task <ServiceResponse <GetPokemonDto> > UpdatePokemon(UpdatePokemonDto updatePokemon) { ServiceResponse <GetPokemonDto> serviceResponse = new ServiceResponse <GetPokemonDto>(); Pokemon pokemon = await _context.Pokemons.FirstOrDefaultAsync(p => p.Id == updatePokemon.Id); try { if (pokemon.Id == updatePokemon.Id) { pokemon.Name = updatePokemon.Name; pokemon.Attack = updatePokemon.Attack; pokemon.Defense = updatePokemon.Defense; pokemon.PokemonClass = pokemon.PokemonClass; _context.Pokemons.Update(pokemon); await _context.SaveChangesAsync(); serviceResponse.Message = "Pokemon updated"; serviceResponse.Data = _mapper.Map <GetPokemonDto>(pokemon); } } catch { serviceResponse.Success = false; serviceResponse.Message = "Pokemon not found"; } return(serviceResponse); }
public async Task <IActionResult> UpdatePokemon(UpdatePokemonDto updatePokemon) { ServiceResponse <GetPokemonDto> response = await _pokemonService.UpdatePokemon(updatePokemon); if (response.Data == null) { return(NotFound(response)); } return(Ok(response)); }
public IActionResult UpdatePokemon([Required][FromBody] UpdatePokemonDto updateDto) { try { var response = _repo.Update(updateDto); return(StatusCode((int)HttpStatusCode.OK, JsonConvert.SerializeObject($"/Pokedex/pokemon/{response}"))); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public async Task <ServiceResponse <GetPokemonDto> > UpdatePokemon(UpdatePokemonDto updatedPokemon) { ServiceResponse <GetPokemonDto> serviceResponse = new ServiceResponse <GetPokemonDto>(); try { Pokemon pokemon = await _context.Pokemons.FirstOrDefaultAsync(p => p.Number == updatedPokemon.Number); if (pokemon == null) { serviceResponse.Success = false; serviceResponse.Message = $"Pokémon #{pokemon.Number} not been found.";; } else { pokemon.Name = updatedPokemon.Name; pokemon.Number = updatedPokemon.Number; pokemon.Species = updatedPokemon.Species; pokemon.Hp = updatedPokemon.Hp; pokemon.Attack = updatedPokemon.Attack; pokemon.Defense = updatedPokemon.Defense; pokemon.SpAttack = updatedPokemon.SpAttack; pokemon.SpDefense = updatedPokemon.SpDefense; pokemon.Speed = updatedPokemon.Speed; _context.Pokemons.Update(pokemon); await _context.SaveChangesAsync(); serviceResponse.Data = _mapper.Map <GetPokemonDto>(pokemon); serviceResponse.Message = $"Info about Pokémon #{pokemon.Number} has been updated."; } } catch (Exception ex) { serviceResponse.Success = false; serviceResponse.Message = ex.Message; } return(serviceResponse); }
public async Task <IActionResult> UpdatePokemon(UpdatePokemonDto updatePokemon) { return(Ok(await _service.UpdatePokemon(updatePokemon))); }