mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-09-08 12:57:13 +08:00
refactor: 使用 SpEL Validator 优化部分校验场景
This commit is contained in:
@@ -21,7 +21,6 @@ import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import top.continew.admin.common.base.controller.BaseController;
|
||||
import top.continew.admin.system.enums.NoticeMethodEnum;
|
||||
import top.continew.admin.system.enums.NoticeScopeEnum;
|
||||
import top.continew.admin.system.model.query.NoticeQuery;
|
||||
import top.continew.admin.system.model.req.NoticeReq;
|
||||
import top.continew.admin.system.model.resp.notice.NoticeDetailResp;
|
||||
@@ -55,10 +54,6 @@ public class NoticeController extends BaseController<NoticeService, NoticeResp,
|
||||
return;
|
||||
}
|
||||
NoticeReq req = (NoticeReq)args[0];
|
||||
// 校验通知范围
|
||||
if (NoticeScopeEnum.USER.equals(req.getNoticeScope())) {
|
||||
ValidationUtils.throwIfEmpty(req.getNoticeUsers(), "通知用户不能为空");
|
||||
}
|
||||
// 校验通知方式
|
||||
List<Integer> noticeMethods = req.getNoticeMethods();
|
||||
if (CollUtil.isNotEmpty(noticeMethods)) {
|
||||
|
@@ -16,7 +16,11 @@
|
||||
|
||||
package top.continew.admin.system.model.req;
|
||||
|
||||
import cn.sticki.spel.validator.constrain.SpelNotEmpty;
|
||||
import cn.sticki.spel.validator.constrain.SpelNotNull;
|
||||
import cn.sticki.spel.validator.jakarta.SpelValid;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.Future;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
@@ -36,6 +40,7 @@ import java.util.List;
|
||||
* @since 2023/8/20 10:55
|
||||
*/
|
||||
@Data
|
||||
@SpelValid
|
||||
@Schema(description = "公告创建或修改请求参数")
|
||||
public class NoticeReq implements Serializable {
|
||||
|
||||
@@ -76,6 +81,7 @@ public class NoticeReq implements Serializable {
|
||||
* 通知用户
|
||||
*/
|
||||
@Schema(description = "通知用户", example = "[1,2,3]")
|
||||
@SpelNotEmpty(condition = "#this.noticeScope == T(top.continew.admin.system.enums.NoticeScopeEnum).USER", message = "通知用户不能为空")
|
||||
private List<String> noticeUsers;
|
||||
|
||||
/**
|
||||
@@ -87,13 +93,16 @@ public class NoticeReq implements Serializable {
|
||||
/**
|
||||
* 是否定时
|
||||
*/
|
||||
@Schema(description = "是否定时", example = "false")
|
||||
@Schema(description = "是否定时", example = "true")
|
||||
@NotNull(message = "是否定时不能为空")
|
||||
private Boolean isTiming;
|
||||
|
||||
/**
|
||||
* 发布时间
|
||||
*/
|
||||
@Schema(description = "发布时间", example = "2023-08-08 00:00:00", type = "string")
|
||||
@SpelNotNull(condition = "#this.isTiming == true", message = "定时发布时间不能为空")
|
||||
@Future(message = "定时发布时间不能早于当前时间")
|
||||
private LocalDateTime publishTime;
|
||||
|
||||
/**
|
||||
|
@@ -36,7 +36,6 @@ import top.continew.admin.system.service.MessageService;
|
||||
import top.continew.admin.system.service.NoticeLogService;
|
||||
import top.continew.admin.system.service.NoticeService;
|
||||
import top.continew.starter.core.util.validation.CheckUtils;
|
||||
import top.continew.starter.core.util.validation.ValidationUtils;
|
||||
import top.continew.starter.extension.crud.model.query.PageQuery;
|
||||
import top.continew.starter.extension.crud.model.resp.PageResp;
|
||||
|
||||
@@ -67,11 +66,6 @@ public class NoticeServiceImpl extends BaseServiceImpl<NoticeMapper, NoticeDO, N
|
||||
|
||||
@Override
|
||||
public void beforeCreate(NoticeReq req) {
|
||||
// 校验定时发布
|
||||
if (Boolean.TRUE.equals(req.getIsTiming())) {
|
||||
ValidationUtils.throwIf(req.getPublishTime() == null, "定时发布时间不能为空");
|
||||
ValidationUtils.throwIf(req.getPublishTime().isBefore(LocalDateTime.now()), "定时发布时间不能早于当前时间");
|
||||
}
|
||||
if (!NoticeStatusEnum.DRAFT.equals(req.getStatus())) {
|
||||
if (Boolean.TRUE.equals(req.getIsTiming())) {
|
||||
// 待发布
|
||||
@@ -113,11 +107,6 @@ public class NoticeServiceImpl extends BaseServiceImpl<NoticeMapper, NoticeDO, N
|
||||
req.setPublishTime(oldNotice.getPublishTime());
|
||||
}
|
||||
case DRAFT, PENDING -> {
|
||||
// 校验定时发布
|
||||
if (Boolean.TRUE.equals(req.getIsTiming())) {
|
||||
ValidationUtils.throwIf(req.getPublishTime() == null, "定时发布时间不能为空");
|
||||
ValidationUtils.throwIf(req.getPublishTime().isBefore(LocalDateTime.now()), "定时发布时间不能早于当前时间");
|
||||
}
|
||||
// 已发布
|
||||
if (NoticeStatusEnum.PUBLISHED.equals(req.getStatus())) {
|
||||
if (Boolean.TRUE.equals(req.getIsTiming())) {
|
||||
|
Reference in New Issue
Block a user