You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -2,7 +2,7 @@ import { AppDataSource } from "../database/data-source";
2
2
import{User}from"../database/entities/User";
3
3
import{Post}from"../database/entities/Post";
4
4
import{signToken}from"../utils/jwt";
5
-
import{Like}from"typeorm";
5
+
import{Like,Raw}from"typeorm";
6
6
7
7
exportclassUserService{
8
8
userRepository=AppDataSource.getRepository(User);
@@ -38,24 +38,158 @@ export class UserService {
38
38
returnawaitthis.userRepository.findOneBy({ id });
39
39
}
40
40
41
-
searchUsers=async(query: string)=>{
42
-
constsearchQuery=query;
41
+
searchUsers=async(
42
+
query: string,
43
+
page: number=1,
44
+
limit: number=10,
45
+
verifiedOnly: boolean=false,
46
+
sortBy: string="relevance"
47
+
)=>{
48
+
// Sanitize and trim the search query
49
+
constsearchQuery=query.trim();
50
+
51
+
// Return empty array if query is too short or empty
52
+
if(searchQuery.length<2){
53
+
return[];
54
+
}
43
55
44
-
returnthis.userRepository.find({
45
-
where: [
46
-
{name: Like(`%${searchQuery}%`)},
47
-
{ename: Like(`%${searchQuery}%`)},
48
-
],
49
-
select: {
50
-
id: true,
51
-
handle: true,
52
-
name: true,
53
-
description: true,
54
-
avatarUrl: true,
55
-
isVerified: true,
56
-
},
57
-
take: 10,
58
-
});
56
+
// Validate pagination parameters
57
+
if(page<1||limit<1||limit>100){
58
+
return[];
59
+
}
60
+
61
+
// Use query builder for more complex queries
62
+
constqueryBuilder=this.userRepository
63
+
.createQueryBuilder("user")
64
+
.select([
65
+
"user.id",
66
+
"user.handle",
67
+
"user.name",
68
+
"user.description",
69
+
"user.avatarUrl",
70
+
"user.isVerified"
71
+
])
72
+
.where(
73
+
"user.name ILIKE :query OR user.ename ILIKE :query OR user.handle ILIKE :query OR user.description ILIKE :query OR user.name ILIKE :fuzzyQuery OR user.ename ILIKE :fuzzyQuery OR user.handle ILIKE :fuzzyQuery",
74
+
{
75
+
query: `%${searchQuery}%`,
76
+
fuzzyQuery: `%${searchQuery.split('').join('%')}%`// Fuzzy search with wildcards between characters
"user.name ILIKE :query OR user.ename ILIKE :query OR user.handle ILIKE :query OR user.description ILIKE :query OR user.name ILIKE :fuzzyQuery OR user.ename ILIKE :fuzzyQuery OR user.handle ILIKE :fuzzyQuery",
129
+
{
130
+
query: `%${searchQuery}%`,
131
+
fuzzyQuery: `%${searchQuery.split('').join('%')}%`// Fuzzy search with wildcards between characters
0 commit comments