Skip to content

Commit 7d0dd7b

Browse files
committed
ft001 database manejo de errores commit
1 parent 3f7e562 commit 7d0dd7b

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/api/routes.py

+19-9
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ def sitemap():
3232
@api.route('/foods', methods=['GET'])
3333
def get_foods():
3434
foods = Food.query.all()
35-
return jsonify([food.serialize() for food in foods])
35+
if not foods:
36+
return "food not found", 404
37+
else:
38+
return jsonify([food.serialize() for food in foods]), 200
3639

3740

3841
# Obtener un alimento por ID
@@ -41,13 +44,15 @@ def get_food(food_id):
4144
food = Food.query.get(food_id)
4245
if not food:
4346
return jsonify({"error": "Food not found"}), 404
44-
return jsonify(food.serialize())
47+
return jsonify(food.serialize()), 200
4548

4649

4750
@api.route('/users', methods=['GET'])
4851
def get_users():
4952
users = User.query.all()
50-
return jsonify([user.serialize() for user in users])
53+
if not users:
54+
return "not users found", 404
55+
return jsonify([user.serialize() for user in users]), 200
5156

5257

5358
# Obtener un usuario por ID
@@ -56,13 +61,15 @@ def get_user(user_id):
5661
user = User.query.get(user_id)
5762
if not user:
5863
return jsonify({"error": "user not found"}), 404
59-
return jsonify(user.serialize())
64+
return jsonify(user.serialize()), 200
6065

6166

6267
@api.route('/pets', methods=['GET'])
6368
def get_pets():
6469
pets = Pet.query.all()
65-
return jsonify([pet.serialize() for pet in pets])
70+
if not pets:
71+
return "no pets found", 404
72+
return jsonify([pet.serialize() for pet in pets]), 200
6673

6774

6875
# Obtener una mascota por ID
@@ -71,7 +78,7 @@ def get_pet(pet_id):
7178
pet = Pet.query.get(pet_id)
7279
if not pet:
7380
return jsonify({"error": "pet not found"}), 404
74-
return jsonify(pet.serialize())
81+
return jsonify(pet.serialize()), 200
7582

7683
#obtener sugerencias de comida según mascota
7784
@api.route('/foods/suggestions/<int:pet_id>', methods=['GET'])
@@ -90,14 +97,17 @@ def get_pet_suggestions(pet_id):
9097
food_suggestions = db.session.execute(select(Food).where(Food.animal_type==pet["animal_type"]),
9198
Food.age==pet["age"],
9299
Food.pathologies==pet["pathologies"]).all()
93-
print(food_suggestions)
94-
return [food[0].serialize() for food in food_suggestions]
100+
if not food_suggestions :
101+
return "no suggestions found", 404
102+
return [food[0].serialize() for food in food_suggestions], 200
95103

96104
# Obtener todos los accesorios
97105
@api.route('/accessories', methods=['GET'])
98106
def get_accessories():
99107
accessories = Accessories.query.all()
100-
return jsonify([accessory.serialize() for accessory in accessories])
108+
if not accessories:
109+
return "no accessories found", 404
110+
return jsonify([accessory.serialize() for accessory in accessories]), 200
101111

102112

103113
# Obtener una accesotio por ID

0 commit comments

Comments
 (0)