Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leaderboard functionality #52

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public ResponseEntity<Long> getRank(@RequestParam @NonNull Integer score) {
}

@GetMapping("/leaderboard")
public ResponseEntity<List<User>> getLeaderboard() {
return ResponseEntity.ok(userService.getLeaderboard());
public ResponseEntity<List<User>> getLeaderboard(@RequestParam @NonNull Integer pageSize,@RequestParam @NonNull Integer pageNum ) {
return ResponseEntity.ok(userService.getLeaderboard(pageSize,pageNum));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Collection;
Expand Down Expand Up @@ -32,8 +33,8 @@ public interface UserRepository extends JpaRepository<User, Integer> {

Long countByXpGreaterThan(Integer xp);

@Query("SELECT u FROM User u ORDER BY u.xp DESC LIMIT 5")
List<User> findAllByByOrderByXpDesc();
@Query("SELECT u FROM User u ORDER BY u.xp DESC LIMIT :pageSize OFFSET :offSetSize")
List<User> findAllByOrderByXpDesc(Integer pageSize, Integer offSetSize);


}
5 changes: 3 additions & 2 deletions src/main/java/com/pecacm/backend/services/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ public Long getRank(Integer score) {
return userRepository.countByXpGreaterThan(score) + 1;
}

public List<User> getLeaderboard() {
return userRepository.findAllByByOrderByXpDesc();
public List<User> getLeaderboard(Integer pageSize, Integer pageNum) {
Integer offSetSize=(pageNum-1)*pageSize;
return userRepository.findAllByOrderByXpDesc(pageSize,offSetSize);
}
}