Adicionar endpoint: DELETE /../pokemon/{pokemonId}

This commit is contained in:
Augusto Gunsch 2021-10-18 17:59:57 -03:00
parent e02e05ea06
commit d6f29b98a6
No known key found for this signature in database
GPG Key ID: F7EEFE29825C72DC
3 changed files with 22 additions and 3 deletions

View File

@ -23,4 +23,4 @@ Os endpoints que já estão funcionando marcados:
- [X] GET /trainer/{trainerId}/pokemon
- [X] POST /trainer/{trainerId}/pokemon
- [X] GET /trainer/{trainerId}/pokemon/{pokemonId}
- [ ] DELETE /trainer/{trainerId}/pokemon/{pokemonId}
- [X] DELETE /trainer/{trainerId}/pokemon/{pokemonId}

View File

@ -7,11 +7,11 @@ import asyncio
def route_get_trainer(trainerId):
return trainer.get_trainer(trainerId)
@app.route("/trainer", methods=["GET"])
@app.route("/trainer/", methods=["GET"])
def route_get_trainers():
return trainer.get_trainers()
@app.route("/trainer", methods=["POST"])
@app.route("/trainer/", methods=["POST"])
def route_create_trainer():
return trainer.post_trainer()
@ -33,3 +33,10 @@ def route_post_pokemons_owned(trainer, trainerId):
@app.route("/trainer/<int:trainerId>/pokemon/<int:pokemonId>", methods=["GET"])
def route_get_pokemon_owned(trainerId, pokemonId):
return pokemon_owned.get_pokemon_owned(trainerId, pokemonId)
@app.route("/trainer/<int:trainerId>/pokemon/<int:pokemonId>", methods=["DELETE"])
@helper.token_required
def route_delete_pokemon_owned(trainer, trainerId, pokemonId):
if trainer.id != trainerId:
return errors.ForbiddenError("Trainer id mismatch")
return pokemon_owned.delete_pokemon_owned(trainer, pokemonId)

View File

@ -73,3 +73,15 @@ def get_pokemon_owned(trainer_id, pokemon_id):
return "", 404
except HTTPError as e:
return FetchError(e.message)
def delete_pokemon_owned(trainer, pokemon_id):
try:
pokemon = get_pokemon_fail(trainer, pokemon_id)
set_pokemon_data(pokemon)
db.session.delete(pokemon)
db.session.commit()
return pokemon_owned_schema.dump(pokemon)
except NotFound:
return "", 404
except HTTPError as e:
return FetchError(e.message)