From 826f51926bf6c5cb76368f080dd288b8aed35c98 Mon Sep 17 00:00:00 2001 From: siujamo Date: Fri, 26 Dec 2025 14:59:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=88=9B=E5=BB=BA=E8=A7=92=E8=89=B2?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../helix/controller/RoleController.java | 8 +++++++ .../onixbyte/helix/domain/entity/Role.java | 13 ++++++++++++ .../domain/web/request/AddRoleRequest.java | 21 +++++++++++++++++++ .../onixbyte/helix/manager/RoleManager.java | 4 ++++ .../onixbyte/helix/service/RoleService.java | 20 ++++++++++++++++++ 5 files changed, 66 insertions(+) create mode 100644 src/main/java/com/onixbyte/helix/domain/web/request/AddRoleRequest.java diff --git a/src/main/java/com/onixbyte/helix/controller/RoleController.java b/src/main/java/com/onixbyte/helix/controller/RoleController.java index 8f25535..7d16f6a 100644 --- a/src/main/java/com/onixbyte/helix/controller/RoleController.java +++ b/src/main/java/com/onixbyte/helix/controller/RoleController.java @@ -1,11 +1,13 @@ package com.onixbyte.helix.controller; import com.onixbyte.helix.domain.entity.Role; +import com.onixbyte.helix.domain.web.request.AddRoleRequest; import com.onixbyte.helix.domain.web.request.QueryRoleRequest; import com.onixbyte.helix.service.RoleService; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; +import org.springframework.http.ResponseEntity; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; @@ -28,4 +30,10 @@ public class RoleController { var pageRequest = PageRequest.of(pageNum - 1, pageSize, Sort.by(Sort.Order.asc("id"))); return roleService.getRoles(pageRequest, request); } + + @PostMapping + public ResponseEntity addRole(@Validated @RequestBody AddRoleRequest request) { + roleService.addRole(request); + return ResponseEntity.ok(null); + } } diff --git a/src/main/java/com/onixbyte/helix/domain/entity/Role.java b/src/main/java/com/onixbyte/helix/domain/entity/Role.java index d000fba..5633d13 100644 --- a/src/main/java/com/onixbyte/helix/domain/entity/Role.java +++ b/src/main/java/com/onixbyte/helix/domain/entity/Role.java @@ -305,4 +305,17 @@ public class Role { return new Role(id, name, code, sort, defaultValue, description, status, createdAt, updatedAt); } } + + @PrePersist + private void onInsert() { + var currentTime = LocalDateTime.now(); + this.createdAt = currentTime; + this.updatedAt = currentTime; + } + + @PreUpdate + private void onUpdate() { + this.updatedAt = LocalDateTime.now(); + } + } diff --git a/src/main/java/com/onixbyte/helix/domain/web/request/AddRoleRequest.java b/src/main/java/com/onixbyte/helix/domain/web/request/AddRoleRequest.java new file mode 100644 index 0000000..1127ba2 --- /dev/null +++ b/src/main/java/com/onixbyte/helix/domain/web/request/AddRoleRequest.java @@ -0,0 +1,21 @@ +package com.onixbyte.helix.domain.web.request; + +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Pattern; + +public record AddRoleRequest( + @NotBlank(message = "角色名称不能为空") + String name, + @NotBlank(message = "角色编码不能为空") + String code, + @NotNull(message = "排序编号不能为空") + Integer sort, + Boolean defaultValue, + String description, + @Pattern( + regexp = "^(ACTIVE|INACTIVE)?$", + message = "状态仅可以是 ACTIVE、INACTIVE 其中之一") + String status +) { +} diff --git a/src/main/java/com/onixbyte/helix/manager/RoleManager.java b/src/main/java/com/onixbyte/helix/manager/RoleManager.java index 3bc32e1..0046f9f 100644 --- a/src/main/java/com/onixbyte/helix/manager/RoleManager.java +++ b/src/main/java/com/onixbyte/helix/manager/RoleManager.java @@ -42,4 +42,8 @@ public class RoleManager { return new PageImpl<>(records, pageable, total); } + + public Role save(Role role) { + return roleRepository.save(role); + } } diff --git a/src/main/java/com/onixbyte/helix/service/RoleService.java b/src/main/java/com/onixbyte/helix/service/RoleService.java index 15a18c6..3cef3b8 100644 --- a/src/main/java/com/onixbyte/helix/service/RoleService.java +++ b/src/main/java/com/onixbyte/helix/service/RoleService.java @@ -3,6 +3,7 @@ package com.onixbyte.helix.service; import com.onixbyte.helix.constant.Status; import com.onixbyte.helix.domain.database.query.wrapper.QueryRoleWrapper; import com.onixbyte.helix.domain.entity.Role; +import com.onixbyte.helix.domain.web.request.AddRoleRequest; import com.onixbyte.helix.domain.web.request.QueryRoleRequest; import com.onixbyte.helix.manager.RoleManager; import org.apache.commons.lang3.StringUtils; @@ -39,4 +40,23 @@ public class RoleService { return roleManager.selectAll(pageable, wrapper); } + + public Role addRole(AddRoleRequest request) { + var isDefaultRole = Optional.ofNullable(request.defaultValue()) + .orElse(false); + var status = Optional.ofNullable(request.status()) + .map(Status::valueOf) + .orElse(Status.ACTIVE); + + var role = Role.builder() + .name(request.name()) + .code(request.code()) + .sort(request.sort()) + .defaultValue(isDefaultRole) + .description(request.description()) + .status(status) + .build(); + + return roleManager.save(role); + } }