Browse Source

feat: 用例相关接口+登录改进成redis和ThreadLocal

haihang
whh 4 months ago
parent
commit
23745b746b
  1. 44
      document/src/main/java/com/haihang/controller/CaseController.java
  2. 24
      document/src/main/java/com/haihang/entity/Case.java
  3. 33
      document/src/main/java/com/haihang/mapper/CaseMapper.java
  4. 16
      document/src/main/java/com/haihang/service/CaseService.java
  5. 98
      document/src/main/java/com/haihang/service/impl/CaseServiceImpl.java
  6. 7
      document/src/main/java/com/haihang/utils/LoginInterceptor.java
  7. 20
      document/src/main/java/com/haihang/utils/UserHolder.java

44
document/src/main/java/com/haihang/controller/CaseController.java

@ -0,0 +1,44 @@
package com.haihang.controller;
import com.haihang.entity.Case;
import com.haihang.service.CaseService;
import com.haihang.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/case")
public class CaseController {
//用例相关接口
@Autowired
private CaseService caseService;
@PostMapping("/add")
public Result add(@RequestBody Case _case){
return caseService.add(_case);
}
@DeleteMapping("/{id}")
public Result delete(@PathVariable int id){
//根据id删除用例,删除一个用例他的子用例会一并全部删除
return caseService.deleteWithSonById(id);
}
@PutMapping("update")
public Result update(@RequestBody Case _case){
//修改用例,只能修改用例名字和用例描述
return caseService.update(_case);
}
@GetMapping("/{id}")
public Result get(@PathVariable int id){
//根据id查询用例信息接口
return caseService.getById(id);
}
}

24
document/src/main/java/com/haihang/entity/Case.java

@ -0,0 +1,24 @@
package com.haihang.entity;
import lombok.Data;
import org.apache.ibatis.annotations.Insert;
import java.util.Date;
//用例信息,与数据库完全相同
@Data
public class Case {
private int id;
private String caseName;
private int parentId;
private int isLeaf;
private int createUser;
private int updateUser;
private Date createTime;
private Date updateTime;
private String caseDesc;//用例描述
}

33
document/src/main/java/com/haihang/mapper/CaseMapper.java

@ -0,0 +1,33 @@
package com.haihang.mapper;
import com.haihang.entity.Case;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface CaseMapper {
@Select("select * from case_table where id = #{parentId}")
Case selectById(int parentId);
@Insert("INSERT INTO `case_table`\n" +
"(id, case_name, parent_id, is_leaf, create_user, update_user, create_time, update_time, case_desc)\n" +
"VALUES(null, #{caseName}, #{parentId}, 1, #{createUser}, #{updateUser}, #{createTime}, #{updateTime}, #{caseDesc});")
void insert(Case aCase);
@Update("update case_table set is_leaf = #{isLeaf},case_name = #{caseName},parent_id = #{parentId}" +
", update_user = #{updateUser},update_time = null ,case_desc=#{caseDesc} " +
"where id = #{id}")
void update(Case parentCase);
@Delete("delete from case_table where id = #{id}")
void deleteById(int id);
@Select("select id from case_table where parent_id =#{id}")
List<Integer> selectByParentId(int id);
@Update("update case_table set is_leaf = #{i} where id =#{parentId}")
void updateIsLeaf(int i, int parentId);
}

16
document/src/main/java/com/haihang/service/CaseService.java

@ -0,0 +1,16 @@
package com.haihang.service;
import com.haihang.entity.Case;
import com.haihang.utils.Result;
public interface CaseService {
Result add(Case aCase);
Result deleteWithSonById(int id);
Result update(Case aCase);
Result getById(int id);
}

98
document/src/main/java/com/haihang/service/impl/CaseServiceImpl.java

@ -0,0 +1,98 @@
package com.haihang.service.impl;
import com.haihang.entity.Case;
import com.haihang.mapper.CaseMapper;
import com.haihang.service.CaseService;
import com.haihang.utils.Result;
import com.haihang.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
@Service
public class CaseServiceImpl implements CaseService {
@Autowired
private CaseMapper caseMapper;
//新增用例
@Override
public Result add(Case aCase) {
aCase.setCreateTime(new Date());
aCase.setUpdateTime(new Date());
//判断传进来的用例父节点id,查询他是不是叶子节点,如果不是叶子节点则不能新增用例
int parentId = aCase.getParentId();
if(parentId == 0){
//如果父节点为空,说明还没建立树,可以直接插入
caseMapper.insert(aCase);
return Result.ok();
}
Case parentCase = caseMapper.selectById(parentId);
int isLeaf = parentCase.getIsLeaf();
if(isLeaf!=1){
return Result.fail("该用例不能添加!原因:父节点并非叶子节点");
}
aCase.setCreateUser(UserHolder.getUser().getId());
aCase.setUpdateUser(UserHolder.getUser().getId());
caseMapper.insert(aCase);
//插入完成要把父亲的isleaf修改掉.
parentCase.setIsLeaf(0);
caseMapper.update(parentCase);
return Result.ok();
}
@Override
public Result deleteWithSonById(int id) {
//先根据id查询
Case aCase = caseMapper.selectById(id);
if(aCase==null){
return Result.ok();
}
//根据当前id查询父节点为id的id列表,找他的儿子们
List<Integer> ids= caseMapper.selectByParentId(id);
//删他的儿子们
for(Integer sonId:ids){
deleteWithSonById(sonId);
}
//在删除自己之前顺便记录parentId
int parentId = aCase.getParentId();
//有值则删除自己
caseMapper.deleteById(id);
//根据他爹id判断他爹isleaf
List<Integer> brotherId= caseMapper.selectByParentId(parentId);
if(brotherId == null || brotherId.isEmpty()){
caseMapper.updateIsLeaf(1,parentId);
}
return Result.ok();
}
@Override
public Result update(Case aCase) {
Case dbCase = caseMapper.selectById(aCase.getId());
dbCase.setCaseName(aCase.getCaseName());
dbCase.setCaseDesc(aCase.getCaseDesc());
dbCase.setUpdateUser(UserHolder.getUser().getId());
dbCase.setUpdateTime(new Date());
caseMapper.update(dbCase);
return Result.ok();
}
@Override
public Result getById(int id) {
Case aCase = caseMapper.selectById(id);
return Result.ok(aCase);
}
}

7
document/src/main/java/com/haihang/utils/LoginInterceptor.java

@ -5,6 +5,7 @@ package com.haihang.utils;
import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.haihang.entity.UserInfo;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.HandlerInterceptor;
@ -53,6 +54,12 @@ public class LoginInterceptor implements HandlerInterceptor {
// //hash转UserDTO存入ThreadLocal // //hash转UserDTO存入ThreadLocal
// UserHolder.saveUser(BeanUtil.fillBeanWithMap(userMap, new UserDTO(), false)); // UserHolder.saveUser(BeanUtil.fillBeanWithMap(userMap, new UserDTO(), false));
//走到这里说明前端传过来的token里面有值且是登录用户,放行就完事了,暂时不用ThreadLocal //走到这里说明前端传过来的token里面有值且是登录用户,放行就完事了,暂时不用ThreadLocal
//将用户id存放到ThreadLocal
UserHolder.saveUser(BeanUtil.fillBeanWithMap(userMap, new UserInfo(), false));
return true; return true;
} }

20
document/src/main/java/com/haihang/utils/UserHolder.java

@ -0,0 +1,20 @@
package com.haihang.utils;
import com.haihang.entity.UserDTO;
import com.haihang.entity.UserInfo;
public class UserHolder {
private static final ThreadLocal<UserInfo> tl = new ThreadLocal<>();
public static void saveUser(UserInfo user){
tl.set(user);
}
public static UserInfo getUser(){
return tl.get();
}
public static void removeUser(){
tl.remove();
}
}
Loading…
Cancel
Save