mirror of
				https://gitlab.com/comunic/comunicapiv2
				synced 2025-11-03 19:14:03 +00:00 
			
		
		
		
	Can search users in the database
This commit is contained in:
		@@ -15,8 +15,10 @@ export interface JoinTableInfo {
 | 
			
		||||
export interface QueryInformation {
 | 
			
		||||
	table: string,
 | 
			
		||||
	joins ?: Array<JoinTableInfo>,
 | 
			
		||||
	fields ?: Array<String>,
 | 
			
		||||
	fields ?: Array<string>,
 | 
			
		||||
	where ?: Object,
 | 
			
		||||
	customWhere ?: string,
 | 
			
		||||
	customWhereArgs ?: Array<string>,
 | 
			
		||||
	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 + " ";
 | 
			
		||||
 
 | 
			
		||||
@@ -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<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> {
 | 
			
		||||
		return new User({
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user