This repository has been archived by the owner on Apr 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathapplication.py
executable file
·90 lines (65 loc) · 2.51 KB
/
application.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from flask import Flask, render_template, jsonify, request, abort
import config
from yelp import get_businesses
# from db import Favorite, get_session
application = Flask(__name__)
# Renders UI
@application.route("/")
def home():
# 💡 renders an html page with url parameters, if any
# 🆘 1. We need to show the correct homepage! Change start.html to homepage.html
return render_template("start.html", city=request.args.get("city", ""))
# API Endpoints
@application.route("/api/places/<city>") # annotation for route
def places(city):
businesses = get_businesses(city) # getting list of businesses from Yelp
return jsonify(businesses) # json rendered array
# ================================================================================
# Code skeleton for favorites. Not covered in this workshop, but a fun next step!
# ================================================================================
@application.route("/api/places/<place_id>/favorites", methods=["POST"])
def create_favorite_event(place_id):
print("Should save a favorite", place_id)
return abort(400)
# user_id = request.headers.get("x-user-id")
# session = get_session()
# favorite = (
# session.query(Favorite).filter_by(place_id=place_id, user_id=user_id).first()
# )
# if favorite:
# return abort(400)
# favorite = Favorite(place_id=place_id, user_id=user_id)
# session.add(favorite)
# session.commit()
# return jsonify(favorite.to_dict())
# @application.route("/api/places/<place_id>/favorites", methods=["DELETE"])
# def delete_favorite_event(place_id):
# user_id = request.headers.get("x-user-id")
# session = get_session()
# favorite = (
# session.query(Favorite).filter_by(place_id=place_id, user_id=user_id).first()
# )
# if favorite:
# session.delete(favorite)
# session.commit()
# return jsonify(favorite.to_dict())
# abort(404)
@application.route("/api/places/<place_id>/favorites", methods=["GET"])
def get_favorites_event(place_id):
print("Should get number of favourites for", place_id)
return jsonify(
{
"count": 0,
"results": [],
}
)
# session = get_session()
# favorites_query = session.query(Favorite).filter_by(place_id=place_id)
# return jsonify(
# {
# "count": favorites_query.count(),
# "results": [x.to_dict() for x in favorites_query.all()],
# }
# )
if __name__ == "__main__":
application.run()