estatesfunding/ef-api/controllers/property.js

57 lines
1.7 KiB
JavaScript
Raw Normal View History

2024-09-12 08:11:07 +00:00
import PropertyModal from "../models/property.js";
import UserModal from "../models/user.js";
import mongoose from "mongoose";
2024-09-15 08:58:40 +00:00
// import { v4 as uuidv4 } from "uuid";
2024-09-12 08:11:07 +00:00
export const createProperty = async (req, res) => {
2024-09-15 08:11:03 +00:00
const propertyData = req.body;
// console.log('Property received:', propertyData);
2024-09-16 05:00:05 +00:00
function generateRandomNumber() {
return Math.floor(Math.random() * 90000) + 10000;
}
const randomNumber = generateRandomNumber().toString();
const propertyId = `EFPI${randomNumber}`;
2024-09-15 08:11:03 +00:00
const newProperty = new PropertyModal({
...propertyData,
2024-09-16 05:00:05 +00:00
creator: req.userId,
createdAt: new Date().toISOString(),
publishedAt: new Date().toISOString(),
currentYear: new Date().getFullYear(),
propertyId: propertyId,
2024-09-15 08:11:03 +00:00
});
try {
await newProperty.save();
res.status(201).json(newProperty);
} catch (error) {
res.status(404).json({ message: "Something went wrong" });
}
};
2024-09-12 08:11:07 +00:00
2024-09-20 11:18:56 +00:00
// Fetch property by userId.. Gets the specif user properties
2024-09-16 10:55:17 +00:00
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 });
}
2024-09-16 15:03:26 +00:00
};
2024-09-20 11:18:56 +00:00
// Fetch property by ID.. which is property view page
2024-09-16 15:03:26 +00:00
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" });
}
};
2024-09-17 14:36:35 +00:00