@@ -120,37 +120,29 @@ export class CreatorsService {
120120 async searchCreators (
121121 searchDto : SearchCreatorsDto ,
122122 ) : Promise < PaginatedResponseDto < PublicCreatorDto > > {
123- const { cursor , limit = 20 , q } = searchDto ;
123+ const { page = 1 , limit = 20 , q } = searchDto ;
124124 const trimmed = q ?. trim ( ) ;
125125
126126 const qb = this . userRepository
127127 . createQueryBuilder ( 'user' )
128128 . leftJoin ( 'user.creator' , 'creator' )
129129 . addSelect ( 'creator.bio' , 'creator_bio' )
130130 . where ( 'user.is_creator = :isCreator' , { isCreator : true } )
131- . orderBy ( 'user.id' , 'ASC' )
132- . take ( limit + 1 ) ;
133-
134- if ( cursor ) {
135- const cursorId = parseInt ( cursor , 10 ) ;
136- if ( ! isNaN ( cursorId ) ) {
137- qb . andWhere ( 'user.id > :cursorId' , { cursorId } ) ;
138- }
139- }
140-
141- const { entities, raw } = await qb . getRawAndEntities ( ) ;
142- const hasMore = entities . length > limit ;
143- if ( hasMore ) {
144- entities . pop ( ) ;
145- }
146-
147- let nextCursor : string | null = null ;
148- if ( entities . length > 0 ) {
149- nextCursor = String ( entities [ entities . length - 1 ] . id ) ;
131+ . orderBy ( 'user.username' , 'ASC' )
132+ . skip ( ( page - 1 ) * limit )
133+ . take ( limit ) ;
134+
135+ if ( trimmed ) {
136+ qb . andWhere (
137+ '(LOWER(user.display_name) LIKE :search OR LOWER(user.username) LIKE :search)' ,
138+ { search : `${ trimmed . toLowerCase ( ) } %` } ,
139+ ) ;
150140 }
151141
152- return new PaginatedResponseDto ( entities , limit , nextCursor , hasMore ) ;
153- }
142+ const [ { entities, raw } , total ] = await Promise . all ( [
143+ qb . getRawAndEntities ( ) ,
144+ qb . getCount ( ) ,
145+ ] ) ;
154146
155147 const data = entities . map ( ( user , index ) => {
156148 const dto = new PublicCreatorDto ( user , user . creator ) ;
@@ -159,7 +151,7 @@ export class CreatorsService {
159151 } ) ;
160152
161153 this . logger . debug (
162- `Creator search returned ${ data . length } rows for query "${ trimmed ?? '' } "` ,
154+ `Creator search returned ${ data . length } / ${ total } rows for query "${ trimmed ?? '' } "` ,
163155 ) ;
164156
165157 return new PaginatedResponseDto ( data , total , page , limit ) ;
0 commit comments