lol
This commit is contained in:
parent
5892c00ae7
commit
a4c2ffa119
2
app.js
2
app.js
@ -5,6 +5,7 @@ const loginRoutes = require('./src/routes/v1/authRoutes');
|
||||
const carsRoutes = require('./src/routes/v1/carsRoutes');
|
||||
const collectionRoutes = require('./src/routes/v1/collectionRoutes');
|
||||
const postRoutes = require('./src/routes/v1/postsRoutes');
|
||||
const searchRoutes = require('./src/routes/v1/searchRoutes');
|
||||
const path = require('path');
|
||||
|
||||
const app = express();
|
||||
@ -29,6 +30,7 @@ app.use('/api/v1/login', loginRoutes);
|
||||
app.use('/api/v1/cars', carsRoutes);
|
||||
app.use('/api/v1/collections', collectionRoutes);
|
||||
app.use('/api/v1/posts', postRoutes);
|
||||
app.use('/api/v1/search', searchRoutes);
|
||||
|
||||
app.use((req, res, next) => {
|
||||
res.status(404).json({ message: 'Route not found' });
|
||||
|
||||
73
src/controllers/searchController.js
Normal file
73
src/controllers/searchController.js
Normal file
@ -0,0 +1,73 @@
|
||||
const User = require('../models/user');
|
||||
const Car = require('../models/cars');
|
||||
const Post = require('../models/post');
|
||||
|
||||
exports.searchAll = async (req, res) => {
|
||||
try {
|
||||
const q = (req.query.q || '').trim();
|
||||
if (!q) return res.status(400).json({ message: 'Query is required' });
|
||||
|
||||
// kick off all three searches in parallel
|
||||
const [ users, cars, posts ] = await Promise.all([
|
||||
User.find({ username: { $regex: q, $options: 'i' } })
|
||||
.select('username profile_pic')
|
||||
.limit(5)
|
||||
.lean(),
|
||||
|
||||
Car.find({
|
||||
$or: [
|
||||
{ make: { $regex: q, $options: 'i' } },
|
||||
{ model: { $regex: q, $options: 'i' } }
|
||||
]
|
||||
})
|
||||
.select('make model year image')
|
||||
.limit(5)
|
||||
.lean(),
|
||||
|
||||
Post.find({ content: { $regex: q, $options: 'i' } })
|
||||
.populate('user_id', 'username profile_pic')
|
||||
.sort({ timestamp: -1 })
|
||||
.limit(5)
|
||||
.lean()
|
||||
]);
|
||||
|
||||
const host = req.get('host');
|
||||
const protocol = req.protocol;
|
||||
|
||||
// map into a unified result shape
|
||||
const userResults = users.map(u => ({
|
||||
type: 'user',
|
||||
id: u._id,
|
||||
username: u.username,
|
||||
avatar: `${protocol}://${host}${u.profile_pic}`,
|
||||
}));
|
||||
|
||||
const carResults = cars.map(c => ({
|
||||
type: 'car',
|
||||
id: c._id,
|
||||
title: `${c.make} ${c.model} (${c.year})`,
|
||||
image: `${protocol}://${host}${c.image}`,
|
||||
}));
|
||||
|
||||
const postResults = posts.map(p => ({
|
||||
type: 'post',
|
||||
id: p._id,
|
||||
snippet: p.content.slice(0, 50) + (p.content.length > 50 ? '…' : ''),
|
||||
image: p.image
|
||||
? `${protocol}://${host}${p.image}`
|
||||
: null,
|
||||
author: p.user_id.username,
|
||||
}));
|
||||
|
||||
// return a single combined array
|
||||
return res.json([
|
||||
...userResults,
|
||||
...carResults,
|
||||
...postResults
|
||||
]);
|
||||
|
||||
} catch (err) {
|
||||
console.error('searchAll error:', err);
|
||||
return res.status(500).json({ message: err.message });
|
||||
}
|
||||
};
|
||||
8
src/routes/v1/searchRoutes.js
Normal file
8
src/routes/v1/searchRoutes.js
Normal file
@ -0,0 +1,8 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const { searchAll } = require('../../controllers/searchController');
|
||||
const auth = require('../../middleware/authMiddleware');
|
||||
|
||||
router.get('/', auth, searchAll);
|
||||
|
||||
module.exports = router;
|
||||
Loading…
x
Reference in New Issue
Block a user