From 50d113dce827ea067bd6f0761cff6dba97221906 Mon Sep 17 00:00:00 2001 From: androiddrew Date: Wed, 20 Jun 2018 14:04:24 -0400 Subject: [PATCH] Added POST capability to books endpoint --- server/booker/app.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/server/booker/app.py b/server/booker/app.py index f5245fd..9ab15f4 100644 --- a/server/booker/app.py +++ b/server/booker/app.py @@ -1,4 +1,4 @@ -from flask import Flask, jsonify +from flask import Flask, jsonify, request from flask_cors import CORS # config @@ -37,12 +37,20 @@ def ping_route(): return jsonify('You pass butter') -@app.route('/books', methods=['GET']) +@app.route('/books', methods=['GET', 'POST']) def all_books(): - return jsonify({ - 'status': 'success', - 'books': BOOKS - }) + response_object = {'status': 'success'} + if request.method == 'POST': + post_data = request.get_json() + BOOKS.append({ + 'title': post_data.get('title'), + 'author': post_data.get('author'), + 'read': post_data.get('read') + }) + response_object['message'] = 'Book added!' + else: + response_object['books'] = BOOKS + return jsonify(response_object) if __name__ == '__main__':