feat: add full project - backend, frontend, docker, specs and configs
This commit is contained in:
parent
b77c7d5a01
commit
e6cb06255b
24489 changed files with 61341 additions and 36 deletions
92
backend/app/routes/agents.py
Normal file
92
backend/app/routes/agents.py
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
from flask import Blueprint, jsonify, request
|
||||
from pydantic import ValidationError
|
||||
|
||||
from app.extensions import db
|
||||
from app.models.agent import Agent
|
||||
from app.schemas.agent import AgentIn, AgentOut
|
||||
from app.utils.auth import require_admin
|
||||
|
||||
agents_public_bp = Blueprint("agents_public", __name__, url_prefix="/api/v1")
|
||||
agents_admin_bp = Blueprint("agents_admin", __name__, url_prefix="/api/v1/admin")
|
||||
|
||||
|
||||
# ── Public ────────────────────────────────────────────────────────────────────
|
||||
|
||||
@agents_public_bp.get("/agents")
|
||||
def list_agents():
|
||||
agents = (
|
||||
Agent.query.filter_by(is_active=True)
|
||||
.order_by(Agent.display_order.asc(), Agent.id.asc())
|
||||
.all()
|
||||
)
|
||||
return jsonify([AgentOut.model_validate(a).model_dump() for a in agents])
|
||||
|
||||
|
||||
# ── Admin ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
@agents_admin_bp.get("/agents")
|
||||
@require_admin
|
||||
def admin_list_agents():
|
||||
agents = Agent.query.order_by(Agent.display_order.asc(), Agent.id.asc()).all()
|
||||
return jsonify([AgentOut.model_validate(a).model_dump() for a in agents])
|
||||
|
||||
|
||||
@agents_admin_bp.post("/agents")
|
||||
@require_admin
|
||||
def admin_create_agent():
|
||||
data = request.get_json(silent=True) or {}
|
||||
try:
|
||||
agent_in = AgentIn.model_validate(data)
|
||||
except ValidationError as exc:
|
||||
return jsonify({"error": "Dados inválidos", "details": exc.errors()}), 422
|
||||
|
||||
agent = Agent(
|
||||
name=agent_in.name,
|
||||
photo_url=agent_in.photo_url,
|
||||
creci=agent_in.creci,
|
||||
email=agent_in.email,
|
||||
phone=agent_in.phone,
|
||||
bio=agent_in.bio,
|
||||
is_active=agent_in.is_active,
|
||||
display_order=agent_in.display_order,
|
||||
)
|
||||
db.session.add(agent)
|
||||
db.session.commit()
|
||||
return jsonify(AgentOut.model_validate(agent).model_dump()), 201
|
||||
|
||||
|
||||
@agents_admin_bp.put("/agents/<int:agent_id>")
|
||||
@require_admin
|
||||
def admin_update_agent(agent_id: int):
|
||||
agent = Agent.query.get(agent_id)
|
||||
if agent is None:
|
||||
return jsonify({"error": "Corretor não encontrado"}), 404
|
||||
|
||||
data = request.get_json(silent=True) or {}
|
||||
try:
|
||||
agent_in = AgentIn.model_validate(data)
|
||||
except ValidationError as exc:
|
||||
return jsonify({"error": "Dados inválidos", "details": exc.errors()}), 422
|
||||
|
||||
agent.name = agent_in.name
|
||||
agent.photo_url = agent_in.photo_url
|
||||
agent.creci = agent_in.creci
|
||||
agent.email = agent_in.email
|
||||
agent.phone = agent_in.phone
|
||||
agent.bio = agent_in.bio
|
||||
agent.is_active = agent_in.is_active
|
||||
agent.display_order = agent_in.display_order
|
||||
db.session.commit()
|
||||
return jsonify(AgentOut.model_validate(agent).model_dump())
|
||||
|
||||
|
||||
@agents_admin_bp.delete("/agents/<int:agent_id>")
|
||||
@require_admin
|
||||
def admin_delete_agent(agent_id: int):
|
||||
agent = Agent.query.get(agent_id)
|
||||
if agent is None:
|
||||
return jsonify({"error": "Corretor não encontrado"}), 404
|
||||
|
||||
agent.is_active = False
|
||||
db.session.commit()
|
||||
return jsonify({"message": "Corretor desativado com sucesso"})
|
||||
Loading…
Add table
Add a link
Reference in a new issue