Skip to content

Commit

Permalink
Merge pull request #14 from Lend-it/121_ajudar_solicitação
Browse files Browse the repository at this point in the history
Solve [US16] - Ajudar Solicitação #121
  • Loading branch information
lucasdutraf authored Apr 3, 2021
2 parents 3d2b21e + 0a4b9b2 commit 0d92fe8
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 20 deletions.
13 changes: 0 additions & 13 deletions .github/workflows/black.yml

This file was deleted.

8 changes: 2 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,5 @@ recreate-db:
sudo docker-compose -f docker-compose.dev.yml run request python manage.py recreate-db

cov-html:
@if [ -d "$(CURRENT_DIR)/htmlcov" ]; then \
google-chrome $(CURRENT_DIR)/htmlcov/index.html; \
else \
sudo docker-compose -f docker-compose.dev.yml run request python manage.py cov; \
google-chrome $(CURRENT_DIR)/htmlcov/index.html; \
fi
sudo docker-compose -f docker-compose.dev.yml run request python manage.py cov;
google-chrome $(CURRENT_DIR)/htmlcov/index.html;
10 changes: 10 additions & 0 deletions project/api/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from project.api.models import Category


def get_category_name(requests: list) -> list:
for request in requests:
productcategoryid = request["productcategoryid"]
category = Category.query.filter_by(productcategoryid=productcategoryid).first()
request["categoryname"] = category.name

return requests
25 changes: 24 additions & 1 deletion project/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from project.api.models import db
from project.api.models import Category
from database_singleton import Singleton
from project.api.utils import get_category_name


category_blueprint = Blueprint("categories", __name__)
Expand Down Expand Up @@ -56,9 +57,10 @@ def add_categories():

@request_blueprint.route("/requests", methods=["GET"])
def get_all_request():
requests = get_category_name([request.to_json() for request in Request.query.all()])
response = {
"status": "success",
"data": {"requests": [request.to_json() for request in Request.query.all()]},
"data": {"requests": requests},
}
return jsonify(response), 200

Expand Down Expand Up @@ -102,6 +104,27 @@ def create_request():
return jsonify(error_response), 400


@request_blueprint.route("/requests/<requestid>", methods=["PATCH"])
def update_request_lender(requestid):
post_data = request.get_json()

error_response = {"status": "fail", "message": "Request not found"}

lender = post_data.get("lender")

product = Request.query.filter_by(requestid=requestid).first()

if not product:
return jsonify(error_response), 404

product.lender = lender
db.session.commit()

response = {"status": "success", "request": product.to_json()}

return jsonify(response), 200


@request_blueprint.route("/requests/<requestid>", methods=["PUT"])
def edit_request(requestid):

Expand Down
31 changes: 31 additions & 0 deletions project/tests/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,34 @@ def test_get_all_requests(self):
)
self.assertIn("Jogo da vida", data["data"]["requests"][1]["productname"])
self.assertIn("War", data["data"]["requests"][2]["productname"])

def test_update_lender_request(self):
product = add_request(
"Banco Imobiliario",
"2020-09-12 00:00:00.000",
"2020-09-30 00:00:00.000",
"Queria um banco imobiliário emprestado para jogar com meus amigos neste fim de semana!",
"[email protected]",
3,
)

with self.client:
response = self.client.patch(
f"/requests/{product.requestid}",
data=json.dumps({"lender": "[email protected]"}),
content_type="application/json",
)

data = json.loads(response.data.decode())
self.assertIn("[email protected]", data["request"]["lender"])

def test_cannot_update_non_existing_request_lender(self):
with self.client:
response = self.client.patch(
"/requests/8d27b6c1-ac8a-4f29-97b0-96cef6938267",
data=json.dumps({"lender": "[email protected]"}),
content_type="application/json",
)

data = json.loads(response.data.decode())
self.assertEqual(response.status_code, 404)

0 comments on commit 0d92fe8

Please sign in to comment.