feat: 创建角色功能

This commit is contained in:
siujamo
2025-12-26 14:59:10 +08:00
parent 28341089e1
commit 826f51926b
5 changed files with 66 additions and 0 deletions
@@ -1,11 +1,13 @@
package com.onixbyte.helix.controller; package com.onixbyte.helix.controller;
import com.onixbyte.helix.domain.entity.Role; 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.domain.web.request.QueryRoleRequest;
import com.onixbyte.helix.service.RoleService; import com.onixbyte.helix.service.RoleService;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; 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"))); var pageRequest = PageRequest.of(pageNum - 1, pageSize, Sort.by(Sort.Order.asc("id")));
return roleService.getRoles(pageRequest, request); return roleService.getRoles(pageRequest, request);
} }
@PostMapping
public ResponseEntity<Void> addRole(@Validated @RequestBody AddRoleRequest request) {
roleService.addRole(request);
return ResponseEntity.ok(null);
}
} }
@@ -305,4 +305,17 @@ public class Role {
return new Role(id, name, code, sort, defaultValue, description, status, createdAt, updatedAt); 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();
}
} }
@@ -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
) {
}
@@ -42,4 +42,8 @@ public class RoleManager {
return new PageImpl<>(records, pageable, total); return new PageImpl<>(records, pageable, total);
} }
public Role save(Role role) {
return roleRepository.save(role);
}
} }
@@ -3,6 +3,7 @@ package com.onixbyte.helix.service;
import com.onixbyte.helix.constant.Status; import com.onixbyte.helix.constant.Status;
import com.onixbyte.helix.domain.database.query.wrapper.QueryRoleWrapper; import com.onixbyte.helix.domain.database.query.wrapper.QueryRoleWrapper;
import com.onixbyte.helix.domain.entity.Role; 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.domain.web.request.QueryRoleRequest;
import com.onixbyte.helix.manager.RoleManager; import com.onixbyte.helix.manager.RoleManager;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@@ -39,4 +40,23 @@ public class RoleService {
return roleManager.selectAll(pageable, wrapper); 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);
}
} }