-3

I have this API that deletes a recipe using recipe_id that belongs to a user_id. I am trying to create a query before the delete function. I want the app to check if ID exists first, if it's not, flash an error and if it is there, delete the recipe. Thanks.

@app.route('/delete/<int:id>', methods=['DELETE'])
@jwt_required()
def delete_recipe(id):
current_user = get_jwt_identity()
query = Recipe.delete().where((Recipe.poster_id == current_user) & (Recipe.id == id))
return jsonify({'result': query.execute()}), 204
icanbe444
  • 11
  • 3

1 Answers1

0

Typically you will raise a 404 not found if the recipe w/the id does not exist.

Peewee can help you with checking existence quite easily:

if not Recipe.select().where(Recipe.id == 234).exists():
    return 'not found', 404
coleifer
  • 24,887
  • 6
  • 60
  • 75
  • Thanks a bunch. this works well for ID that doesn't exist. How do I manage the ID that exists but current_user does not have permission to delete it. each Id is linked to a current_user by foreignkey. – icanbe444 Apr 22 '22 at 14:39