Skip to content

Commit

Permalink
Merge pull request #6 from Lend-it/93_visualizar_categorias
Browse files Browse the repository at this point in the history
Solve visualizar categorias #93
  • Loading branch information
lucasdutraf authored Mar 22, 2021
2 parents 326a6e0 + 137f669 commit 37af93f
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
2 changes: 2 additions & 0 deletions project/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from flask import Flask, jsonify
from flask_sqlalchemy import SQLAlchemy
from database_singleton import Singleton
from project.api.views import category_blueprint
from project.api.views import request_blueprint

# instantiate the app
Expand All @@ -19,6 +20,7 @@ def create_app(script_info=None):
db.init_app(app)
migrate.init_app(app, db)

app.register_blueprint(category_blueprint)
app.register_blueprint(request_blueprint)

@app.shell_context_processor
Expand Down
6 changes: 6 additions & 0 deletions project/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ class Category(db.Model):
def __init__(self, name):
self.name = name

def to_json(self):
return {
"productcategoryid": self.productcategoryid,
"name": self.name,
}


class Request(db.Model):
__tablename__ = "request"
Expand Down
37 changes: 36 additions & 1 deletion project/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,46 @@
from sqlalchemy import exc

from project.api.models import Request
from project.api.models import Category
from database_singleton import Singleton

db = Singleton().database_connection()

category_blueprint = Blueprint("categories", __name__)
request_blueprint = Blueprint("requests", __name__)

db = Singleton().database_connection()

@category_blueprint.route("/product_category", methods=["GET"])
def get_all_request():
response = {
"status": "success",
"data": {
"categories": [category.to_json() for category in Category.query.all()]
},
}
return jsonify(response), 200


@category_blueprint.route("/product_category", methods=["POST"])
def add_categories():
if not post_data:
return jsonify(error_response), 400

name = post_data.get("name")

try:
db.session.add(Category(name))
db.session.commit()

response = {
"status": "success",
"data": {"message": f"Category {name} was created!"},
}

return jsonify(response), 201
except exc.IntegrityError:
db.session.rollback()
return jsonify(error_response), 400


@request_blueprint.route("/requests", methods=["POST"])
Expand Down
34 changes: 34 additions & 0 deletions project/tests/test_product_category.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import json
import unittest

from project.tests.base import BaseTestCase
from database_singleton import Singleton

from project.api.models import Category

db = Singleton().database_connection()


def add_category(name):
category = Category(name)
db.session.add(category)
db.session.commit()
return category


class TestProductCategory(BaseTestCase):
def test_get_all_category(self):
add_category("Eletrodomésticos")
add_category("Livros e revistas")
add_category("Eletrônicos")

with self.client:
response = self.client.get("/product_category")
data = json.loads(response.data.decode())

self.assertEqual(response.status_code, 200)
self.assertIn("success", data["status"])

self.assertIn("Eletrodomésticos", data["data"]["categories"][0]["name"])
self.assertIn("Livros e revistas", data["data"]["categories"][1]["name"])
self.assertIn("Eletrônicos", data["data"]["categories"][2]["name"])

0 comments on commit 37af93f

Please sign in to comment.