@@ -123,56 +123,115 @@ export class PostRepository extends AbstractRepository<PostModel> {
123123 . getMany ( ) ;
124124 }
125125
126- public async filterPostsByCategories ( categories : string [ ] ) : Promise < PostModel [ ] > {
127- return await this . repository
126+ public async filterPostsByCategories ( categories : string [ ] , skip : number , limit : number ) : Promise < PostModel [ ] > {
127+ const postIds = await this . repository
128128 . createQueryBuilder ( "post" )
129- . leftJoinAndSelect ( "post.user" , "user ")
129+ . select ( "post.id " )
130130 . innerJoin ( "post.categories" , "category" )
131131 . where ( "category.name IN (:...categories)" , { categories } )
132132 . andWhere ( "post.archive = false" )
133+ . skip ( skip )
134+ . take ( limit )
133135 . getMany ( ) ;
134- }
135136
136- public async filterPostsByPrice ( lowerBound : number , upperBound : number ) : Promise < PostModel [ ] > {
137+ const ids = postIds . map ( post => post . id ) ;
138+ if ( ids . length === 0 ) return [ ] ;
139+
137140 return await this . repository
138141 . createQueryBuilder ( "post" )
139142 . leftJoinAndSelect ( "post.user" , "user" )
140143 . leftJoinAndSelect ( "post.categories" , "categories" )
144+ . where ( "post.id IN (:...ids)" , { ids } )
145+ . getMany ( ) ;
146+ }
147+
148+ public async filterPostsByPrice ( lowerBound : number , upperBound : number , skip : number , limit : number ) : Promise < PostModel [ ] > {
149+ const postIds = await this . repository
150+ . createQueryBuilder ( "post" )
151+ . select ( "post.id" )
141152 . where ( "CASE WHEN post.altered_price = -1 THEN post.original_price ELSE post.altered_price END >= :lowerBound" , { lowerBound : lowerBound } )
142153 . andWhere ( "CASE WHEN post.altered_price = -1 THEN post.original_price ELSE post.altered_price END <= :upperBound" , { upperBound : upperBound } )
143154 . andWhere ( "post.archive = false" )
155+ . skip ( skip )
156+ . take ( limit )
157+ . getMany ( ) ;
158+
159+ const ids = postIds . map ( post => post . id ) ;
160+ if ( ids . length === 0 ) return [ ] ;
161+
162+ return await this . repository
163+ . createQueryBuilder ( "post" )
164+ . leftJoinAndSelect ( "post.user" , "user" )
165+ . leftJoinAndSelect ( "post.categories" , "categories" )
166+ . where ( "post.id IN (:...ids)" , { ids } )
144167 . orderBy ( "categories.name" , "ASC" )
145168 . getMany ( ) ;
146169 }
147170
148- public async filterPriceHighToLow ( ) : Promise < PostModel [ ] > {
171+ public async filterPriceHighToLow ( skip : number , limit : number ) : Promise < PostModel [ ] > {
172+ const postIds = await this . repository
173+ . createQueryBuilder ( "post" )
174+ . select ( "post.id" )
175+ . where ( "post.archive = false" )
176+ . orderBy ( "CASE WHEN post.altered_price = -1 THEN post.original_price ELSE post.altered_price END" , "DESC" )
177+ . skip ( skip )
178+ . take ( limit )
179+ . getMany ( ) ;
180+
181+ const ids = postIds . map ( post => post . id ) ;
182+ if ( ids . length === 0 ) return [ ] ;
183+
149184 return await this . repository
150185 . createQueryBuilder ( "post" )
151186 . leftJoinAndSelect ( "post.user" , "user" )
152187 . leftJoinAndSelect ( "post.categories" , "categories" )
153- . where ( "post.archive = false" )
188+ . where ( "post.id IN (:...ids)" , { ids } )
154189 . orderBy ( "CASE WHEN post.altered_price = -1 THEN post.original_price ELSE post.altered_price END" , "DESC" )
155190 . addOrderBy ( "categories.name" , "ASC" )
156191 . getMany ( ) ;
157192 }
158193
159- public async filterPriceLowToHigh ( ) : Promise < PostModel [ ] > {
194+ public async filterPriceLowToHigh ( skip : number , limit : number ) : Promise < PostModel [ ] > {
195+ const postIds = await this . repository
196+ . createQueryBuilder ( "post" )
197+ . select ( "post.id" )
198+ . where ( "post.archive = false" )
199+ . orderBy ( "CASE WHEN post.altered_price = -1 THEN post.original_price ELSE post.altered_price END" , "ASC" )
200+ . skip ( skip )
201+ . take ( limit )
202+ . getMany ( ) ;
203+
204+ const ids = postIds . map ( post => post . id ) ;
205+ if ( ids . length === 0 ) return [ ] ;
206+
160207 return await this . repository
161208 . createQueryBuilder ( "post" )
162209 . leftJoinAndSelect ( "post.user" , "user" )
163210 . leftJoinAndSelect ( "post.categories" , "categories" )
164- . where ( "post.archive = false" )
211+ . where ( "post.id IN (:...ids)" , { ids } )
165212 . orderBy ( "CASE WHEN post.altered_price = -1 THEN post.original_price ELSE post.altered_price END" , "ASC" )
166213 . addOrderBy ( "categories.name" , "ASC" )
167214 . getMany ( ) ;
168215 }
169216
170- public async filterNewlyListed ( ) : Promise < PostModel [ ] > {
217+ public async filterNewlyListed ( skip : number , limit : number ) : Promise < PostModel [ ] > {
218+ const postIds = await this . repository
219+ . createQueryBuilder ( "post" )
220+ . select ( "post.id" )
221+ . where ( "post.archive = false" )
222+ . orderBy ( "post.created" , "DESC" )
223+ . skip ( skip )
224+ . take ( limit )
225+ . getMany ( ) ;
226+
227+ const ids = postIds . map ( post => post . id ) ;
228+ if ( ids . length === 0 ) return [ ] ;
229+
171230 return await this . repository
172231 . createQueryBuilder ( "post" )
173232 . leftJoinAndSelect ( "post.user" , "user" )
174233 . leftJoinAndSelect ( "post.categories" , "categories" )
175- . where ( "post.archive = false" )
234+ . where ( "post.id IN (:...ids)" , { ids } )
176235 . orderBy ( "post.created" , "DESC" )
177236 . getMany ( ) ;
178237 }
@@ -188,22 +247,32 @@ export class PostRepository extends AbstractRepository<PostModel> {
188247 . getMany ( ) ;
189248 }
190249
191- public async filterByCondition ( conditions : string [ ] ) : Promise < PostModel [ ] > {
250+ public async filterByCondition ( conditions : string [ ] , skip : number , limit : number ) : Promise < PostModel [ ] > {
251+ const postIds = await this . repository
252+ . createQueryBuilder ( "post" )
253+ . select ( "post.id" )
254+ . where ( "post.condition IN (:...conditions)" , { conditions } )
255+ . andWhere ( "post.archive = false" )
256+ . skip ( skip )
257+ . take ( limit )
258+ . getMany ( ) ;
259+
260+ const ids = postIds . map ( post => post . id ) ;
261+ if ( ids . length === 0 ) return [ ] ;
262+
192263 return await this . repository
193264 . createQueryBuilder ( "post" )
194265 . leftJoinAndSelect ( "post.user" , "user" )
195266 . leftJoinAndSelect ( "post.categories" , "categories" )
196- . where ( "post.condition IN (:...conditions)" , { conditions } )
197- . andWhere ( "post.archive = false" )
267+ . where ( "post.id IN (:...ids)" , { ids } )
198268 . orderBy ( "categories.name" , "ASC" )
199269 . getMany ( ) ;
200270 }
201271
202- public async filterPostsUnified ( filterPostsUnifiedRequest : FilterPostsUnifiedRequest ) : Promise < PostModel [ ] > {
272+ public async filterPostsUnified ( filterPostsUnifiedRequest : FilterPostsUnifiedRequest , skip : number , limit : number ) : Promise < PostModel [ ] > {
203273 const qb = this . repository
204274 . createQueryBuilder ( "post" )
205- . leftJoinAndSelect ( "post.user" , "user" )
206- . leftJoinAndSelect ( "post.categories" , "categories" )
275+ . select ( "post.id" )
207276 . where ( "post.archive = false" ) ;
208277
209278 // Categories
@@ -257,7 +326,18 @@ export class PostRepository extends AbstractRepository<PostModel> {
257326 }
258327 }
259328
260- return await qb . getMany ( ) ;
329+ qb . skip ( skip ) . take ( limit ) ;
330+
331+ const postIds = await qb . getMany ( ) ;
332+ const ids = postIds . map ( post => post . id ) ;
333+ if ( ids . length === 0 ) return [ ] ;
334+
335+ return await this . repository
336+ . createQueryBuilder ( "post" )
337+ . leftJoinAndSelect ( "post.user" , "user" )
338+ . leftJoinAndSelect ( "post.categories" , "categories" )
339+ . where ( "post.id IN (:...ids)" , { ids } )
340+ . getMany ( ) ;
261341 }
262342
263343
@@ -289,7 +369,8 @@ export class PostRepository extends AbstractRepository<PostModel> {
289369
290370 public async archiveAllPostsByUserId ( userId : string ) : Promise < void > {
291371 await this . repository
292- . createQueryBuilder ( )
372+ . createQueryBuilder ( "post" )
373+ . leftJoin ( "post.user" , "user" )
293374 . update ( PostModel )
294375 . set ( { archive : true } )
295376 . where ( "user.firebaseUid = :userId" , { userId } )
@@ -325,7 +406,7 @@ export class PostRepository extends AbstractRepository<PostModel> {
325406 . createQueryBuilder ( "post" )
326407 . leftJoinAndSelect ( "post.user" , "user" )
327408 . where ( "post.id != :excludePostId" , { excludePostId } )
328- . andWhere ( "post. user != :excludeUserId" , { excludeUserId } )
409+ . andWhere ( "user.firebaseUid != :excludeUserId" , { excludeUserId } )
329410 . orderBy ( `embedding::vector <-> CAST('${ lit } ' AS vector(512))` )
330411 . setParameters ( { embedding : queryEmbedding } )
331412 . limit ( 10 )
@@ -341,9 +422,8 @@ export class PostRepository extends AbstractRepository<PostModel> {
341422 . createQueryBuilder ( "post" )
342423 . leftJoinAndSelect ( "post.user" , "user" )
343424 . where ( "post.embedding IS NOT NULL" )
344- . andWhere ( "post. user != :excludeUserId" , { excludeUserId } )
425+ . andWhere ( "user.firebaseUid != :excludeUserId" , { excludeUserId } )
345426 . orderBy ( `post.embedding::vector <-> CAST('${ lit } ' AS vector(512))` )
346- . setParameter ( "embedding" , embedding )
347427 . limit ( limit )
348428 . getMany ( ) ;
349429 }
0 commit comments