Commit 15337921 authored by Willard's avatar Willard

Accept dish review as JSON instead of form

parent 44616206
...@@ -108,9 +108,9 @@ def dish_favorite(dish_id): ...@@ -108,9 +108,9 @@ def dish_favorite(dish_id):
@auth.login_required @auth.login_required
def create_dish_review(dish_id): def create_dish_review(dish_id):
user_id = g.user.id user_id = g.user.id
title = request.form['title'] title = request.json['title']
body = request.form['body'] body = request.json['body']
rating = int(request.form['rating']) rating = int(request.json['rating'])
review = DishReview(title, body, rating, user_id, dish_id) review = DishReview(title, body, rating, user_id, dish_id)
db.session.add(review) db.session.add(review)
db.session.commit() db.session.commit()
...@@ -118,12 +118,28 @@ def create_dish_review(dish_id): ...@@ -118,12 +118,28 @@ def create_dish_review(dish_id):
@app.route('/api/dishes/<int:dish_id>/reviews', methods=['GET']) @app.route('/api/dishes/<int:dish_id>/reviews', methods=['GET'])
def dish_reviews(dish_id): def dish_reviews(dish_id):
reviews = Dish.query.get(dish_id).reviews.all() dish = Dish.query.get(dish_id)
if dish is None:
return 'Invalid dish'
reviews = dish.reviews.all()
data = [] data = []
for review in reviews: for review in reviews:
data.append(review_obj(review)) data.append(review_obj(review))
return jsonify(data) return jsonify(data)
@app.route('/api/stalls', methods=['GET'])
def get_stall_by_name():
name = request.args.get('name')
return jsonify(stall_obj(Stall.query.filter_by(name=name).first()))
@app.route('/api/stalls/<int:stall_id>/dishes', methods=['GET'])
def get_dishes_by_stall(stall_id):
dishes = []
stall = Stall.query.get(stall_id)
for dish in stall.dishes:
dishes.append(dish_obj(dish))
return jsonify(dishes)
@app.route('/api/dishes/<int:dish_id>/reviews/<int:review_id>', methods=['GET']) @app.route('/api/dishes/<int:dish_id>/reviews/<int:review_id>', methods=['GET'])
def dish_review(dish_id, review_id): def dish_review(dish_id, review_id):
review = Dish.query.get(dish_id).reviews.filter_by(id=review_id).first() review = Dish.query.get(dish_id).reviews.filter_by(id=review_id).first()
...@@ -178,12 +194,21 @@ def dish_obj(dish): ...@@ -178,12 +194,21 @@ def dish_obj(dish):
'image_path': dish.image_path 'image_path': dish.image_path
} }
def stall_obj(stall):
if stall is None:
return None
return {
'id': stall.id,
'name': stall.name,
'description': stall.description,
'location': Location.query.get(stall.location_id).name
}
def review_obj(review): def review_obj(review):
if review is None: if review is None:
return None return None
return { return {
'title': review.title, 'title': review.title,
'body': review.body,
'rating': review.rating, 'rating': review.rating,
'user_id': review.user_id, 'user_id': review.user_id,
'dish_id': review.dish_id 'dish_id': review.dish_id
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment