Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions backend/controllers/propertyController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 19 additions & 1 deletion backend/models/property.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down Expand Up @@ -163,5 +182,4 @@ propertySchema.virtual("isAvailable").get(function () {
});

const Property = mongoose.model("Property", propertySchema);

module.exports = Property;