forked from Anteros-Code-Mentoria/poc-mvc-ocr
att
This commit is contained in:
parent
11924af113
commit
6b45f7afcb
@ -1,12 +1,10 @@
|
||||
from unittest.mock import Base
|
||||
from app.database import db
|
||||
from sqlalchemy import Column, ForeignKey
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
import uuid
|
||||
|
||||
from app.database import db
|
||||
|
||||
class DocTag(Base):
|
||||
__tablename__ = "doc_tag"
|
||||
class DocTag(db.Model):
|
||||
__tablename__ = "doc_tags"
|
||||
|
||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
documento_id = Column(UUID(as_uuid=True), ForeignKey("documento.id"), nullable=False)
|
||||
@ -1,82 +1,39 @@
|
||||
from flask import Flask, request, jsonify
|
||||
from app.models.model_tag import Tag
|
||||
from app.models.doc_tag import Doc_Tag
|
||||
from flask import Blueprint, request, jsonify
|
||||
from app.models.model_doc_tag import DocTag
|
||||
from app.database import db
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from uuid import UUID
|
||||
|
||||
app = Flask(__name__)
|
||||
# Criação do Blueprint
|
||||
doc_tag_bp = Blueprint("doc_tag_routes", __name__)
|
||||
|
||||
@app.route("/tags", methods=["POST"])
|
||||
def create_tag():
|
||||
try:
|
||||
nome = request.json.get("nome")
|
||||
if not nome:
|
||||
return jsonify({"error": "Nome é obrigatório"}), 400
|
||||
|
||||
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
|
||||
|
||||
except SQLAlchemyError as e:
|
||||
db.session.rollback()
|
||||
return jsonify({"error": "Erro ao criar tag", "details": str(e)}), 500
|
||||
|
||||
@app.route("/tags", 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
|
||||
|
||||
@app.route("/tags/<uuid:tag_id>", methods=["DELETE"])
|
||||
def delete_tag(tag_id):
|
||||
try:
|
||||
tag = Tag.query.get(tag_id)
|
||||
if not tag:
|
||||
return jsonify({"error": "Tag não encontrada"}), 404
|
||||
|
||||
db.session.delete(tag)
|
||||
db.session.commit()
|
||||
return jsonify({"message": "Tag deletada com sucesso"}), 200
|
||||
|
||||
except SQLAlchemyError as e:
|
||||
db.session.rollback()
|
||||
return jsonify({"error": "Erro ao deletar tag", "details": str(e)}), 500
|
||||
|
||||
@app.route("/doc-tags", methods=["POST"])
|
||||
@doc_tag_bp.route("/", methods=["POST"], endpoint="create_doc_tag")
|
||||
def create_doc_tag():
|
||||
try:
|
||||
documento_id = request.json.get("documento_id")
|
||||
tag_id = request.json.get("tag_id")
|
||||
|
||||
# Verifica se os IDs são UUID válidos
|
||||
try:
|
||||
documento_id = UUID(documento_id)
|
||||
tag_id = UUID(tag_id)
|
||||
except ValueError:
|
||||
return jsonify({"error": "IDs devem ser UUID válidos"}), 400
|
||||
|
||||
new_doc_tag = DocTag(documento_id=documento_id, tag_id=tag_id)
|
||||
db.session.add(new_doc_tag)
|
||||
db.session.commit()
|
||||
return jsonify({"id": new_doc_tag.id, "documento_id": new_doc_tag.documento_id, "tag_id": new_doc_tag.tag_id}), 201
|
||||
return jsonify({"id": new_doc_tag.id, "documento_id": str(new_doc_tag.documento_id), "tag_id": str(new_doc_tag.tag_id)}), 201
|
||||
|
||||
except SQLAlchemyError as e:
|
||||
db.session.rollback()
|
||||
return jsonify({"error": "Erro ao criar DocTag", "details": str(e)}), 500
|
||||
|
||||
@app.route("/doc-tags", methods=["GET"])
|
||||
@doc_tag_bp.route("/", methods=["GET"], endpoint="list_doc_tags")
|
||||
def list_doc_tags():
|
||||
try:
|
||||
doc_tags = DocTag.query.all()
|
||||
return jsonify([{ "id": dt.id, "documento_id": dt.documento_id, "tag_id": dt.tag_id } for dt in doc_tags]), 200
|
||||
return jsonify([{ "id": str(dt.id), "documento_id": str(dt.documento_id), "tag_id": str(dt.tag_id) } for dt in doc_tags]), 200
|
||||
|
||||
except SQLAlchemyError as e:
|
||||
return jsonify({"error": "Erro ao listar DocTags", "details": str(e)}), 500
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(debug=True)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user