public ActionResult Post([FromBody] Pokemon pokemon) { var validator = new PokemonValidator(); var result = validator.Validate(pokemon); if (ModelState.IsValid) { pokemonRepository.Add(pokemon); return(Ok()); } return(BadRequest(result.Errors)); }
private bool CheckPokemon(IPokemonData pokemon) { return(PokemonValidator.Validate(pokemon)); //TODO: more }
public Microsoft.AspNetCore.Mvc.ActionResult <dynamic> InsertPokemon(Pokemons newPokemon) { BindingList <string> errors = new BindingList <string>(); PokemonValidator validator = new PokemonValidator(); ValidationResult results = validator.Validate(newPokemon); //valida o pokemon que tá chegando if (results.IsValid == false) { foreach (ValidationFailure failure in results.Errors) { errors.Add($"{failure.PropertyName}: {failure.ErrorMessage}"); } return(errors); } using (IDbConnection conn = Connection) { conn.Open(); Guid pokeguid = Guid.NewGuid(); int rowsAffected = conn.QueryFirst <int>(@"INSERT INTO pokemons( id, name, hp, attack, defense, special_attack, special_defense, speed, generation) VALUES( @pokemonguid, @name, @hp, @attack, @defense, @special_attack, @special_defense, @speed, @generation)", new { pokemonguid = pokeguid, name = newPokemon.name, hp = newPokemon.hp, attack = newPokemon.attack, defense = newPokemon.defense, special_attack = newPokemon.special_attack, special_defense = newPokemon.special_defense, speed = newPokemon.speed, generation = newPokemon.generation }); if (newPokemon.types != null && newPokemon.types.Count > 0) { foreach (var types in newPokemon.types) { int rowsAffectedAddresses = conn.QueryFirst <int>(@"INSERT INTO pokemon_types( id, pokemon_id, type_id) VALUES (@guidtipo, @pokemonguid, @guidtipotabela);", new { guidtipo = Guid.NewGuid(), pokemonguid = pokeguid, guidtipotabela = Guid.NewGuid() //não vou conseguir mostrar os tipos por tempo e não entender direito como ir na outra tabela...Sorry }); } } if (rowsAffected > 0) { return(true); } } return(false); }