2024年4月12日

关卡修改、删除
This commit is contained in:
Ch3nGO 2024-04-12 11:05:15 +08:00
parent 6e61ffd198
commit a7442babbe
3 changed files with 75 additions and 5 deletions

View File

@ -3,10 +3,12 @@ package com.example.catchTheLetters.controller;
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.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
@ -20,6 +22,9 @@ import org.springframework.web.bind.annotation.*;
@Api(tags = "关卡API")
@RequestMapping("/level")
public class LevelController {
@Resource
private LevelService levelService;
@ApiOperation(value = "关卡列表只返回ID、名称和类型")
@GetMapping("/list")
public R<Level> list() {
@ -41,14 +46,15 @@ public class LevelController {
@ApiOperation(value = "关卡修改web前端管理员提交")
@PostMapping("/update")
public R update(@RequestBody Level level) {
return null;
public R update(@RequestBody Level level)
{
return levelService.update(level) ? R.ok() : R.fail();
}
@ApiOperation(value = "关卡删除web前端管理员提交")
@PostMapping("/delete")
public R delete(Long id) {
return null;
public R delete(String id) {
return levelService.delete(id) ? R.ok() : R.fail();
}
@ApiOperation(value = "获取某个关卡排行榜数据")

View File

@ -1,5 +1,7 @@
package com.example.catchTheLetters.service;
import com.example.catchTheLetters.entity.Level;
/**
* @author 慕华
* @date 2024/4/10
@ -7,4 +9,19 @@ package com.example.catchTheLetters.service;
* @description
*/
public interface LevelService {
/**
* 删除关卡
* @param id 关卡id
* @return 是否成功
*/
boolean delete(String id);
/**
* 更新关卡
* @param level 关卡
* @return 是否成功
*/
boolean update(Level level);
}

View File

@ -1,5 +1,19 @@
package com.example.catchTheLetters.service.impl;
import com.example.catchTheLetters.constant.CommonConstant;
import com.example.catchTheLetters.entity.Level;
import com.example.catchTheLetters.service.LevelService;
import com.example.catchTheLetters.utils.R;
import com.github.yulichang.method.mp.Delete;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import com.mongodb.internal.bulk.DeleteRequest;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
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.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
/**
@ -9,5 +23,38 @@ import org.springframework.stereotype.Service;
* @description
*/
@Service
public class LevelServiceImpl {
public class LevelServiceImpl implements LevelService {
@Resource
private MongoTemplate mongoTemplate;
@Override
public boolean delete(String id) {
Level level = new Level();
level.setId(id);
DeleteResult deleteRequest = mongoTemplate.remove(level);
if (deleteRequest.getDeletedCount() > 0) {
return true;
} else {
return false;
}
}
@Override
public boolean update(Level level) {
Query query=Query.query(Criteria.where("id").is(level.getId()));
Update update = new Update();
update.set("order",level.getOrder());
update.set("name",level.getName());
update.set("type",level.getType());
update.set("timeLimit",level.getTimeLimit());
update.set("targetScore",level.getTargetScore());
update.set("words",level.getWords());
UpdateResult updateResult=mongoTemplate.updateFirst(query, update, Level.class);
if (updateResult.getModifiedCount() > 0) {
return true;
} else {
return false;
}
}
}