forked from Anteros-Code-Mentoria/poc-mvc-ocr
Rotas de Tag validadas
This commit is contained in:
parent
23b18ff30c
commit
d0960bb6ca
@ -6,6 +6,7 @@ from app.routes.user_routes import user_bp
|
||||
from app.routes.login_form import login_bp
|
||||
from app.routes.dashboard import dashboard_bp
|
||||
from app.routes.invoice_routes import invoice_bp
|
||||
from app.routes.tag_routes import tag_bp
|
||||
from werkzeug.security import generate_password_hash
|
||||
|
||||
def create_default_user():
|
||||
@ -58,5 +59,5 @@ def create_app():
|
||||
app.register_blueprint(invoice_bp, url_prefix="/invoices")
|
||||
app.register_blueprint(login_bp, url_prefix="/login_bp")
|
||||
app.register_blueprint(dashboard_bp, url_prefix="/dashboard")
|
||||
|
||||
app.register_blueprint(tag_bp, url_prefix="/tags")
|
||||
return app
|
||||
|
||||
@ -1,12 +1,10 @@
|
||||
from sqlalchemy import Column, String
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from app.database import db
|
||||
|
||||
import uuid
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
class Tag(Base):
|
||||
class Tag(db.Model):
|
||||
__tablename__ = "tag"
|
||||
|
||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
nome = Column(String, unique=True, nullable=False)
|
||||
id = db.Column(db.UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
nome = db.Column(db.String, unique=True, nullable=False)
|
||||
|
||||
@ -1,18 +1,39 @@
|
||||
from flask import Blueprint, request, jsonify
|
||||
from models.tag import Tag
|
||||
from models.base import db
|
||||
from app.models.model_tag import Tag
|
||||
from app.database import db
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
tag_routes = Blueprint("tag_routes", __name__, url_prefix="/tags")
|
||||
tag_bp = Blueprint("tag_routes", __name__, url_prefix="/tags")
|
||||
|
||||
@tag_routes.route("/", methods=["POST"])
|
||||
@tag_bp.route("/", methods=["POST"])
|
||||
def create_tag():
|
||||
try:
|
||||
nome = request.json.get("nome")
|
||||
if not nome:
|
||||
return jsonify({"error": "Nome é obrigatório"}), 400
|
||||
|
||||
# Verifica se já existe uma tag com o mesmo nome
|
||||
existing_tag = Tag.query.filter_by(nome=nome).first()
|
||||
if existing_tag:
|
||||
return jsonify({"error": "Nome já está em uso"}), 409
|
||||
|
||||
new_tag = Tag(nome=nome)
|
||||
db.session.add(new_tag)
|
||||
db.session.commit()
|
||||
return jsonify({"id": new_tag.id, "nome": new_tag.nome}), 201
|
||||
|
||||
@tag_routes.route("/", methods=["GET"])
|
||||
except SQLAlchemyError as e:
|
||||
db.session.rollback()
|
||||
return jsonify({"error": "Erro ao criar tag", "details": str(e)}), 500
|
||||
|
||||
@tag_bp.route("/", methods=["GET"])
|
||||
def list_tags():
|
||||
try:
|
||||
tags = Tag.query.all()
|
||||
if not tags:
|
||||
return jsonify({"message": "Nenhuma tag encontrada"}), 404
|
||||
|
||||
return jsonify([{"id": tag.id, "nome": tag.nome} for tag in tags]), 200
|
||||
|
||||
except SQLAlchemyError as e:
|
||||
return jsonify({"error": "Erro ao listar tags", "details": str(e)}), 500
|
||||
|
||||
Loading…
Reference in New Issue
Block a user