|
| 1 | +from flask import Flask, jsonify |
| 2 | +from models import db, People, Planet, User, Favorite |
| 3 | + |
| 4 | +app = Flask(__name__) |
| 5 | + |
| 6 | +app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///starwars.db" |
| 7 | +app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False |
| 8 | + |
| 9 | +db.init_app(app) |
| 10 | + |
| 11 | +with app.app_context(): |
| 12 | + db.create_all() |
| 13 | + |
| 14 | +@app.route("/") |
| 15 | +def home(): |
| 16 | + return jsonify({"msg": "Star Wars API running 🚀"}) |
| 17 | + |
| 18 | + |
| 19 | +@app.route("/people", methods=["GET"]) |
| 20 | +def get_people(): |
| 21 | + people = People.query.all() |
| 22 | + return jsonify([p.serialize() for p in people]), 200 |
| 23 | + |
| 24 | + |
| 25 | +@app.route("/people/<int:people_id>", methods=["GET"]) |
| 26 | +def get_single_person(people_id): |
| 27 | + person = People.query.get(people_id) |
| 28 | + if not person: |
| 29 | + return jsonify({"error": "Person not found"}), 404 |
| 30 | + return jsonify(person.serialize()), 200 |
| 31 | + |
| 32 | + |
| 33 | +@app.route("/planets", methods=["GET"]) |
| 34 | +def get_planets(): |
| 35 | + planets = Planet.query.all() |
| 36 | + return jsonify([p.serialize() for p in planets]), 200 |
| 37 | + |
| 38 | + |
| 39 | +@app.route("/planets/<int:planet_id>", methods=["GET"]) |
| 40 | +def get_single_planet(planet_id): |
| 41 | + planet = Planet.query.get(planet_id) |
| 42 | + if not planet: |
| 43 | + return jsonify({"error": "Planet not found"}), 404 |
| 44 | + return jsonify(planet.serialize()), 200 |
| 45 | + |
| 46 | + |
| 47 | +@app.route("/users", methods=["GET"]) |
| 48 | +def get_users(): |
| 49 | + users = User.query.all() |
| 50 | + return jsonify([u.serialize() for u in users]), 200 |
| 51 | + |
| 52 | + |
| 53 | +def get_current_user(): |
| 54 | + return User.query.first() |
| 55 | + |
| 56 | + |
| 57 | +@app.route("/users/favorites", methods=["GET"]) |
| 58 | +def get_user_favorites(): |
| 59 | + user = get_current_user() |
| 60 | + if not user: |
| 61 | + return jsonify([]), 200 |
| 62 | + |
| 63 | + favorites = [] |
| 64 | + |
| 65 | + for fav in user.favorites: |
| 66 | + if fav.people: |
| 67 | + favorites.append({ |
| 68 | + "type": "people", |
| 69 | + "item": fav.people.serialize() |
| 70 | + }) |
| 71 | + if fav.planet: |
| 72 | + favorites.append({ |
| 73 | + "type": "planet", |
| 74 | + "item": fav.planet.serialize() |
| 75 | + }) |
| 76 | + |
| 77 | + return jsonify(favorites), 200 |
| 78 | + |
| 79 | + |
| 80 | +@app.route("/favorite/people/<int:people_id>", methods=["POST"]) |
| 81 | +def add_favorite_people(people_id): |
| 82 | + user = get_current_user() |
| 83 | + person = People.query.get(people_id) |
| 84 | + |
| 85 | + if not user or not person: |
| 86 | + return jsonify({"error": "User or person not found"}), 404 |
| 87 | + |
| 88 | + favorite = Favorite(user_id=user.id, people_id=person.id) |
| 89 | + db.session.add(favorite) |
| 90 | + db.session.commit() |
| 91 | + |
| 92 | + return jsonify({"msg": "Favorite person added"}), 201 |
| 93 | + |
| 94 | + |
| 95 | +@app.route("/favorite/planet/<int:planet_id>", methods=["POST"]) |
| 96 | +def add_favorite_planet(planet_id): |
| 97 | + user = get_current_user() |
| 98 | + planet = Planet.query.get(planet_id) |
| 99 | + |
| 100 | + if not user or not planet: |
| 101 | + return jsonify({"error": "User or planet not found"}), 404 |
| 102 | + |
| 103 | + favorite = Favorite(user_id=user.id, planet_id=planet.id) |
| 104 | + db.session.add(favorite) |
| 105 | + db.session.commit() |
| 106 | + |
| 107 | + return jsonify({"msg": "Favorite planet added"}), 201 |
| 108 | + |
| 109 | + |
| 110 | +@app.route("/favorite/people/<int:people_id>", methods=["DELETE"]) |
| 111 | +def delete_favorite_people(people_id): |
| 112 | + user = get_current_user() |
| 113 | + |
| 114 | + favorite = Favorite.query.filter_by( |
| 115 | + user_id=user.id, |
| 116 | + people_id=people_id |
| 117 | + ).first() |
| 118 | + |
| 119 | + if not favorite: |
| 120 | + return jsonify({"error": "Favorite not found"}), 404 |
| 121 | + |
| 122 | + db.session.delete(favorite) |
| 123 | + db.session.commit() |
| 124 | + |
| 125 | + return jsonify({"msg": "Favorite person deleted"}), 200 |
| 126 | + |
| 127 | + |
| 128 | +@app.route("/favorite/planet/<int:planet_id>", methods=["DELETE"]) |
| 129 | +def delete_favorite_planet(planet_id): |
| 130 | + user = get_current_user() |
| 131 | + |
| 132 | + favorite = Favorite.query.filter_by( |
| 133 | + user_id=user.id, |
| 134 | + planet_id=planet_id |
| 135 | + ).first() |
| 136 | + |
| 137 | + if not favorite: |
| 138 | + return jsonify({"error": "Favorite not found"}), 404 |
| 139 | + |
| 140 | + db.session.delete(favorite) |
| 141 | + db.session.commit() |
| 142 | + |
| 143 | + return jsonify({"msg": "Favorite planet deleted"}), 200 |
| 144 | + |
| 145 | + |
| 146 | +if __name__ == "__main__": |
| 147 | + app.run(debug=True) |
| 148 | + |
0 commit comments