Skip to content

Commit

Permalink
Merge pull request #7 from Lend-it/95_solicitar_emprestimo
Browse files Browse the repository at this point in the history
Solve solicitar emprestimo #95
  • Loading branch information
lucasdutraf authored Mar 20, 2021
2 parents 6e23934 + 3447a90 commit 326a6e0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
4 changes: 2 additions & 2 deletions project/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -19,7 +19,7 @@ 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.shell_context_processor
def ctx():
Expand Down
47 changes: 38 additions & 9 deletions project/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,44 @@
from sqlalchemy import exc

from project.api.models import Request
from project import db
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"])
def get_all_request():
response = {
"status": "success",
"data": {"tasks": [request.to_json() for request in Request.query.all()]},
}
return jsonify(response), 200

@request_blueprint.route("/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

0 comments on commit 326a6e0

Please sign in to comment.