@@ -27,64 +27,7 @@ func (c *Collection) Paginate(ctx context.Context, rawFilter any, s *usecasex.So
27
27
filter = And (rawFilter , "" , pFilter )
28
28
}
29
29
30
- sortKey := idKey
31
- sortOrder := 1
32
- if s != nil && s .Key != "" {
33
- sortKey = s .Key
34
- if s .Reverted {
35
- sortOrder = - 1
36
- }
37
- }
38
-
39
- if p .Cursor != nil && p .Cursor .Last != nil {
40
- sortOrder *= - 1
41
- }
42
-
43
- sort := bson.D {{Key : sortKey , Value : sortOrder }}
44
- if sortKey != idKey {
45
- sort = append (sort , bson.E {Key : idKey , Value : sortOrder })
46
- }
47
-
48
- findOpts := options .Find ().
49
- SetSort (sort ).
50
- SetLimit (limit (* p ))
51
-
52
- if p .Offset != nil {
53
- findOpts .SetSkip (p .Offset .Offset )
54
- }
55
-
56
- cursor , err := c .collection .Find (ctx , filter , append ([]* options.FindOptions {findOpts }, opts ... )... )
57
- if err != nil {
58
- return nil , rerror .ErrInternalByWithContext (ctx , fmt .Errorf ("failed to find: %w" , err ))
59
- }
60
- defer func () {
61
- _ = cursor .Close (ctx )
62
- }()
63
-
64
- count , err := c .collection .CountDocuments (ctx , rawFilter )
65
- if err != nil {
66
- return nil , rerror .ErrInternalByWithContext (ctx , fmt .Errorf ("failed to count: %w" , err ))
67
- }
68
-
69
- items , startCursor , endCursor , hasMore , err := consume (ctx , cursor , limit (* p ))
70
- if err != nil {
71
- return nil , err
72
- }
73
-
74
- if p .Cursor != nil && p .Cursor .Last != nil {
75
- reverse (items )
76
- startCursor , endCursor = endCursor , startCursor
77
- }
78
-
79
- for _ , item := range items {
80
- if err := consumer .Consume (item ); err != nil {
81
- return nil , err
82
- }
83
- }
84
-
85
- hasNextPage , hasPreviousPage := pageInfo (p , hasMore )
86
-
87
- return usecasex .NewPageInfo (count , startCursor , endCursor , hasNextPage , hasPreviousPage ), nil
30
+ return c .paginate (ctx , rawFilter , s , p , filter , consumer , opts )
88
31
}
89
32
90
33
func (c * Collection ) PaginateAggregation (ctx context.Context , pipeline []any , s * usecasex.Sort , p * usecasex.Pagination , consumer Consumer , opts ... * options.AggregateOptions ) (* usecasex.PageInfo , error ) {
@@ -327,3 +270,84 @@ func sortDirection(p usecasex.Pagination, s *usecasex.Sort) int {
327
270
}
328
271
return 1
329
272
}
273
+
274
+ func (c * Collection ) PaginateProject (ctx context.Context , rawFilter any , s * usecasex.Sort , p * usecasex.Pagination , consumer Consumer , opts ... * options.FindOptions ) (* usecasex.PageInfo , error ) {
275
+ if p == nil || (p .Cursor == nil && p .Offset == nil ) {
276
+ return nil , nil
277
+ }
278
+
279
+ pFilter , err := c .pageFilter (ctx , * p , s )
280
+ if err != nil {
281
+ return nil , rerror .ErrInternalByWithContext (ctx , err )
282
+ }
283
+
284
+ filter := rawFilter
285
+ if pFilter != nil {
286
+ filter = AddCondition (rawFilter , "" , pFilter )
287
+ }
288
+
289
+ return c .paginate (ctx , rawFilter , s , p , filter , consumer , opts )
290
+
291
+ }
292
+
293
+ func (c * Collection ) paginate (ctx context.Context , rawFilter any , s * usecasex.Sort , p * usecasex.Pagination , filter any , consumer Consumer , opts []* options.FindOptions ) (* usecasex.PageInfo , error ) {
294
+
295
+ sortKey := idKey
296
+ sortOrder := 1
297
+ if s != nil && s .Key != "" {
298
+ sortKey = s .Key
299
+ if s .Reverted {
300
+ sortOrder = - 1
301
+ }
302
+ }
303
+
304
+ if p .Cursor != nil && p .Cursor .Last != nil {
305
+ sortOrder *= - 1
306
+ }
307
+
308
+ sort := bson.D {{Key : sortKey , Value : sortOrder }}
309
+ if sortKey != idKey {
310
+ sort = append (sort , bson.E {Key : idKey , Value : sortOrder })
311
+ }
312
+
313
+ findOpts := options .Find ().
314
+ SetSort (sort ).
315
+ SetLimit (limit (* p ))
316
+
317
+ if p .Offset != nil {
318
+ findOpts .SetSkip (p .Offset .Offset )
319
+ }
320
+
321
+ cursor , err := c .collection .Find (ctx , filter , append ([]* options.FindOptions {findOpts }, opts ... )... )
322
+ if err != nil {
323
+ return nil , rerror .ErrInternalByWithContext (ctx , fmt .Errorf ("failed to find: %w" , err ))
324
+ }
325
+ defer func () {
326
+ _ = cursor .Close (ctx )
327
+ }()
328
+
329
+ count , err := c .collection .CountDocuments (ctx , rawFilter )
330
+ if err != nil {
331
+ return nil , rerror .ErrInternalByWithContext (ctx , fmt .Errorf ("failed to count: %w" , err ))
332
+ }
333
+
334
+ items , startCursor , endCursor , hasMore , err := consume (ctx , cursor , limit (* p ))
335
+ if err != nil {
336
+ return nil , err
337
+ }
338
+
339
+ if p .Cursor != nil && p .Cursor .Last != nil {
340
+ reverse (items )
341
+ startCursor , endCursor = endCursor , startCursor
342
+ }
343
+
344
+ for _ , item := range items {
345
+ if err := consumer .Consume (item ); err != nil {
346
+ return nil , err
347
+ }
348
+ }
349
+
350
+ hasNextPage , hasPreviousPage := pageInfo (p , hasMore )
351
+
352
+ return usecasex .NewPageInfo (count , startCursor , endCursor , hasNextPage , hasPreviousPage ), nil
353
+ }
0 commit comments