1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-26 23:39:22 +00:00
comunicapiv2/src/controllers/SearchController.ts
2020-03-21 15:00:46 +01:00

56 lines
1.2 KiB
TypeScript

import { RequestHandler } from "../entities/RequestHandler";
import { UserHelper } from "../helpers/UserHelper";
import { GroupsHelper } from "../helpers/GroupsHelper";
/**
* Search controller
*
* @author Pierre HUBERT
*/
export class SearchController {
/**
* Search for user
*
* @param h Request handler
*/
public static async SearchUser(h : RequestHandler) {
// Get request
const query = h.postString("query", 1);
const limit = h.postInt("searchLimit", 5);
const list = await UserHelper.SearchUser(query, limit);
h.send(list);
}
/**
* Perform a global search (search both groups & users)
*
* @param h Request handler
*/
public static async SearchGlobal(h: RequestHandler) {
const query = h.postString("query", 1);
const limit = 10;
const results = [];
// First, search for groups
const groups = await GroupsHelper.SearchGroup(query, limit);
groups.map((e) => ({
kind: "group",
id: e
})).forEach((el) => results.push(el));
// Then search for users
const users = await UserHelper.SearchUser(query, limit);
users.map((e) => ({
kind: "user",
id: e
})).forEach((el) => results.push(el));
h.send(results);
}
}