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
109
backend/app/routes/client_area.py
Normal file
109
backend/app/routes/client_area.py
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
import uuid as _uuid
|
||||
from flask import Blueprint, request, jsonify, g
|
||||
from pydantic import ValidationError
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from app import db
|
||||
from app.models.saved_property import SavedProperty
|
||||
from app.models.visit_request import VisitRequest
|
||||
from app.models.boleto import Boleto
|
||||
from app.models.property import Property
|
||||
from app.schemas.client_area import (
|
||||
SavedPropertyOut,
|
||||
FavoriteIn,
|
||||
VisitRequestOut,
|
||||
BoletoOut,
|
||||
)
|
||||
from app.utils.auth import require_auth
|
||||
|
||||
client_bp = Blueprint("client", __name__)
|
||||
|
||||
|
||||
@client_bp.get("/favorites")
|
||||
@require_auth
|
||||
def get_favorites():
|
||||
saved = (
|
||||
SavedProperty.query.filter_by(user_id=g.current_user_id)
|
||||
.order_by(SavedProperty.created_at.desc())
|
||||
.all()
|
||||
)
|
||||
return (
|
||||
jsonify(
|
||||
[SavedPropertyOut.model_validate(s).model_dump(mode="json") for s in saved]
|
||||
),
|
||||
200,
|
||||
)
|
||||
|
||||
|
||||
@client_bp.post("/favorites")
|
||||
@require_auth
|
||||
def add_favorite():
|
||||
try:
|
||||
data = FavoriteIn.model_validate(request.get_json() or {})
|
||||
except ValidationError as e:
|
||||
return jsonify({"error": e.errors(include_url=False)}), 422
|
||||
|
||||
try:
|
||||
prop_uuid = _uuid.UUID(data.property_id)
|
||||
except ValueError:
|
||||
return jsonify({"error": "property_id inválido"}), 422
|
||||
prop = db.session.get(Property, prop_uuid)
|
||||
if not prop:
|
||||
return jsonify({"error": "Imóvel não encontrado"}), 404
|
||||
|
||||
saved = SavedProperty(user_id=g.current_user_id, property_id=prop_uuid)
|
||||
db.session.add(saved)
|
||||
try:
|
||||
db.session.commit()
|
||||
except IntegrityError:
|
||||
db.session.rollback()
|
||||
return jsonify({"error": "Imóvel já está nos favoritos"}), 409
|
||||
|
||||
return jsonify(SavedPropertyOut.model_validate(saved).model_dump(mode="json")), 201
|
||||
|
||||
|
||||
@client_bp.delete("/favorites/<property_id>")
|
||||
@require_auth
|
||||
def remove_favorite(property_id: str):
|
||||
try:
|
||||
prop_uuid = _uuid.UUID(property_id)
|
||||
except ValueError:
|
||||
return jsonify({"error": "property_id inválido"}), 422
|
||||
saved = SavedProperty.query.filter_by(
|
||||
user_id=g.current_user_id, property_id=prop_uuid
|
||||
).first()
|
||||
if not saved:
|
||||
return jsonify({"error": "Favorito não encontrado"}), 404
|
||||
|
||||
db.session.delete(saved)
|
||||
db.session.commit()
|
||||
return "", 204
|
||||
|
||||
|
||||
@client_bp.get("/visits")
|
||||
@require_auth
|
||||
def get_visits():
|
||||
visits = (
|
||||
VisitRequest.query.filter_by(user_id=g.current_user_id)
|
||||
.order_by(VisitRequest.created_at.desc())
|
||||
.all()
|
||||
)
|
||||
return (
|
||||
jsonify(
|
||||
[VisitRequestOut.model_validate(v).model_dump(mode="json") for v in visits]
|
||||
),
|
||||
200,
|
||||
)
|
||||
|
||||
|
||||
@client_bp.get("/boletos")
|
||||
@require_auth
|
||||
def get_boletos():
|
||||
boletos = (
|
||||
Boleto.query.filter_by(user_id=g.current_user_id)
|
||||
.order_by(Boleto.due_date.asc())
|
||||
.all()
|
||||
)
|
||||
return (
|
||||
jsonify([BoletoOut.model_validate(b).model_dump(mode="json") for b in boletos]),
|
||||
200,
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue