57 lines
1.7 KiB
JavaScript
57 lines
1.7 KiB
JavaScript
import PropertyModal from "../models/property.js";
|
|
import UserModal from "../models/user.js";
|
|
import mongoose from "mongoose";
|
|
// import { v4 as uuidv4 } from "uuid";
|
|
|
|
export const createProperty = async (req, res) => {
|
|
const propertyData = req.body;
|
|
// console.log('Property received:', propertyData);
|
|
function generateRandomNumber() {
|
|
return Math.floor(Math.random() * 90000) + 10000;
|
|
}
|
|
const randomNumber = generateRandomNumber().toString();
|
|
const propertyId = `EFPI${randomNumber}`;
|
|
|
|
const newProperty = new PropertyModal({
|
|
...propertyData,
|
|
creator: req.userId,
|
|
createdAt: new Date().toISOString(),
|
|
publishedAt: new Date().toISOString(),
|
|
currentYear: new Date().getFullYear(),
|
|
propertyId: propertyId,
|
|
});
|
|
|
|
try {
|
|
await newProperty.save();
|
|
|
|
res.status(201).json(newProperty);
|
|
} catch (error) {
|
|
res.status(404).json({ message: "Something went wrong" });
|
|
}
|
|
|
|
};
|
|
|
|
// Fetch property by userId.. Gets the specif user properties
|
|
export const getUserProperties = async (req, res) => {
|
|
try {
|
|
const userId = req.params.userId;
|
|
const properties = await PropertyModal.find({ userId: userId });
|
|
res.status(200).json(properties);
|
|
} catch (error) {
|
|
res.status(500).json({ message: "Error retrieving properties", error });
|
|
}
|
|
};
|
|
|
|
// Fetch property by ID.. which is property view page
|
|
export const getPropertyById = async (req, res) => {
|
|
const { propertyId } = req.params;
|
|
try {
|
|
const property = await PropertyModal.findOne({ propertyId });
|
|
res.status(200).json(property);
|
|
} catch (error) {
|
|
res.status(404).json({ message: "Something went wrong" });
|
|
}
|
|
};
|
|
|
|
|