feat: add pagination support for user transactions retrieval
This commit is contained in:
parent
9817fb95db
commit
03d67ef362
6 changed files with 92 additions and 17 deletions
|
@ -16,6 +16,6 @@ public interface TransactionRepository extends JpaRepository<TransactionEntity,
|
|||
@Query("SELECT t FROM TransactionEntity t WHERE t.user = ?1")
|
||||
List<TransactionEntity> findAllByUserId(UserEntity id);
|
||||
|
||||
@Query("SELECT t FROM TransactionEntity t WHERE t.user = ?1 ORDER BY t.createdAt DESC LIMIT ?2")
|
||||
List<TransactionEntity> findByUserIdWithLimit(UserEntity userEntity, Integer limit);
|
||||
@Query("SELECT t FROM TransactionEntity t WHERE t.user = ?1 ORDER BY t.createdAt DESC LIMIT ?2 OFFSET ?3")
|
||||
List<TransactionEntity> findByUserIdWithLimit(UserEntity userEntity, Integer limit, Integer offset);
|
||||
}
|
||||
|
|
|
@ -20,11 +20,11 @@ public class GetTransactionService {
|
|||
@Autowired
|
||||
private TransactionRepository transactionRepository;
|
||||
|
||||
public List<TransactionEntity> getUserTransactions(String authToken, Integer limit) {
|
||||
public List<TransactionEntity> getUserTransactions(String authToken, Integer limit, Integer offset) {
|
||||
Optional<UserEntity> user = this.userService.getCurrentUser(authToken);
|
||||
if (user.isPresent()) {
|
||||
if (limit != null && limit > 0) {
|
||||
return this.transactionRepository.findByUserIdWithLimit(user.get(), limit);
|
||||
return this.transactionRepository.findByUserIdWithLimit(user.get(), limit, offset);
|
||||
} else {
|
||||
return this.transactionRepository.findAllByUserId(user.get());
|
||||
}
|
||||
|
|
|
@ -18,8 +18,12 @@ public class TransactionController {
|
|||
private GetTransactionService transactionService;
|
||||
|
||||
@GetMapping("/user/transactions")
|
||||
public ResponseEntity<List<GetTransactionDto>> getUserTransactions(@RequestHeader("Authorization") String authToken, @RequestParam(value = "limit", required = false) Integer limit) {
|
||||
List<TransactionEntity> transactionEntities = this.transactionService.getUserTransactions(authToken, limit);
|
||||
public ResponseEntity<List<GetTransactionDto>> getUserTransactions(
|
||||
@RequestHeader("Authorization") String authToken,
|
||||
@RequestParam(value = "limit", required = false) Integer limit,
|
||||
@RequestParam(value = "offset", required = false) Integer offset
|
||||
) {
|
||||
List<TransactionEntity> transactionEntities = this.transactionService.getUserTransactions(authToken, limit, offset);
|
||||
|
||||
return ResponseEntity.ok(this.transactionService.mapTransactionsToDtos(transactionEntities));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue