From e9e7adab6afe20614e6d6233e148c5fa13a3f540 Mon Sep 17 00:00:00 2001 From: Matheus Afonso Date: Wed, 17 Mar 2021 22:53:35 -0300 Subject: [PATCH 1/3] feat: fga-eps-mds/2020.2-Lend.it#95 Created post request route Co-authored-by: Thiago Mesquita --- project/api/views.py | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/project/api/views.py b/project/api/views.py index a6e9eac..4963eb7 100755 --- a/project/api/views.py +++ b/project/api/views.py @@ -3,10 +3,11 @@ from sqlalchemy import exc from project.api.models import Request -from project import db +from database_singleton import Singleton test_blueprint = Blueprint("tasks", __name__) +db = Singleton().database_connection() @test_blueprint.route("/api/requests", methods=["GET"]) def get_all_request(): @@ -15,3 +16,40 @@ def get_all_request(): "data": {"tasks": [request.to_json() for request in Request.query.all()]}, } return jsonify(response), 200 + +@test_blueprint.route("/api/requests", methods=["POST"]) +def create_request(): + post_data = request.get_json() + + error_response = { + 'status': 'fail', + 'message': 'Invalid payload.' + } + + productname = post_data.get('productname'), + startdate = post_data.get('startdate'), + enddate = post_data.get('enddate'), + description = post_data.get('description'), + requester = post_data.get('requester'), + productcategoryid = post_data.get('productcategoryid'), + lender = None + + lending_request = Request(productname, startdate, enddate, description, requester, lender, productcategoryid) + + try: + db.session.add(lending_request) + db.session.commit() + + response = { + 'status': 'success', + 'data': { + 'request': lending_request.to_json() + } + } + + return jsonify(response), 201 + except exc.IntegrityError as e: + db.session.rollback() + return jsonify(error_response), 400 + + return jsonify(content), 200 From 4b41c046e611e32f7bf48b1e57b1ef355fe61ac7 Mon Sep 17 00:00:00 2001 From: Matheus Afonso Date: Fri, 19 Mar 2021 20:44:55 -0300 Subject: [PATCH 2/3] chore: fga-eps-mds/2020.2-Lend.it#95 Create request blueprint Co-authored-by: Thiago Mesquita --- project/__init__.py | 5 +++-- project/api/views.py | 48 +++++++++++++++++++++++--------------------- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/project/__init__.py b/project/__init__.py index 7cd395c..425d79f 100755 --- a/project/__init__.py +++ b/project/__init__.py @@ -2,7 +2,7 @@ from flask import Flask, jsonify from flask_sqlalchemy import SQLAlchemy from database_singleton import Singleton -from project.api.views import test_blueprint +from project.api.views import request_blueprint # instantiate the app def create_app(script_info=None): @@ -19,7 +19,8 @@ def create_app(script_info=None): db.init_app(app) migrate.init_app(app, db) - app.register_blueprint(test_blueprint) + app.register_blueprint(request_blueprint) + app.register_blueprint(request_blueprint) @app.shell_context_processor def ctx(): diff --git a/project/api/views.py b/project/api/views.py index 4963eb7..4edd2b8 100755 --- a/project/api/views.py +++ b/project/api/views.py @@ -5,47 +5,49 @@ from project.api.models import Request from database_singleton import Singleton -test_blueprint = Blueprint("tasks", __name__) +request_blueprint = Blueprint("requests", __name__) db = Singleton().database_connection() -@test_blueprint.route("/api/requests", methods=["GET"]) + +@request_blueprint.route("/api/requests", methods=["GET"]) def get_all_request(): response = { "status": "success", - "data": {"tasks": [request.to_json() for request in Request.query.all()]}, + "data": {"requests": [request.to_json() for request in Request.query.all()]}, } return jsonify(response), 200 -@test_blueprint.route("/api/requests", methods=["POST"]) + +@request_blueprint.route("/api/requests", methods=["POST"]) def create_request(): post_data = request.get_json() - - error_response = { - 'status': 'fail', - 'message': 'Invalid payload.' - } - productname = post_data.get('productname'), - startdate = post_data.get('startdate'), - enddate = post_data.get('enddate'), - description = post_data.get('description'), - requester = post_data.get('requester'), - productcategoryid = post_data.get('productcategoryid'), + error_response = {"status": "fail", "message": "Invalid payload."} + + productname = (post_data.get("productname"),) + startdate = (post_data.get("startdate"),) + enddate = (post_data.get("enddate"),) + description = (post_data.get("description"),) + requester = (post_data.get("requester"),) + productcategoryid = (post_data.get("productcategoryid"),) lender = None - - lending_request = Request(productname, startdate, enddate, description, requester, lender, productcategoryid) + + lending_request = Request( + productname, + startdate, + enddate, + description, + requester, + lender, + productcategoryid, + ) try: db.session.add(lending_request) db.session.commit() - response = { - 'status': 'success', - 'data': { - 'request': lending_request.to_json() - } - } + response = {"status": "success", "data": {"request": lending_request.to_json()}} return jsonify(response), 201 except exc.IntegrityError as e: From 94fd125f286d3a5d64044b1e5dcfe3bb97dae809 Mon Sep 17 00:00:00 2001 From: Matheus Afonso Date: Sat, 20 Mar 2021 13:52:14 -0300 Subject: [PATCH 3/3] chore: fga-eps-mds/2020.2-Lend.it#95 Remove blueprint line and fixed post route Co-authored-by: Thiago Mesquita --- project/__init__.py | 1 - project/api/views.py | 13 +------------ 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/project/__init__.py b/project/__init__.py index 425d79f..d2ba351 100755 --- a/project/__init__.py +++ b/project/__init__.py @@ -19,7 +19,6 @@ def create_app(script_info=None): db.init_app(app) migrate.init_app(app, db) - app.register_blueprint(request_blueprint) app.register_blueprint(request_blueprint) @app.shell_context_processor diff --git a/project/api/views.py b/project/api/views.py index 4edd2b8..eda5323 100755 --- a/project/api/views.py +++ b/project/api/views.py @@ -10,16 +10,7 @@ db = Singleton().database_connection() -@request_blueprint.route("/api/requests", methods=["GET"]) -def get_all_request(): - response = { - "status": "success", - "data": {"requests": [request.to_json() for request in Request.query.all()]}, - } - return jsonify(response), 200 - - -@request_blueprint.route("/api/requests", methods=["POST"]) +@request_blueprint.route("/requests", methods=["POST"]) def create_request(): post_data = request.get_json() @@ -53,5 +44,3 @@ def create_request(): except exc.IntegrityError as e: db.session.rollback() return jsonify(error_response), 400 - - return jsonify(content), 200