Role修改以及日志模块
This commit is contained in:
parent
762d9ebe66
commit
9ffdf3d33c
@ -1,15 +1,20 @@
|
||||
package com.jsl.oa.controllers;
|
||||
|
||||
import com.jsl.oa.model.voData.RoleEditVO;
|
||||
import com.jsl.oa.services.RoleService;
|
||||
import com.jsl.oa.utils.BaseResponse;
|
||||
import com.jsl.oa.utils.ErrorCode;
|
||||
import com.jsl.oa.utils.Processing;
|
||||
import com.jsl.oa.utils.ResultUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.lang.Nullable;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/**
|
||||
* <h1>角色控制器</h1>
|
||||
@ -20,6 +25,7 @@ import javax.validation.constraints.NotNull;
|
||||
* @see RoleService
|
||||
* @since v1.1.0
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
public class RoleController {
|
||||
@ -35,9 +41,30 @@ public class RoleController {
|
||||
*/
|
||||
@GetMapping("/role/get")
|
||||
public BaseResponse roleGet(HttpServletRequest request, @RequestParam @Nullable String id) {
|
||||
log.info("请求接口[GET]: /role/get");
|
||||
return roleService.roleGet(request, id);
|
||||
}
|
||||
|
||||
/**
|
||||
* <h2>角色编辑</h2>
|
||||
* <hr/>
|
||||
* 角色编辑接口
|
||||
*
|
||||
* @param request 请求
|
||||
* @param roleEditVO 角色编辑VO
|
||||
* @param bindingResult 参数校验结果
|
||||
* @return {@link BaseResponse}
|
||||
*/
|
||||
@PutMapping("/role/edit")
|
||||
public BaseResponse roleEdit(HttpServletRequest request, @RequestBody @Validated RoleEditVO roleEditVO, @NotNull BindingResult bindingResult) {
|
||||
log.info("请求接口[PUT]: /role/edit");
|
||||
// 判断是否有参数错误
|
||||
if (bindingResult.hasErrors()) {
|
||||
return ResultUtil.error(ErrorCode.REQUEST_BODY_ERROR, Processing.getValidatedErrorList(bindingResult));
|
||||
}
|
||||
return roleService.roleEdit(request, roleEditVO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户权限授予
|
||||
*
|
||||
@ -45,6 +72,7 @@ public class RoleController {
|
||||
*/
|
||||
@PostMapping("role/user/add")
|
||||
public BaseResponse roleAddUser(HttpServletRequest request, @RequestParam Long uid, @RequestParam Long rid) {
|
||||
log.info("请求接口[POST]: /role/user/add");
|
||||
// 判断是否有参数错误
|
||||
if (uid == null || rid == null) {
|
||||
return ResultUtil.error(ErrorCode.PARAMETER_ERROR);
|
||||
@ -59,6 +87,7 @@ public class RoleController {
|
||||
*/
|
||||
@DeleteMapping("role/user/remove")
|
||||
public BaseResponse roleRemoveUser(HttpServletRequest request, @RequestParam Long uid) {
|
||||
log.info("请求接口[POST]: /role/user/remove");
|
||||
// 判断是否有参数错误
|
||||
if (uid == null) {
|
||||
return ResultUtil.error(ErrorCode.PARAMETER_ERROR);
|
||||
|
@ -21,13 +21,21 @@ public class RoleDAO {
|
||||
roleMapper.roleRemoveUser(uid);
|
||||
}
|
||||
|
||||
public List<RoleDO> getRoleById(String id) {
|
||||
public List<RoleDO> getRolesById(String id) {
|
||||
ArrayList<RoleDO> getRoleList = new ArrayList<>();
|
||||
getRoleList.add(roleMapper.getRoleById(Long.valueOf(id)));
|
||||
return getRoleList;
|
||||
}
|
||||
|
||||
public RoleDO getRoleById(Long id) {
|
||||
return roleMapper.getRoleById(id);
|
||||
}
|
||||
|
||||
public List<RoleDO> getRole() {
|
||||
return roleMapper.getRole();
|
||||
}
|
||||
|
||||
public boolean roleEdit(RoleDO getRole) {
|
||||
return roleMapper.roleEdit(getRole);
|
||||
}
|
||||
}
|
||||
|
@ -17,19 +17,19 @@ public class ProcessException {
|
||||
|
||||
@ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
|
||||
public ResponseEntity<BaseResponse> businessMethodNotAllowedException() {
|
||||
log.debug("请求方法错误");
|
||||
log.warn("请求方法错误");
|
||||
return ResultUtil.error("MethodNotAllowed", 405, "请求方法错误");
|
||||
}
|
||||
|
||||
@ExceptionHandler(value = DuplicateKeyException.class)
|
||||
public ResponseEntity<BaseResponse> businessDuplicateKeyException(@NotNull DuplicateKeyException e) {
|
||||
log.debug(e.getMessage(), e);
|
||||
log.warn(e.getMessage(), e);
|
||||
return ResultUtil.error("DuplicateEntry", 400, "数据重复/外键约束");
|
||||
}
|
||||
|
||||
@ExceptionHandler(value = HttpMessageNotReadableException.class)
|
||||
public ResponseEntity<BaseResponse> businessHttpMessageNotReadableException(HttpMessageNotReadableException e) {
|
||||
log.debug(e.getMessage(), e);
|
||||
log.warn(e.getMessage(), e);
|
||||
return ResultUtil.error("HttpMessageNotReadable", 400, "请求参数错误");
|
||||
}
|
||||
|
||||
|
@ -2,10 +2,7 @@ package com.jsl.oa.mapper;
|
||||
|
||||
import com.jsl.oa.model.doData.RoleDO;
|
||||
import com.jsl.oa.model.doData.RoleUserDO;
|
||||
import org.apache.ibatis.annotations.Delete;
|
||||
import org.apache.ibatis.annotations.Insert;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
import org.apache.ibatis.annotations.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -29,4 +26,7 @@ public interface RoleMapper {
|
||||
|
||||
@Select("SELECT * FROM organize_oa.oa_role ORDER BY id DESC")
|
||||
List<RoleDO> getRole();
|
||||
|
||||
@Update("UPDATE organize_oa.oa_role SET role_name=#{roleName},display_name=#{displayName} WHERE id=#{id}")
|
||||
boolean roleEdit(RoleDO getRole);
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ import java.sql.Timestamp;
|
||||
public class RoleDO {
|
||||
private Long id;
|
||||
private String roleName;
|
||||
private String displayName;
|
||||
private Timestamp createdAt;
|
||||
private Timestamp updatedAt;
|
||||
}
|
||||
|
25
src/main/java/com/jsl/oa/model/voData/RoleEditVO.java
Normal file
25
src/main/java/com/jsl/oa/model/voData/RoleEditVO.java
Normal file
@ -0,0 +1,25 @@
|
||||
package com.jsl.oa.model.voData;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.Pattern;
|
||||
|
||||
/**
|
||||
* <h1>角色编辑VO</h1>
|
||||
* <hr/>
|
||||
* 角色编辑VO,用于接收角色编辑请求
|
||||
*
|
||||
* @version v1.1.0
|
||||
* @author 筱锋xiao_lfeng
|
||||
* @since v1.1.0
|
||||
*/
|
||||
@Data
|
||||
public class RoleEditVO {
|
||||
private Long id;
|
||||
@NotBlank
|
||||
@Pattern(regexp = "^[a-zA-Z0-9_]{3,20}$", message = "角色名只能为3-16位的字母、数字、下划线组成")
|
||||
private String name;
|
||||
@NotBlank
|
||||
private String displayName;
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package com.jsl.oa.services;
|
||||
|
||||
import com.jsl.oa.model.voData.RoleEditVO;
|
||||
import com.jsl.oa.utils.BaseResponse;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
@ -18,4 +19,6 @@ public interface RoleService {
|
||||
BaseResponse roleRemoveUser(HttpServletRequest request,Long uid);
|
||||
|
||||
BaseResponse roleGet(HttpServletRequest request, String id);
|
||||
|
||||
BaseResponse roleEdit(HttpServletRequest request, RoleEditVO roleEditVO);
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
package com.jsl.oa.services.impl;
|
||||
|
||||
import com.jsl.oa.dao.RoleDAO;
|
||||
import com.jsl.oa.dao.UserDAO;
|
||||
import com.jsl.oa.model.doData.RoleDO;
|
||||
import com.jsl.oa.model.voData.RoleEditVO;
|
||||
import com.jsl.oa.services.RoleService;
|
||||
import com.jsl.oa.utils.BaseResponse;
|
||||
import com.jsl.oa.utils.ErrorCode;
|
||||
@ -47,7 +47,7 @@ public class RoleServiceImpl implements RoleService {
|
||||
ArrayList<RoleDO> getRoleList;
|
||||
if (id != null && !id.isEmpty()) {
|
||||
if (Pattern.matches("^[0-9]+$", id)) {
|
||||
getRoleList = (ArrayList<RoleDO>) roleDAO.getRoleById(id);
|
||||
getRoleList = (ArrayList<RoleDO>) roleDAO.getRolesById(id);
|
||||
} else {
|
||||
ArrayList<String> error = new ArrayList<>();
|
||||
error.add("id 只能为数字");
|
||||
@ -60,4 +60,28 @@ public class RoleServiceImpl implements RoleService {
|
||||
// 返回数据
|
||||
return ResultUtil.success(getRoleList);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseResponse roleEdit(HttpServletRequest request, RoleEditVO roleEditVO) {
|
||||
// 检查用户权限
|
||||
if (!Processing.checkUserIsAdmin(request, roleDAO.roleMapper)) {
|
||||
return ResultUtil.error(ErrorCode.NOT_ADMIN);
|
||||
}
|
||||
// 获取 Role 相关信息
|
||||
RoleDO getRole = roleDAO.getRoleById(roleEditVO.getId());
|
||||
// 判断是否存在该 Role
|
||||
if (getRole != null) {
|
||||
// 替换 Role 信息
|
||||
getRole.setRoleName(roleEditVO.getName())
|
||||
.setDisplayName(roleEditVO.getDisplayName());
|
||||
// 更新 Role 信息
|
||||
if (roleDAO.roleEdit(getRole)) {
|
||||
return ResultUtil.success();
|
||||
} else {
|
||||
return ResultUtil.error(ErrorCode.DATABASE_UPDATE_ERROR);
|
||||
}
|
||||
} else {
|
||||
return ResultUtil.error(ErrorCode.ROLE_NOT_FOUNDED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ public enum ErrorCode {
|
||||
NOT_ADMIN("NotAdmin", 40300, "不是管理员"),
|
||||
EMAIL_LOGIN_NOT_SUPPORT("EmailLoginNotSupport", 40300, "请使用邮箱登陆"),
|
||||
PASSWORD_NOT_SAME("PasswordNotSame", 40301, "两次密码不一致"),
|
||||
ROLE_NOT_FOUNDED("RoleNotFounded", 40400, "角色不存在"),
|
||||
DATABASE_INSERT_ERROR("DatabaseInsertError", 50010, "数据库插入错误"),
|
||||
DATABASE_UPDATE_ERROR("DatabaseUpdateError", 50011, "数据库更新错误"),
|
||||
DATABASE_DELETE_ERROR("DatabaseDeleteError", 50012, "数据库删除错误"),
|
||||
|
@ -10,48 +10,48 @@ public class ResultUtil {
|
||||
|
||||
@Contract(" -> new")
|
||||
public static @NotNull BaseResponse success() {
|
||||
log.debug("请求接口成功");
|
||||
log.info("请求接口成功[200] 不含数据");
|
||||
return new BaseResponse("Success", 200, "操作成功", null);
|
||||
}
|
||||
|
||||
@Contract("_ -> new")
|
||||
public static @NotNull BaseResponse success(String message) {
|
||||
log.debug("请求接口成功");
|
||||
log.info(message + "[200]");
|
||||
return new BaseResponse("Success", 200, message, null);
|
||||
}
|
||||
|
||||
@Contract(value = "_ -> new", pure = true)
|
||||
public static @NotNull BaseResponse success(Object data) {
|
||||
log.debug("请求接口成功");
|
||||
log.info("请求接口成功[200] 带数据");
|
||||
return new BaseResponse("Success", 200, "操作成功", data);
|
||||
}
|
||||
|
||||
@Contract(value = "_, _ -> new", pure = true)
|
||||
public static @NotNull BaseResponse success(String message, Object data) {
|
||||
log.debug("请求接口成功");
|
||||
log.info(message + "[200] 带数据");
|
||||
return new BaseResponse("Success", 200, message, data);
|
||||
}
|
||||
|
||||
@Contract("_ -> new")
|
||||
public static @NotNull BaseResponse error(@NotNull ErrorCode errorCode) {
|
||||
log.debug("请求接口错误[" + errorCode.getCode() + "] " + errorCode.getMessage());
|
||||
log.info("请求接口错误[" + errorCode.getCode() + "] " + errorCode.getMessage());
|
||||
return new BaseResponse(errorCode.getOutput(), errorCode.getCode(), errorCode.getMessage());
|
||||
}
|
||||
|
||||
@Contract("_, _ -> new")
|
||||
public static @NotNull BaseResponse error(@NotNull ErrorCode errorCode, Object data) {
|
||||
log.debug("请求接口错误[" + errorCode.getCode() + "] " + errorCode.getMessage());
|
||||
log.info("请求接口错误[" + errorCode.getCode() + "] " + errorCode.getMessage());
|
||||
return new BaseResponse(errorCode.getOutput(), errorCode.getCode(), errorCode.getMessage(), data);
|
||||
}
|
||||
|
||||
@Contract(value = "_, _, _, _ -> new", pure = true)
|
||||
public static @NotNull BaseResponse error(String output, Integer code, String message, Object data) {
|
||||
log.debug("请求接口错误[" + code + "] " + message);
|
||||
log.info("请求接口错误[" + code + "] " + message);
|
||||
return new BaseResponse(output, code, message, data);
|
||||
}
|
||||
|
||||
public static @NotNull ResponseEntity<BaseResponse> error(String output, Integer code, String message) {
|
||||
log.debug("请求接口错误[" + code + "] " + message);
|
||||
log.info("请求接口错误[" + code + "] " + message);
|
||||
return ResponseEntity.status(code)
|
||||
.body(new BaseResponse(output, code, message));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user