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
63
backend/app/routes/catalog.py
Normal file
63
backend/app/routes/catalog.py
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
from flask import Blueprint, jsonify
|
||||
from sqlalchemy import func
|
||||
|
||||
from app.extensions import db
|
||||
from app.models.catalog import Amenity, PropertyType
|
||||
from app.models.imobiliaria import Imobiliaria
|
||||
from app.models.property import Property
|
||||
from app.schemas.catalog import AmenityOut, ImobiliariaOut, PropertyTypeOut
|
||||
|
||||
catalog_bp = Blueprint("catalog", __name__, url_prefix="/api/v1")
|
||||
|
||||
|
||||
@catalog_bp.get("/property-types")
|
||||
def list_property_types():
|
||||
"""Return top-level categories with their subtypes nested."""
|
||||
# Build count_map for subtypes (leaf nodes with parent_id IS NOT NULL)
|
||||
rows = (
|
||||
db.session.query(PropertyType.id, func.count(Property.id).label("cnt"))
|
||||
.filter(PropertyType.parent_id.isnot(None))
|
||||
.outerjoin(
|
||||
Property,
|
||||
(Property.subtype_id == PropertyType.id) & (Property.is_active.is_(True)),
|
||||
)
|
||||
.group_by(PropertyType.id)
|
||||
.all()
|
||||
)
|
||||
count_map: dict[int, int] = {row.id: row.cnt for row in rows}
|
||||
|
||||
categories = (
|
||||
PropertyType.query.filter_by(parent_id=None).order_by(PropertyType.id).all()
|
||||
)
|
||||
|
||||
def serialize_category(cat) -> dict:
|
||||
data = PropertyTypeOut.model_validate(cat).model_dump(mode="json")
|
||||
data["subtypes"] = [
|
||||
{**sub, "property_count": count_map.get(sub["id"], 0)}
|
||||
for sub in data["subtypes"]
|
||||
]
|
||||
return data
|
||||
|
||||
return jsonify([serialize_category(c) for c in categories])
|
||||
|
||||
|
||||
@catalog_bp.get("/amenities")
|
||||
def list_amenities():
|
||||
"""Return all amenities grouped."""
|
||||
amenities = Amenity.query.order_by(Amenity.group, Amenity.name).all()
|
||||
return jsonify(
|
||||
[AmenityOut.model_validate(a).model_dump(mode="json") for a in amenities]
|
||||
)
|
||||
|
||||
|
||||
@catalog_bp.get("/imobiliarias")
|
||||
def list_imobiliarias():
|
||||
"""Return active imobiliárias ordered by display_order."""
|
||||
items = (
|
||||
Imobiliaria.query.filter_by(is_active=True)
|
||||
.order_by(Imobiliaria.display_order, Imobiliaria.name)
|
||||
.all()
|
||||
)
|
||||
return jsonify(
|
||||
[ImobiliariaOut.model_validate(i).model_dump(mode="json") for i in items]
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue