添加分页获取排行榜数据

This commit is contained in:
ETHEREAL 2024-06-08 20:23:35 +08:00
parent db7ffa2f78
commit 157894a8a6
7 changed files with 78 additions and 0 deletions

View File

@ -147,6 +147,11 @@
<artifactId>poi-ooxml</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
</dependencies>
<build>

View File

@ -1,6 +1,8 @@
package com.example.catchTheLetters.controller;
import com.example.catchTheLetters.entity.Level;
import com.example.catchTheLetters.entity.Page;
import com.example.catchTheLetters.entity.ScoreInfoPage;
import com.example.catchTheLetters.model.vo.RankVo;
import com.example.catchTheLetters.entity.ScoreInfo;
import com.example.catchTheLetters.service.LevelService;
@ -100,4 +102,9 @@ public class LevelController {
return R.ok(versionService.getVersion("level"));
}
@ApiOperation(value = "分页获取某个排行榜排名")
@GetMapping("/rank-page")
public R<ScoreInfoPage> getScores(@RequestBody Page page) {
return R.ok(levelService.getScoreInfo( page.getLevelId(),page.getPage(), page.getSize()));
}
}

View File

@ -0,0 +1,14 @@
package com.example.catchTheLetters.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Page {
private Integer page;
private Integer size;
private String levelId;
}

View File

@ -0,0 +1,15 @@
package com.example.catchTheLetters.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ScoreInfoPage {
private List<ScoreInfo> scores;
private Long total;
}

View File

@ -2,6 +2,7 @@ package com.example.catchTheLetters.service;
import com.example.catchTheLetters.entity.Level;
import com.example.catchTheLetters.entity.ScoreInfo;
import com.example.catchTheLetters.entity.ScoreInfoPage;
import com.example.catchTheLetters.model.vo.RankVo;
import com.example.catchTheLetters.utils.R;
@ -70,6 +71,14 @@ public interface LevelService {
*/
boolean update(Level level);
/**
* 分页获取某个关卡的排行榜
* @param levelId 关卡id
* @param page 页数
* @param size 大小
* @return 集合
*/
ScoreInfoPage getScoreInfo(String levelId, Integer page, Integer size);
}

View File

@ -2,12 +2,15 @@ package com.example.catchTheLetters.service.impl;
import com.example.catchTheLetters.entity.Level;
import com.example.catchTheLetters.entity.ScoreInfo;
import com.example.catchTheLetters.entity.ScoreInfoPage;
import com.example.catchTheLetters.model.vo.RankVo;
import com.example.catchTheLetters.service.AuthService;
import com.example.catchTheLetters.service.LevelService;
import com.example.catchTheLetters.utils.JwtUtil;
import com.example.catchTheLetters.utils.R;
import jakarta.annotation.Resource;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
@ -128,4 +131,12 @@ public class LevelServiceImpl implements LevelService {
var save = mongoTemplate.save(level);
return save.getId() != null;
}
@Override
public ScoreInfoPage getScoreInfo(String levelId, Integer page, Integer size) {
long total = mongoTemplate.count(new Query(),ScoreInfo.class)/size;
Pageable pageable = PageRequest.of(page - 1, size);
List<ScoreInfo> scores = mongoTemplate.find(new Query(Criteria.where("levelId").is(levelId)).with(pageable), ScoreInfo.class);
return new ScoreInfoPage(scores,total);
}
}

View File

@ -2,10 +2,16 @@ package com.example.catchTheLetters;
import com.example.catchTheLetters.controller.LevelController;
import com.example.catchTheLetters.entity.Level;
import com.example.catchTheLetters.entity.ScoreInfo;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import java.util.ArrayList;
import java.util.List;
@ -17,6 +23,9 @@ class TestInsertLevel {
@Resource
private LevelController levelController;
@Resource
private MongoTemplate mongoTemplate;
private final List<String> words = new ArrayList<>() {
{
add("apple");
@ -84,4 +93,12 @@ class TestInsertLevel {
levelController.create(level);
}
@Test
@Disabled
void pageTest(){
Pageable pageable = PageRequest.of(0, 1);
List<ScoreInfo> scores = mongoTemplate.find(new Query(Criteria.where("levelId").is("0")).with(pageable), ScoreInfo.class);
System.out.println(scores);
}
}