mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2024-11-22 21:39:22 +00:00
Can search users in the database
This commit is contained in:
parent
c28206e5c0
commit
09d744d6f7
@ -3,6 +3,7 @@ import { RequestHandler } from "../entities/RequestHandler";
|
|||||||
import { AccountController } from "./AccountController";
|
import { AccountController } from "./AccountController";
|
||||||
import { UserController } from "./UserController";
|
import { UserController } from "./UserController";
|
||||||
import { ConversationsController } from "./ConversationsController";
|
import { ConversationsController } from "./ConversationsController";
|
||||||
|
import { SearchController } from "./SearchController";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Controllers routes
|
* Controllers routes
|
||||||
@ -51,4 +52,9 @@ export const Routes : Route[] = [
|
|||||||
|
|
||||||
{path: "/conversations/getInfoOne", cb: (h) => ConversationsController.GetInfoSingle(h)},
|
{path: "/conversations/getInfoOne", cb: (h) => ConversationsController.GetInfoSingle(h)},
|
||||||
{path: "/conversations/getInfosOne", cb: (h) => ConversationsController.GetInfoSingle(h)}, // Legacy
|
{path: "/conversations/getInfosOne", cb: (h) => ConversationsController.GetInfoSingle(h)}, // Legacy
|
||||||
|
|
||||||
|
|
||||||
|
// Search controller
|
||||||
|
{path: "/search/user", cb: (h) => SearchController.SearchUser(h)},
|
||||||
|
{path: "/user/search", cb: (h) => SearchController.SearchUser(h)}, // Legacy
|
||||||
]
|
]
|
28
src/controllers/SearchController.ts
Normal file
28
src/controllers/SearchController.ts
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import { RequestHandler } from "../entities/RequestHandler";
|
||||||
|
import { UserHelper } from "../helpers/UserHelper";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -15,8 +15,10 @@ export interface JoinTableInfo {
|
|||||||
export interface QueryInformation {
|
export interface QueryInformation {
|
||||||
table: string,
|
table: string,
|
||||||
joins ?: Array<JoinTableInfo>,
|
joins ?: Array<JoinTableInfo>,
|
||||||
fields ?: Array<String>,
|
fields ?: Array<string>,
|
||||||
where ?: Object,
|
where ?: Object,
|
||||||
|
customWhere ?: string,
|
||||||
|
customWhereArgs ?: Array<string>,
|
||||||
order ?: string,
|
order ?: string,
|
||||||
limit ?: number,
|
limit ?: number,
|
||||||
}
|
}
|
||||||
@ -108,6 +110,18 @@ export class DatabaseHelper {
|
|||||||
request = request.substr(0, request.length - 4)
|
request = request.substr(0, request.length - 4)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add custom WHERE clause
|
||||||
|
if(info.customWhere) {
|
||||||
|
|
||||||
|
if(!info.where)
|
||||||
|
request += " WHERE " + info.customWhere + " ";
|
||||||
|
else
|
||||||
|
request += " AND (" + info.customWhere + ")";
|
||||||
|
|
||||||
|
if(info.customWhereArgs)
|
||||||
|
info.customWhereArgs.forEach((e) => args.push(e));
|
||||||
|
}
|
||||||
|
|
||||||
// Order (if any)
|
// Order (if any)
|
||||||
if(info.order)
|
if(info.order)
|
||||||
request += " ORDER BY " + info.order + " ";
|
request += " ORDER BY " + info.order + " ";
|
||||||
|
@ -32,6 +32,28 @@ export class UserHelper {
|
|||||||
return this.DbToUser(result);
|
return this.DbToUser(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Search for user in the database
|
||||||
|
*
|
||||||
|
* @param query The query
|
||||||
|
* @param limit Limit for the request (default: 10)
|
||||||
|
* @returns The list of ids of the user
|
||||||
|
*/
|
||||||
|
public static async SearchUser(query: string, limit: number = 10) : Promise<Array<number>> {
|
||||||
|
|
||||||
|
query = "%" + query.replace(/\ /g, "%") + "%";
|
||||||
|
|
||||||
|
const request = await DatabaseHelper.Query({
|
||||||
|
fields: ["ID"],
|
||||||
|
table: TABLE_NAME,
|
||||||
|
customWhere: "(nom LIKE ?) || (prenom LIKE ?) || (CONCAT(prenom, '%', nom) LIKE ?) || (CONCAT(nom, '%', prenom) LIKE ?)",
|
||||||
|
customWhereArgs: [query, query, query, query],
|
||||||
|
limit: limit,
|
||||||
|
});
|
||||||
|
|
||||||
|
return request.map((e) => e.ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private static async DbToUser(row: any) : Promise<User> {
|
private static async DbToUser(row: any) : Promise<User> {
|
||||||
return new User({
|
return new User({
|
||||||
|
Loading…
Reference in New Issue
Block a user