feat: refactor authority request handling and update methods to use AuthorityRequest
This commit is contained in:
@@ -1,8 +1,7 @@
|
|||||||
package com.onixbyte.helix.controller;
|
package com.onixbyte.helix.controller;
|
||||||
|
|
||||||
import com.onixbyte.helix.domain.entity.Authority;
|
import com.onixbyte.helix.domain.entity.Authority;
|
||||||
import com.onixbyte.helix.domain.web.request.AddAuthorityRequest;
|
import com.onixbyte.helix.domain.web.request.AuthorityRequest;
|
||||||
import com.onixbyte.helix.domain.web.request.EditAuthorityRequest;
|
|
||||||
import com.onixbyte.helix.domain.web.request.QueryAuthorityRequest;
|
import com.onixbyte.helix.domain.web.request.QueryAuthorityRequest;
|
||||||
import com.onixbyte.helix.domain.web.response.ActionResponse;
|
import com.onixbyte.helix.domain.web.response.ActionResponse;
|
||||||
import com.onixbyte.helix.service.AuthorityService;
|
import com.onixbyte.helix.service.AuthorityService;
|
||||||
@@ -52,7 +51,7 @@ public class AuthorityController {
|
|||||||
* @return created authority
|
* @return created authority
|
||||||
*/
|
*/
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public Authority addAuthority(@Validated @RequestBody AddAuthorityRequest request) {
|
public Authority addAuthority(@Validated @RequestBody AuthorityRequest request) {
|
||||||
return authorityService.addAuthority(request);
|
return authorityService.addAuthority(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,9 +61,12 @@ public class AuthorityController {
|
|||||||
* @param request authority specs
|
* @param request authority specs
|
||||||
* @return edited authority
|
* @return edited authority
|
||||||
*/
|
*/
|
||||||
@PutMapping
|
@PutMapping("/{id:\\d+}")
|
||||||
public Authority editAuthority(@Validated @RequestBody EditAuthorityRequest request) {
|
public Authority editAuthority(
|
||||||
return authorityService.editAuthority(request);
|
@PathVariable Long id,
|
||||||
|
@Validated @RequestBody AuthorityRequest request
|
||||||
|
) {
|
||||||
|
return authorityService.editAuthority(id, request);
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/{authorityId:\\d+}")
|
@DeleteMapping("/{authorityId:\\d+}")
|
||||||
|
|||||||
@@ -1,15 +0,0 @@
|
|||||||
package com.onixbyte.helix.domain.web.request;
|
|
||||||
|
|
||||||
import jakarta.validation.constraints.NotBlank;
|
|
||||||
import jakarta.validation.constraints.NotNull;
|
|
||||||
import jakarta.validation.constraints.Pattern;
|
|
||||||
|
|
||||||
public record AddAuthorityRequest(
|
|
||||||
@NotNull @NotBlank(message = "权限编码不能为空") String code,
|
|
||||||
@NotNull @NotBlank(message = "权限名称不能为空") String name,
|
|
||||||
String description,
|
|
||||||
@Pattern(
|
|
||||||
regexp = "^(ACTIVE|INACTIVE)?$",
|
|
||||||
message = "状态错误") String status
|
|
||||||
) {
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package com.onixbyte.helix.domain.web.request;
|
||||||
|
|
||||||
|
import com.onixbyte.helix.enumeration.Status;
|
||||||
|
import com.onixbyte.helix.validation.group.OnCreate;
|
||||||
|
import com.onixbyte.helix.validation.group.OnUpdate;
|
||||||
|
import jakarta.validation.constraints.NotBlank;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
import jakarta.validation.constraints.Null;
|
||||||
|
|
||||||
|
public record AuthorityRequest(
|
||||||
|
@Null(groups = {OnUpdate.class}, message = "Code cannot be edited.")
|
||||||
|
@NotNull(groups = {OnCreate.class}, message = "Code cannot be null.")
|
||||||
|
String code,
|
||||||
|
@NotNull(message = "Name of the authority cannot be null")
|
||||||
|
@NotBlank(message = "Name of the authority cannot be null")
|
||||||
|
String name,
|
||||||
|
String description,
|
||||||
|
Status status
|
||||||
|
) {
|
||||||
|
}
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
package com.onixbyte.helix.domain.web.request;
|
|
||||||
|
|
||||||
import jakarta.validation.constraints.Pattern;
|
|
||||||
|
|
||||||
public record EditAuthorityRequest(
|
|
||||||
Long id,
|
|
||||||
String name,
|
|
||||||
String description,
|
|
||||||
@Pattern(
|
|
||||||
regexp = "^(ACTIVE|INACTIVE)?$",
|
|
||||||
message = "状态参数错误")
|
|
||||||
String status
|
|
||||||
) {
|
|
||||||
}
|
|
||||||
@@ -2,7 +2,7 @@ package com.onixbyte.helix.manager;
|
|||||||
|
|
||||||
import com.onixbyte.helix.domain.database.query.wrapper.QueryAuthorityWrapper;
|
import com.onixbyte.helix.domain.database.query.wrapper.QueryAuthorityWrapper;
|
||||||
import com.onixbyte.helix.domain.entity.Authority;
|
import com.onixbyte.helix.domain.entity.Authority;
|
||||||
import com.onixbyte.helix.domain.web.request.EditAuthorityRequest;
|
import com.onixbyte.helix.domain.web.request.AuthorityRequest;
|
||||||
import com.onixbyte.helix.enumeration.Status;
|
import com.onixbyte.helix.enumeration.Status;
|
||||||
import com.onixbyte.helix.exception.BizException;
|
import com.onixbyte.helix.exception.BizException;
|
||||||
import com.onixbyte.helix.mapper.AuthorityMapper;
|
import com.onixbyte.helix.mapper.AuthorityMapper;
|
||||||
@@ -17,6 +17,7 @@ import org.springframework.http.HttpStatus;
|
|||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@@ -53,19 +54,29 @@ public class AuthorityManager {
|
|||||||
return authorityRepository.save(authority);
|
return authorityRepository.save(authority);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fully updates an existing authority by ID.
|
||||||
|
* <p>
|
||||||
|
* The method loads the target authority, replaces mutable fields ({@code name},
|
||||||
|
* {@code description}, {@code status}), and refreshes {@code updatedAt} to the current time.
|
||||||
|
* The update runs within a transactional context.
|
||||||
|
*
|
||||||
|
* @param id the ID of the authority to update
|
||||||
|
* @param authority the source data carrying new field values
|
||||||
|
* @return the supplied {@link Authority} object
|
||||||
|
* @throws BizException if the target authority does not exist
|
||||||
|
*/
|
||||||
@Transactional
|
@Transactional
|
||||||
public Authority update(EditAuthorityRequest request) {
|
public Authority fullUpdateById(Long id, Authority authority) {
|
||||||
var authority = authorityRepository.findById(request.id())
|
var updatedAt = LocalDateTime.now();
|
||||||
|
|
||||||
|
var authorityToUpdate = authorityRepository.findById(id)
|
||||||
.orElseThrow(() -> new BizException(HttpStatus.NOT_FOUND, "找不到指定的权限信息"));
|
.orElseThrow(() -> new BizException(HttpStatus.NOT_FOUND, "找不到指定的权限信息"));
|
||||||
|
|
||||||
Optional.ofNullable(request.name())
|
authorityToUpdate.setName(authority.getName());
|
||||||
.ifPresent(authority::setName);
|
authorityToUpdate.setDescription(authority.getDescription());
|
||||||
|
authorityToUpdate.setStatus(authority.getStatus());
|
||||||
authority.setDescription(request.description());
|
authorityToUpdate.setUpdatedAt(updatedAt);
|
||||||
|
|
||||||
Optional.ofNullable(request.status())
|
|
||||||
.map(Status::valueOf)
|
|
||||||
.ifPresent(authority::setStatus);
|
|
||||||
|
|
||||||
return authority;
|
return authority;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,10 +2,8 @@ package com.onixbyte.helix.service;
|
|||||||
|
|
||||||
import com.onixbyte.helix.domain.database.query.wrapper.QueryAuthorityWrapper;
|
import com.onixbyte.helix.domain.database.query.wrapper.QueryAuthorityWrapper;
|
||||||
import com.onixbyte.helix.domain.entity.Authority;
|
import com.onixbyte.helix.domain.entity.Authority;
|
||||||
import com.onixbyte.helix.domain.web.request.AddAuthorityRequest;
|
import com.onixbyte.helix.domain.web.request.AuthorityRequest;
|
||||||
import com.onixbyte.helix.domain.web.request.EditAuthorityRequest;
|
|
||||||
import com.onixbyte.helix.domain.web.request.QueryAuthorityRequest;
|
import com.onixbyte.helix.domain.web.request.QueryAuthorityRequest;
|
||||||
import com.onixbyte.helix.enumeration.Status;
|
|
||||||
import com.onixbyte.helix.exception.BizException;
|
import com.onixbyte.helix.exception.BizException;
|
||||||
import com.onixbyte.helix.manager.AuthorityManager;
|
import com.onixbyte.helix.manager.AuthorityManager;
|
||||||
import com.onixbyte.helix.manager.RoleAuthorityManager;
|
import com.onixbyte.helix.manager.RoleAuthorityManager;
|
||||||
@@ -16,8 +14,6 @@ import org.springframework.http.HttpStatus;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class AuthorityService {
|
public class AuthorityService {
|
||||||
|
|
||||||
@@ -35,14 +31,12 @@ public class AuthorityService {
|
|||||||
return authorityManager.selectAll(pageable, wrapper);
|
return authorityManager.selectAll(pageable, wrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Authority addAuthority(AddAuthorityRequest request) {
|
public Authority addAuthority(AuthorityRequest request) {
|
||||||
var authority = Authority.builder()
|
var authority = Authority.builder()
|
||||||
.code(request.code())
|
.code(request.code())
|
||||||
.name(request.name())
|
.name(request.name())
|
||||||
.description(request.description())
|
.description(request.description())
|
||||||
.status(Optional.ofNullable(request.status())
|
.status(request.status())
|
||||||
.map(Status::valueOf)
|
|
||||||
.orElse(Status.ACTIVE))
|
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
if (authorityManager.existsByCode(authority)) {
|
if (authorityManager.existsByCode(authority)) {
|
||||||
@@ -52,8 +46,12 @@ public class AuthorityService {
|
|||||||
return authorityManager.save(authority);
|
return authorityManager.save(authority);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Authority editAuthority(EditAuthorityRequest request) {
|
public Authority editAuthority(Long id, AuthorityRequest request) {
|
||||||
return authorityManager.update(request);
|
return authorityManager.fullUpdateById(id, Authority.builder()
|
||||||
|
.name(request.name())
|
||||||
|
.description(request.description())
|
||||||
|
.status(request.status())
|
||||||
|
.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional(rollbackFor = Throwable.class)
|
@Transactional(rollbackFor = Throwable.class)
|
||||||
|
|||||||
Reference in New Issue
Block a user