1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-22 13:29:22 +00:00

Can search users in the database

This commit is contained in:
Pierre HUBERT 2019-11-30 09:28:50 +01:00
parent c28206e5c0
commit 09d744d6f7
4 changed files with 71 additions and 1 deletions

View File

@ -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
] ]

View 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);
}
}

View File

@ -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 + " ";

View File

@ -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({