|
| 1 | +package org.openpodcastapi.opa.pagination; |
| 2 | + |
| 3 | +import com.querydsl.core.types.dsl.*; |
| 4 | +import com.querydsl.jpa.impl.JPAQueryFactory; |
| 5 | +import org.springframework.stereotype.Repository; |
| 6 | + |
| 7 | +import java.time.Instant; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +/// Generic repository for returning paginated results. |
| 11 | +/// Works only with entities that implement [Cursorable]. |
| 12 | +@Repository |
| 13 | +public class CursorRepository { |
| 14 | + private final JPAQueryFactory queryFactory; |
| 15 | + |
| 16 | + /// All-args constructor |
| 17 | + /// |
| 18 | + /// @param queryFactory the JPAQueryFactory to use |
| 19 | + public CursorRepository(JPAQueryFactory queryFactory) { |
| 20 | + this.queryFactory = queryFactory; |
| 21 | + } |
| 22 | + |
| 23 | + /// Fetches a paginated set of results for an entity type with a cursor |
| 24 | + /// |
| 25 | + /// @param <T> the [Cursorable] entity type |
| 26 | + /// @param <Q> the QueryDSL type of the entity |
| 27 | + /// @param qEntity the entity (as its QueryDSL type) |
| 28 | + /// @param cursor the cursor used to filter results |
| 29 | + /// @param limit the number of results to fetch |
| 30 | + /// @param additionalFilter any additional [BooleanExpression] to apply to the query |
| 31 | + /// @param forward whether to cursor forwards or backwards |
| 32 | + /// @return a paginated response |
| 33 | + public <T extends Cursorable, Q extends EntityPathBase<T>> CursorPage<T> findWithCursor( |
| 34 | + Q qEntity, |
| 35 | + CursorPayload cursor, |
| 36 | + int limit, |
| 37 | + BooleanExpression additionalFilter, |
| 38 | + boolean forward |
| 39 | + ) { |
| 40 | + // Get the `createdAt` timestamp of the entity |
| 41 | + final var createdAtPath = Expressions.dateTimePath(Instant.class, qEntity, "createdAt"); |
| 42 | + // Get the `id` of the entity |
| 43 | + final var idPath = Expressions.numberPath(Long.class, qEntity, "id"); |
| 44 | + |
| 45 | + // Create the cursor pagination predicate |
| 46 | + final var predicate = buildKeysetPredicate(createdAtPath, idPath, cursor, forward); |
| 47 | + |
| 48 | + final List<T> results = queryFactory |
| 49 | + .selectFrom(qEntity) |
| 50 | + .where(additionalFilter, predicate) |
| 51 | + .orderBy(createdAtPath.desc(), idPath.desc()) |
| 52 | + .limit(limit) |
| 53 | + .fetch(); |
| 54 | + |
| 55 | + return CursorPage.of( |
| 56 | + results, |
| 57 | + limit, |
| 58 | + e -> new CursorPayload(e.getCreatedAt(), e.getId()) |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + /// Helper function to fetch pageable results using `id` and `createdAt` fields |
| 63 | + /// |
| 64 | + /// @param createdAt the created at timestamp of the entity |
| 65 | + /// @param id the database ID of the entity |
| 66 | + /// @param cursor the cursor payload to use for pagination |
| 67 | + /// @param forward whether to search forward or backwards (older or newer records) |
| 68 | + private BooleanExpression buildKeysetPredicate( |
| 69 | + DateTimePath<Instant> createdAt, |
| 70 | + NumberPath<Long> id, |
| 71 | + CursorPayload cursor, |
| 72 | + boolean forward |
| 73 | + ) { |
| 74 | + if (cursor == null) return null; |
| 75 | + |
| 76 | + if (forward) { |
| 77 | + return createdAt.lt(cursor.createdAt()) |
| 78 | + .or(createdAt.eq(cursor.createdAt()) |
| 79 | + .and(id.lt(cursor.id()))); |
| 80 | + } else { |
| 81 | + return createdAt.gt(cursor.createdAt()) |
| 82 | + .or(createdAt.eq(cursor.createdAt()) |
| 83 | + .and(id.gt(cursor.id()))); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments