|
| 1 | +from flask import Flask, jsonify, render_template, request |
| 2 | +from flask_sqlalchemy import SQLAlchemy |
| 3 | +import random as random |
| 4 | + |
| 5 | +app = Flask(__name__) |
| 6 | + |
| 7 | +# Connect to Database |
| 8 | +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///cafes.db' |
| 9 | +app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False |
| 10 | +db = SQLAlchemy(app) |
| 11 | + |
| 12 | + |
| 13 | +# Cafe TABLE Configuration |
| 14 | +class Cafe(db.Model): |
| 15 | + id = db.Column(db.Integer, primary_key=True) |
| 16 | + name = db.Column(db.String(250), unique=True, nullable=False) |
| 17 | + map_url = db.Column(db.String(500), nullable=False) |
| 18 | + img_url = db.Column(db.String(500), nullable=False) |
| 19 | + location = db.Column(db.String(250), nullable=False) |
| 20 | + seats = db.Column(db.String(250), nullable=False) |
| 21 | + has_toilet = db.Column(db.Boolean, nullable=False) |
| 22 | + has_wifi = db.Column(db.Boolean, nullable=False) |
| 23 | + has_sockets = db.Column(db.Boolean, nullable=False) |
| 24 | + can_take_calls = db.Column(db.Boolean, nullable=False) |
| 25 | + coffee_price = db.Column(db.String(250), nullable=True) |
| 26 | + |
| 27 | + def to_dict(self): |
| 28 | + return {column.name: getattr(self, column.name) for column in self.__table__.columns} |
| 29 | + |
| 30 | + |
| 31 | +@app.route("/") |
| 32 | +def home(): |
| 33 | + return render_template("index.html") |
| 34 | + |
| 35 | + |
| 36 | +# HTTP GET - Read Record |
| 37 | +@app.route("/random") |
| 38 | +def get_random_cafe(): |
| 39 | + cafes = db.session.query(Cafe).all() |
| 40 | + random_cafe = random.choice(cafes) |
| 41 | + return jsonify(cafe={ |
| 42 | + "id": random_cafe.id, |
| 43 | + "name": random_cafe.name, |
| 44 | + "map_url": random_cafe.map_url, |
| 45 | + "img_url": random_cafe.img_url, |
| 46 | + "location": random_cafe.location, |
| 47 | + |
| 48 | + "amenities": { |
| 49 | + "seats": random_cafe.seats, |
| 50 | + "has_toilet": random_cafe.has_toilet, |
| 51 | + "has_wifi": random_cafe.has_wifi, |
| 52 | + "has_sockets": random_cafe.has_sockets, |
| 53 | + "can_take_calls": random_cafe.can_take_calls, |
| 54 | + "coffee_price": random_cafe.coffee_price, |
| 55 | + } |
| 56 | + }) |
| 57 | + |
| 58 | + |
| 59 | +@app.route("/all") |
| 60 | +def get_all_cafes(): |
| 61 | + cafes = db.session.query(Cafe).all() |
| 62 | + return jsonify(cafes=[cafe.to_dict() for cafe in cafes]) |
| 63 | + |
| 64 | + |
| 65 | +@app.route("/search") |
| 66 | +def get_cafe_at_location(): |
| 67 | + query_location = request.args.get("location") |
| 68 | + print(query_location) |
| 69 | + cafe = db.session.query(Cafe).filter_by(location=query_location).first() |
| 70 | + if cafe: |
| 71 | + return jsonify(cafe=cafe.to_dict()) |
| 72 | + else: |
| 73 | + return jsonify(error={"Not Found": "Error 404: Sorry, we didn't find a cafe at that location."}) |
| 74 | + |
| 75 | + |
| 76 | +# HTTP POST - Create Record |
| 77 | +@app.route("/add", methods=["POST"]) |
| 78 | +def post_new_cafe(): |
| 79 | + new_cafe = Cafe( |
| 80 | + name=request.form.get("name"), |
| 81 | + map_url=request.form.get("map_url"), |
| 82 | + img_url=request.form.get("img_url"), |
| 83 | + location=request.form.get("location"), |
| 84 | + has_sockets=bool(request.form.get("sockets")), |
| 85 | + has_toilet=bool(request.form.get("toilet")), |
| 86 | + has_wifi=bool(request.form.get("wifi")), |
| 87 | + can_take_calls=bool(request.form.get("calls")), |
| 88 | + seats=request.form.get("seats"), |
| 89 | + coffee_price=request.form.get("coffee_price"), |
| 90 | + ) |
| 91 | + db.session.add(new_cafe) |
| 92 | + db.session.commit() |
| 93 | + return jsonify(response={"success": "Successfully added the new cafe."}) |
| 94 | + |
| 95 | + |
| 96 | +# HTTP PUT/PATCH - Update Record |
| 97 | +@app.route("/update-price/<int:cafe_id>", methods=["PATCH"]) |
| 98 | +def patch_new_price(cafe_id): |
| 99 | + new_price = request.args.get("new_price") |
| 100 | + cafe = db.session.query(Cafe).get(cafe_id) |
| 101 | + if cafe: |
| 102 | + cafe.coffee_price = new_price |
| 103 | + db.session.commit() |
| 104 | + return jsonify(response={"success": "Successfully updated the price."}) |
| 105 | + else: |
| 106 | + return jsonify(error={"Not Found": "Sorry a cafe with that id was not found in the database."}) |
| 107 | + |
| 108 | + |
| 109 | +# HTTP DELETE - Delete Record |
| 110 | +@app.route("/report-closed/<int:cafe_id>", methods=["DELETE"]) |
| 111 | +def delete_cafe(cafe_id): |
| 112 | + api_key = request.args.get("api-key") |
| 113 | + if api_key == "TopSecretAPIKey": |
| 114 | + cafe = db.session.query(Cafe).get(cafe_id) |
| 115 | + if cafe: |
| 116 | + db.session.delete(cafe) |
| 117 | + db.session.commit() |
| 118 | + return jsonify(response={"success": "Successfully deleted the cafe from the database."}), 200 |
| 119 | + else: |
| 120 | + return jsonify(error={"Not Found": "Sorry a cafe with that id was not found in the database."}), 404 |
| 121 | + else: |
| 122 | + return jsonify(error={"Forbidden": "Sorry, that's not allowed. Make sure you have the correct api_key."}), 403 |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == '__main__': |
| 126 | + app.run(debug=True) |
0 commit comments