added SearchController

This commit is contained in:
SrGooglo 2023-05-24 17:41:26 +00:00
parent 318a62fe35
commit 1782db93c1
3 changed files with 50 additions and 30 deletions

View File

@ -0,0 +1,20 @@
import getMutuals from "@services/getMutuals"
export default {
method: "GET",
route: "/quick",
middlewares: ["withAuthentication"],
fn: async (req, res) => {
let mutuals = await getMutuals({
from_user_id: req.user._id.toString(),
}).catch((error) => {
console.error(error)
return []
})
return res.json({
friends: mutuals,
})
}
}

View File

@ -0,0 +1,28 @@
import { User } from "@models"
export default {
method: "GET",
route: "/",
middlewares: ["withOptionalAuthentication"],
fn: async (req, res) => {
const { keywords = "" } = req.query
let suggestions = {}
// search users by username or name
const users = await User.find({
$or: [
{ username: { $regex: keywords, $options: "i" } },
{ fullName: { $regex: keywords, $options: "i" } },
],
})
.limit(5)
.select("username fullName avatar verified")
if (users.length > 0) {
suggestions["users"] = users
}
return res.json(suggestions)
}
}

View File

@ -1,37 +1,9 @@
import { Controller } from "linebridge/dist/server"
import { User, Post } from "@models"
import generateEndpointsFromDir from "linebridge/dist/server/lib/generateEndpointsFromDir"
export default class SearchController extends Controller {
static refName = "SearchController"
static useRoute = "/search"
httpEndpoints = {
get: {
"/": {
middlewares: ["withOptionalAuthentication"],
fn: async (req, res) => {
const { keywords = "" } = req.query
let suggestions = {}
// search users by username or name
const users = await User.find({
$or: [
{ username: { $regex: keywords, $options: "i" } },
{ fullName: { $regex: keywords, $options: "i" } },
],
})
.limit(5)
.select("username fullName avatar verified")
if (users.length > 0) {
suggestions["users"] = users
}
return res.json(suggestions)
}
}
}
}
httpEndpoints = generateEndpointsFromDir(__dirname + "/endpoints")
}