feat(transactions): add pagination support for user transactions
Some checks failed
Some checks failed
This commit is contained in:
parent
157e774e86
commit
35184807c0
11 changed files with 47 additions and 24 deletions
|
@ -18,4 +18,7 @@ public interface TransactionRepository extends JpaRepository<TransactionEntity,
|
|||
|
||||
@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);
|
||||
|
||||
@Query("SELECT COUNT(t) > ?2 + ?3 FROM TransactionEntity t WHERE t.user = ?1")
|
||||
Boolean hasMore(UserEntity userEntity, Integer limit, Integer offset);
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import de.szut.casino.deposit.TransactionRepository;
|
|||
import de.szut.casino.user.UserEntity;
|
||||
import de.szut.casino.user.UserService;
|
||||
import de.szut.casino.user.transaction.dto.GetTransactionDto;
|
||||
import de.szut.casino.user.transaction.dto.UserTransactionsDto;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
@ -20,17 +21,16 @@ public class GetTransactionService {
|
|||
@Autowired
|
||||
private TransactionRepository transactionRepository;
|
||||
|
||||
public List<TransactionEntity> getUserTransactions(String authToken, Integer limit, Integer offset) {
|
||||
public UserTransactionsDto getUserTransactionsDto(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, offset);
|
||||
} else {
|
||||
return this.transactionRepository.findAllByUserId(user.get());
|
||||
}
|
||||
List<TransactionEntity> transactionEntities = this.transactionRepository.findByUserIdWithLimit(user.get(), limit, offset);
|
||||
Boolean hasMore = this.transactionRepository.hasMore(user.get(), limit, offset);
|
||||
|
||||
return new UserTransactionsDto(mapTransactionsToDtos(transactionEntities), hasMore);
|
||||
}
|
||||
|
||||
return List.of();
|
||||
return new UserTransactionsDto(List.of(), false);
|
||||
}
|
||||
|
||||
public List<GetTransactionDto> mapTransactionsToDtos(List<TransactionEntity> transactions) {
|
||||
|
|
|
@ -2,6 +2,7 @@ package de.szut.casino.user.transaction;
|
|||
|
||||
import de.szut.casino.deposit.TransactionEntity;
|
||||
import de.szut.casino.user.transaction.dto.GetTransactionDto;
|
||||
import de.szut.casino.user.transaction.dto.UserTransactionsDto;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
@ -18,14 +19,14 @@ public class TransactionController {
|
|||
private GetTransactionService transactionService;
|
||||
|
||||
@GetMapping("/user/transactions")
|
||||
public ResponseEntity<List<GetTransactionDto>> getUserTransactions(
|
||||
public ResponseEntity<UserTransactionsDto> 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);
|
||||
UserTransactionsDto transactionEntities = this.transactionService.getUserTransactionsDto(authToken, limit, offset);
|
||||
|
||||
return ResponseEntity.ok(this.transactionService.mapTransactionsToDtos(transactionEntities));
|
||||
return ResponseEntity.ok(transactionEntities);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
package de.szut.casino.user.transaction.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@AllArgsConstructor
|
||||
public class UserTransactionsDto {
|
||||
public List<GetTransactionDto> transactions;
|
||||
public Boolean hasMore;
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue