1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-26 15:29:22 +00:00
comunicapiv2/src/controllers/SearchController.ts

56 lines
1.2 KiB
TypeScript
Raw Normal View History

2019-11-30 08:28:50 +00:00
import { RequestHandler } from "../entities/RequestHandler";
import { UserHelper } from "../helpers/UserHelper";
2020-03-21 14:00:46 +00:00
import { GroupsHelper } from "../helpers/GroupsHelper";
2019-11-30 08:28:50 +00:00
/**
* 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);
}
2020-03-21 14:00:46 +00:00
/**
* 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);
}
2019-11-30 08:28:50 +00:00
}