forked from Anteros-Code-Mentoria/poc-mvc-ocr
Compare commits
No commits in common. "main" and "main" have entirely different histories.
@ -27,8 +27,6 @@ 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 app.routes.doc_tag_routes import doc_tag_bp
|
||||
from werkzeug.security import generate_password_hash
|
||||
|
||||
def create_default_user():
|
||||
@ -81,6 +79,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")
|
||||
app.register_blueprint(doc_tag_bp, url_prefix="/doc-tags")
|
||||
|
||||
return app
|
||||
|
||||
@ -1,11 +0,0 @@
|
||||
from app.database import db
|
||||
from sqlalchemy import Column, ForeignKey
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
import uuid
|
||||
|
||||
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)
|
||||
tag_id = Column(UUID(as_uuid=True), ForeignKey("tag.id"), nullable=False)
|
||||
@ -1,10 +0,0 @@
|
||||
from app.database import db
|
||||
|
||||
import uuid
|
||||
|
||||
|
||||
class Tag(db.Model):
|
||||
__tablename__ = "tag"
|
||||
|
||||
id = db.Column(db.UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
nome = db.Column(db.String, unique=True, nullable=False)
|
||||
@ -1,16 +0,0 @@
|
||||
from sqlalchemy import Column, String, Boolean, ForeignKey, DateTime
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
class OCRResult(Base):
|
||||
__tablename__ = "ocr_result" # Corrigido
|
||||
|
||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
documento_id = Column(UUID(as_uuid=True), ForeignKey("documento.id"), unique=True, nullable=False)
|
||||
caminho_texto = Column(String, nullable=True)
|
||||
indexado = Column(Boolean, default=False)
|
||||
criado_em = Column(DateTime, default=lambda: datetime.now(timezone.utc), nullable=False)
|
||||
@ -1,39 +0,0 @@
|
||||
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
|
||||
|
||||
# Criação do Blueprint
|
||||
doc_tag_bp = Blueprint("doc_tag_routes", __name__)
|
||||
|
||||
@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": 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
|
||||
|
||||
@doc_tag_bp.route("/", methods=["GET"], endpoint="list_doc_tags")
|
||||
def list_doc_tags():
|
||||
try:
|
||||
doc_tags = DocTag.query.all()
|
||||
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
|
||||
@ -1,19 +0,0 @@
|
||||
from flask import Blueprint, request, jsonify
|
||||
from models.ocr_result import OCRResult
|
||||
from models.base import db
|
||||
import uuid
|
||||
|
||||
ocr_routes = Blueprint("ocr_routes", __name__, url_prefix="/ocr")
|
||||
|
||||
@ocr_routes.route("/", methods=["POST"])
|
||||
def create_ocr():
|
||||
documento_id = request.json.get("documento_id")
|
||||
new_ocr = OCRResult(documento_id=documento_id)
|
||||
db.session.add(new_ocr)
|
||||
db.session.commit()
|
||||
return jsonify({"id": new_ocr.id, "documento_id": new_ocr.documento_id}), 201
|
||||
|
||||
@ocr_routes.route("/", methods=["GET"])
|
||||
def list_ocr():
|
||||
ocrs = OCRResult.query.all()
|
||||
return jsonify([{"id": ocr.id, "documento_id": ocr.documento_id} for ocr in ocrs]), 200
|
||||
@ -1,55 +0,0 @@
|
||||
from flask import Blueprint, request, jsonify
|
||||
from app.models.model_tag import Tag
|
||||
from app.database import db
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from uuid import UUID
|
||||
|
||||
tag_bp = Blueprint("tag_routes", __name__, url_prefix="/tags")
|
||||
|
||||
@tag_bp.route("/", methods=["POST"], endpoint="create_tag")
|
||||
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
|
||||
|
||||
@tag_bp.route("/", methods=["GET"], endpoint="list_tags")
|
||||
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
|
||||
|
||||
@tag_bp.route("/<uuid:tag_id>", methods=["DELETE"], endpoint="delete_tag")
|
||||
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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user