实现了本地、云端数据库校验版本号接口
This commit is contained in:
parent
7c84d070d9
commit
de25a05c80
|
@ -4,6 +4,7 @@ import com.example.catchTheLetters.entity.Level;
|
|||
import com.example.catchTheLetters.model.vo.RankVo;
|
||||
import com.example.catchTheLetters.entity.ScoreInfo;
|
||||
import com.example.catchTheLetters.service.LevelService;
|
||||
import com.example.catchTheLetters.service.VersionService;
|
||||
import com.example.catchTheLetters.utils.R;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
|
@ -27,6 +28,9 @@ public class LevelController {
|
|||
@Resource
|
||||
private LevelService levelService;
|
||||
|
||||
@Resource
|
||||
private VersionService versionService;
|
||||
|
||||
@ApiOperation(value = "关卡列表(只返回ID、排序、名称和类型)")
|
||||
@GetMapping("/list")
|
||||
public R<List<Level>> list() {
|
||||
|
@ -43,20 +47,38 @@ public class LevelController {
|
|||
@ApiOperation(value = "关卡创建(web前端管理员提交)")
|
||||
@PostMapping("/create")
|
||||
public R create(@RequestBody Level level) {
|
||||
return levelService.levelCreate(level) ? R.ok() : R.fail();
|
||||
var res = levelService.levelCreate(level);
|
||||
// 如果创建成功,将当前UNIX时间戳作为版本号
|
||||
if (res) {
|
||||
versionService.setVersion("level", System.currentTimeMillis());
|
||||
return R.ok();
|
||||
}
|
||||
return R.fail();
|
||||
}
|
||||
|
||||
@ApiOperation(value = "关卡修改(web前端管理员提交)")
|
||||
@PostMapping("/update")
|
||||
public R update(@RequestBody Level level)
|
||||
{
|
||||
return levelService.update(level) ? R.ok() : R.fail();
|
||||
var res = levelService.update(level);
|
||||
// 如果修改成功,将当前UNIX时间戳作为版本号
|
||||
if (res) {
|
||||
versionService.setVersion("level", System.currentTimeMillis());
|
||||
return R.ok();
|
||||
}
|
||||
return R.fail();
|
||||
}
|
||||
|
||||
@ApiOperation(value = "关卡删除(web前端管理员提交)")
|
||||
@PostMapping("/delete")
|
||||
public R delete(String id) {
|
||||
return levelService.delete(id) ? R.ok() : R.fail();
|
||||
var res = levelService.delete(id);
|
||||
// 如果删除成功,将当前UNIX时间戳作为版本号
|
||||
if (res) {
|
||||
versionService.setVersion("level", System.currentTimeMillis());
|
||||
return R.ok();
|
||||
}
|
||||
return R.fail();
|
||||
}
|
||||
|
||||
@ApiOperation(value = "获取某个关卡排行榜数据")
|
||||
|
@ -71,4 +93,10 @@ public class LevelController {
|
|||
public R<ScoreInfo> settle(@RequestBody ScoreInfo scoreInfo, @RequestHeader("token")String token) {
|
||||
return levelService.settle(scoreInfo, token);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "获取表版本号(用于本地数据库和服务器数据库同步)")
|
||||
@GetMapping("/version")
|
||||
public R<Long> version() {
|
||||
return R.ok(versionService.getVersion("level"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,4 +19,12 @@ public class Version implements Serializable {
|
|||
* 数据表版本号
|
||||
*/
|
||||
private Long version;
|
||||
|
||||
public Version() {
|
||||
}
|
||||
|
||||
public Version(String table, Long version) {
|
||||
this.table = table;
|
||||
this.version = version;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
package com.example.catchTheLetters.service;
|
||||
|
||||
/**
|
||||
* 版本服务接口
|
||||
*
|
||||
* @author spyn
|
||||
*/
|
||||
public interface VersionService {
|
||||
/**
|
||||
* 获取某个表的版本号(用于Realm对MongoDB的数据表版本进行同步检测,版本号不同则进行同步)
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @return 版本号
|
||||
*/
|
||||
Long getVersion(String tableName);
|
||||
|
||||
/**
|
||||
* 设置某个表的版本号(用于Realm对MongoDB的数据表版本进行同步检测,版本号不同则进行同步)
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @param version 版本号
|
||||
*/
|
||||
void setVersion(String tableName, Long version);
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.example.catchTheLetters.service.impl;
|
||||
|
||||
import com.example.catchTheLetters.entity.Version;
|
||||
import com.example.catchTheLetters.service.VersionService;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class VersionServiceImpl implements VersionService {
|
||||
|
||||
@Resource
|
||||
private MongoTemplate mongoTemplate;
|
||||
|
||||
@Override
|
||||
public Long getVersion(String tableName) {
|
||||
var res = mongoTemplate.findOne(new Query(Criteria.where("table").is(tableName)), Version.class);
|
||||
return res == null ? null : res.getVersion();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setVersion(String tableName, Long version) {
|
||||
var res = mongoTemplate.findOne(new Query(Criteria.where("table").is(tableName)), Version.class);
|
||||
if (res == null) {
|
||||
mongoTemplate.save(new Version(tableName, version));
|
||||
} else {
|
||||
res.setVersion(version);
|
||||
mongoTemplate.save(res);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
package com.example.catchTheLetters;
|
||||
|
||||
import com.example.catchTheLetters.controller.LevelController;
|
||||
import com.example.catchTheLetters.entity.Level;
|
||||
import com.example.catchTheLetters.service.LevelService;
|
||||
import jakarta.annotation.Resource;
|
||||
|
@ -7,17 +8,16 @@ import org.junit.jupiter.api.Test;
|
|||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
@SpringBootTest
|
||||
class TestInsertLevel {
|
||||
|
||||
@Resource
|
||||
private LevelService levelService;
|
||||
private LevelController levelController;
|
||||
|
||||
@Test
|
||||
void testInsertLevel() {
|
||||
var words = new ArrayList<String>(){
|
||||
private final List<String> words = new ArrayList<>() {
|
||||
{
|
||||
add("apple");
|
||||
add("banana");
|
||||
|
@ -48,6 +48,8 @@ class TestInsertLevel {
|
|||
}
|
||||
};
|
||||
|
||||
@Test
|
||||
void testInsertLevel() {
|
||||
// 存20个关卡
|
||||
for (int i = 0; i < 20; i++) {
|
||||
var type = new Random().nextInt(2);
|
||||
|
@ -59,9 +61,25 @@ class TestInsertLevel {
|
|||
wordList.add(words.get(new Random().nextInt(words.size())));
|
||||
}
|
||||
// 时间全部设置为3分钟
|
||||
var level = new Level(Integer.toString(i), i, "Level" + i, type, 180000L, targetScore, wordList);
|
||||
var level = new Level(Integer.toString(i), i, "Level " + i, type, 180000L, targetScore, wordList);
|
||||
|
||||
levelService.levelCreate(level);
|
||||
levelController.create(level);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void insertALevel() {
|
||||
var type = new Random().nextInt(2);
|
||||
// 如果是1,则需要设置目标分数
|
||||
var targetScore = type == 1 ? new Random().nextInt(100) : null;
|
||||
// 如果是0,则单词列表5个就够,否则15个
|
||||
var wordList = new ArrayList<String>();
|
||||
for (int j = 0; j < (type == 0 ? 5 : 15); j++) {
|
||||
wordList.add(words.get(new Random().nextInt(words.size())));
|
||||
}
|
||||
// 时间全部设置为3分钟
|
||||
var level = new Level("20", 20, "Level 20", type, 180000L, targetScore, wordList);
|
||||
|
||||
levelController.create(level);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue