diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index 2ea7059..4b01640 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -3,6 +3,7 @@ import { RequestHandler } from "../entities/RequestHandler"; import { AccountController } from "./AccountController"; import { UserController } from "./UserController"; import { ConversationsController } from "./ConversationsController"; +import { SearchController } from "./SearchController"; /** * Controllers routes @@ -51,4 +52,9 @@ export const Routes : Route[] = [ {path: "/conversations/getInfoOne", cb: (h) => ConversationsController.GetInfoSingle(h)}, {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 ] \ No newline at end of file diff --git a/src/controllers/SearchController.ts b/src/controllers/SearchController.ts new file mode 100644 index 0000000..389f71d --- /dev/null +++ b/src/controllers/SearchController.ts @@ -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); + } + +} \ No newline at end of file diff --git a/src/helpers/DatabaseHelper.ts b/src/helpers/DatabaseHelper.ts index e229726..637af6b 100644 --- a/src/helpers/DatabaseHelper.ts +++ b/src/helpers/DatabaseHelper.ts @@ -15,8 +15,10 @@ export interface JoinTableInfo { export interface QueryInformation { table: string, joins ?: Array, - fields ?: Array, + fields ?: Array, where ?: Object, + customWhere ?: string, + customWhereArgs ?: Array, order ?: string, limit ?: number, } @@ -108,6 +110,18 @@ export class DatabaseHelper { 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) if(info.order) request += " ORDER BY " + info.order + " "; diff --git a/src/helpers/UserHelper.ts b/src/helpers/UserHelper.ts index 7d4dff9..66f67e6 100644 --- a/src/helpers/UserHelper.ts +++ b/src/helpers/UserHelper.ts @@ -32,6 +32,28 @@ export class UserHelper { 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> { + + 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 { return new User({