create search api route
This commit is contained in:
parent
cd79548dfc
commit
917b01c521
3 changed files with 48 additions and 6 deletions
|
@ -1,7 +1,6 @@
|
||||||
import { useEffect, useState } from "react"
|
import { useEffect, useState } from "react"
|
||||||
import {useParams} from 'react-router-dom'
|
import {useParams} from 'react-router-dom'
|
||||||
import {Swiper, SwiperSlide} from 'swiper/react'
|
import {Swiper, SwiperSlide} from 'swiper/react'
|
||||||
import { useSelector } from 'react-redux';
|
|
||||||
import 'swiper/css';
|
import 'swiper/css';
|
||||||
import 'swiper/css/free-mode';
|
import 'swiper/css/free-mode';
|
||||||
import 'swiper/css/navigation';
|
import 'swiper/css/navigation';
|
||||||
|
@ -16,7 +15,6 @@ const Listing = () => {
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const [error, setError] = useState(false);
|
const [error, setError] = useState(false);
|
||||||
const [listing, setListing] = useState(null);
|
const [listing, setListing] = useState(null);
|
||||||
const { currentUser } = useSelector((state) => state.user);
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchListing = async() =>{
|
const fetchListing = async() =>{
|
||||||
try {
|
try {
|
||||||
|
@ -89,10 +87,10 @@ const Listing = () => {
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<p className='text-slate-800 flex flex-col mb-6'>
|
<p className='text-slate-800 flex flex-col mb-6'>
|
||||||
<span className='font-semibold text-black mt-4 mb-2 text-xl'>About this property</span>
|
<span className='font-semibold text-black mt-4 mb-2 text-xl capitalize'>About this property</span>
|
||||||
<span>{listing.description}</span>
|
<span>{listing.description}</span>
|
||||||
</p>
|
</p>
|
||||||
<button className='bg-blue-950 text-white rounded-md uppercase hover:opacity-95 p-2 mb-10 text-sm'>Contact Agent</button>
|
<button onClick={() => window.location = 'mailto:chicagoland@example.com'} className='bg-blue-950 text-white rounded-md uppercase hover:opacity-95 p-2 mb-10 text-sm'>Contact Agent</button>
|
||||||
</div>
|
</div>
|
||||||
</>
|
</>
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,4 +56,47 @@ export const getListing = async (req, res, next) => {
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
|
export const getListings = async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
const limit = parseInt(req.query.limit) || 9;
|
||||||
|
const startIndex = parseInt(req.query.startIndex) || 0;
|
||||||
|
|
||||||
|
let offer = req.query.offer;
|
||||||
|
if(offer === undefined || offer === 'false'){
|
||||||
|
offer = {$in: [false, true]};
|
||||||
|
}
|
||||||
|
let furnished = req.query.furnished;
|
||||||
|
if(furnished === undefined || furnished === 'false'){
|
||||||
|
furnished = {$in: [false, true]};
|
||||||
|
}
|
||||||
|
let parking = req.query.parking;
|
||||||
|
if(parking === undefined || parking === 'false'){
|
||||||
|
parking = {$in: [false, true]};
|
||||||
|
}
|
||||||
|
let type = req.query.type;
|
||||||
|
if(type === undefined || type === 'all'){
|
||||||
|
type = {$in: ['sale', 'rent']};
|
||||||
|
}
|
||||||
|
|
||||||
|
const searchTerm = req.query.searchTerm || '';
|
||||||
|
const sort = req.query.sort || 'createdAt';
|
||||||
|
const order = req.query.order || 'desc';
|
||||||
|
const listings = await Listing.find({
|
||||||
|
name: {
|
||||||
|
$regex: searchTerm,
|
||||||
|
$options: 'i'
|
||||||
|
},
|
||||||
|
offer,
|
||||||
|
furnished,
|
||||||
|
parking,
|
||||||
|
type,
|
||||||
|
}).sort({ [sort]: order }).limit(limit).skip(startIndex);
|
||||||
|
|
||||||
|
return res.status(200).json(listings);
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
|
};
|
|
@ -1,5 +1,5 @@
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
import { createListing, deleteListing, updateListing, getListing } from '../controllers/listing.controller.js';
|
import { createListing, deleteListing, updateListing, getListing, getListings } from '../controllers/listing.controller.js';
|
||||||
import { verifyToken } from '../utils/verifyUser.js';
|
import { verifyToken } from '../utils/verifyUser.js';
|
||||||
|
|
||||||
|
|
||||||
|
@ -9,5 +9,6 @@ router.post('/create', verifyToken, createListing)
|
||||||
router.delete('/delete/:id', verifyToken, deleteListing)
|
router.delete('/delete/:id', verifyToken, deleteListing)
|
||||||
router.post('/update/:id', verifyToken, updateListing)
|
router.post('/update/:id', verifyToken, updateListing)
|
||||||
router.get('/get/:id', getListing)
|
router.get('/get/:id', getListing)
|
||||||
|
router.get('/get', getListings)
|
||||||
|
|
||||||
export default router;
|
export default router;
|
Loading…
Add table
Reference in a new issue