forked from Anteros-Code-Mentoria/poc-mvc-ocr
Inclusão do "delete" em "tag_routes"
This commit is contained in:
parent
d0960bb6ca
commit
715dd6e2b9
@ -2,17 +2,17 @@ from flask import Blueprint, request, jsonify
|
|||||||
from app.models.model_tag import Tag
|
from app.models.model_tag import Tag
|
||||||
from app.database import db
|
from app.database import db
|
||||||
from sqlalchemy.exc import SQLAlchemyError
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
tag_bp = Blueprint("tag_routes", __name__, url_prefix="/tags")
|
tag_bp = Blueprint("tag_routes", __name__, url_prefix="/tags")
|
||||||
|
|
||||||
@tag_bp.route("/", methods=["POST"])
|
@tag_bp.route("/", methods=["POST"], endpoint="create_tag")
|
||||||
def create_tag():
|
def create_tag():
|
||||||
try:
|
try:
|
||||||
nome = request.json.get("nome")
|
nome = request.json.get("nome")
|
||||||
if not nome:
|
if not nome:
|
||||||
return jsonify({"error": "Nome é obrigatório"}), 400
|
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()
|
existing_tag = Tag.query.filter_by(nome=nome).first()
|
||||||
if existing_tag:
|
if existing_tag:
|
||||||
return jsonify({"error": "Nome já está em uso"}), 409
|
return jsonify({"error": "Nome já está em uso"}), 409
|
||||||
@ -26,7 +26,7 @@ def create_tag():
|
|||||||
db.session.rollback()
|
db.session.rollback()
|
||||||
return jsonify({"error": "Erro ao criar tag", "details": str(e)}), 500
|
return jsonify({"error": "Erro ao criar tag", "details": str(e)}), 500
|
||||||
|
|
||||||
@tag_bp.route("/", methods=["GET"])
|
@tag_bp.route("/", methods=["GET"], endpoint="list_tags")
|
||||||
def list_tags():
|
def list_tags():
|
||||||
try:
|
try:
|
||||||
tags = Tag.query.all()
|
tags = Tag.query.all()
|
||||||
@ -37,3 +37,19 @@ def list_tags():
|
|||||||
|
|
||||||
except SQLAlchemyError as e:
|
except SQLAlchemyError as e:
|
||||||
return jsonify({"error": "Erro ao listar tags", "details": str(e)}), 500
|
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