diff --git a/backend/controllers/propertyController.js b/backend/controllers/propertyController.js index be041b4..15596de 100644 --- a/backend/controllers/propertyController.js +++ b/backend/controllers/propertyController.js @@ -34,6 +34,37 @@ const createProperty = async (req, res) => { res.status(500).json({ message: "Server Error" }); } }; +exports.searchProperties = async (req, res) => { + try { + const { city, state, lat, lng, radius = 5 } = req.query; + + let query = {}; + + // City / State filter + if (city) query.city = new RegExp(city, "i"); + if (state) query.state = new RegExp(state, "i"); + + // Geo-based search + if (lat && lng) { + query.location = { + $near: { + $geometry: { + type: "Point", + coordinates: [lng, lat] + }, + $maxDistance: radius * 1000 // km → meters + } + }; + } + + const properties = await Property.find(query); + + res.status(200).json(properties); + } catch (error) { + res.status(500).json({ message: error.message }); + } +}; + // @desc Get all properties // @route GET /api/properties diff --git a/backend/models/property.js b/backend/models/property.js index 695c946..a7d7f3e 100644 --- a/backend/models/property.js +++ b/backend/models/property.js @@ -2,6 +2,25 @@ const mongoose = require("mongoose"); const propertySchema = new mongoose.Schema( { + title: String, + price: Number, + category: String, + + city: String, + state: String, + area: String, + + location: { + type: { + type: String, + enum: ["Point"], + required: true + }, + coordinates: { + type: [Number], // [longitude, latitude] + required: true + } + } title: { type: String, required: [true, "Title is required"], @@ -163,5 +182,4 @@ propertySchema.virtual("isAvailable").get(function () { }); const Property = mongoose.model("Property", propertySchema); - module.exports = Property;