Skip to content

Commit

Permalink
add get one pet route and validate model
Browse files Browse the repository at this point in the history
  • Loading branch information
apradoada committed Dec 6, 2024
1 parent 66174e2 commit a755f61
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion app/routes/pet_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,24 @@ def get_pets():
for pet in pets:
response.append(pet.to_dict())

return jsonify(response)
return jsonify(response)

@bp.get("/<pet_id>")
def get_single_cat(pet_id):
cat = validate_model(Pet,pet_id)
return cat.to_dict()

def validate_model(cls,id):
try:
id = int(id)
except:
response = response = {"message": f"{cls.__name__} {id} invalid"}
abort(make_response(response , 400))

query = db.select(cls).where(cls.id == id)
model = db.session.scalar(query)
if model:
return model

response = {"message": f"{cls.__name__} {id} not found"}
abort(make_response(response, 404))

0 comments on commit a755f61

Please sign in to comment.