Update v1.1.1-beta1
This commit is contained in:
98
routes/Dpanel/API/UserSearch.js
Normal file
98
routes/Dpanel/API/UserSearch.js
Normal file
@@ -0,0 +1,98 @@
|
||||
const express = require('express');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const router = express.Router();
|
||||
const authMiddleware = require('../../../Middlewares/authMiddleware');
|
||||
const { logger } = require('../../../config/logs');
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /api/dpanel/users/search:
|
||||
* get:
|
||||
* tags:
|
||||
* - Users
|
||||
* summary: Search for users by name or ID
|
||||
* parameters:
|
||||
* - in: query
|
||||
* name: term
|
||||
* required: true
|
||||
* schema:
|
||||
* type: string
|
||||
* description: Search term (name or ID)
|
||||
* - in: query
|
||||
* name: username
|
||||
* required: false
|
||||
* schema:
|
||||
* type: string
|
||||
* description: Exact username to search for
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Search results
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* users:
|
||||
* type: array
|
||||
* items:
|
||||
* type: object
|
||||
* properties:
|
||||
* id:
|
||||
* type: string
|
||||
* name:
|
||||
* type: string
|
||||
* profilePicture:
|
||||
* type: string
|
||||
* found:
|
||||
* type: boolean
|
||||
* user:
|
||||
* type: object
|
||||
* 500:
|
||||
* description: Internal server error
|
||||
*/
|
||||
|
||||
router.get('/', authMiddleware, async (req, res) => {
|
||||
try {
|
||||
const { term, username } = req.query;
|
||||
const userFilePath = path.join(__dirname, '../../../data', 'user.json');
|
||||
const users = JSON.parse(fs.readFileSync(userFilePath, 'utf8'));
|
||||
|
||||
if (username) {
|
||||
// Search for exact username match (for collaboration search)
|
||||
const user = users.find(u => u.name.toLowerCase() === username.toLowerCase());
|
||||
if (user) {
|
||||
res.json({
|
||||
found: true,
|
||||
user: {
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
profilePicture: user.profilePicture
|
||||
}
|
||||
});
|
||||
} else {
|
||||
res.json({ found: false });
|
||||
}
|
||||
} else if (term) {
|
||||
// Search for users by term (for general user search)
|
||||
const searchTerm = term.toLowerCase();
|
||||
const filteredUsers = users.filter(user =>
|
||||
user.name.toLowerCase().includes(searchTerm) ||
|
||||
user.id.toLowerCase().includes(searchTerm)
|
||||
).map(user => ({
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
profilePicture: user.profilePicture
|
||||
}));
|
||||
|
||||
res.json({ users: filteredUsers });
|
||||
} else {
|
||||
res.status(400).json({ error: 'Search term or username required' });
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error('Error searching users:', error);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user