feat: rename AddDepartmentRequest to DepartmentRequest and implement department editing functionality

This commit is contained in:
siujamo
2026-03-24 10:07:37 +08:00
parent 712a675325
commit e1ad5cdfd8
4 changed files with 52 additions and 5 deletions
@@ -2,7 +2,7 @@ package com.onixbyte.helix.controller;
import com.onixbyte.helix.domain.entity.Department;
import com.onixbyte.helix.domain.common.TreeNode;
import com.onixbyte.helix.domain.web.request.AddDepartmentRequest;
import com.onixbyte.helix.domain.web.request.DepartmentRequest;
import com.onixbyte.helix.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
@@ -38,7 +38,15 @@ public class DepartmentController {
}
@PostMapping
public Department addDepartment(@Validated @RequestBody AddDepartmentRequest request) {
public Department addDepartment(@Validated @RequestBody DepartmentRequest request) {
return departmentService.addDepartment(request);
}
@PutMapping("/{id:\\d+}")
public Department editDepartment(
@PathVariable Long id,
@Validated @RequestBody DepartmentRequest request
) {
return departmentService.editDepartment(id, request);
}
}
@@ -26,7 +26,7 @@ import com.onixbyte.helix.enumeration.Status;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
public record AddDepartmentRequest(
public record DepartmentRequest(
@NotNull(message = "Name of the department should not be null")
@NotBlank(message = "Name of the department should not be null")
String name,
@@ -1,12 +1,16 @@
package com.onixbyte.helix.manager;
import com.onixbyte.helix.domain.entity.Department;
import com.onixbyte.helix.exception.BizException;
import com.onixbyte.helix.repository.DepartmentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.Optional;
@Component
@@ -34,4 +38,34 @@ public class DepartmentManager {
public Department save(Department department) {
return departmentRepository.save(department);
}
/**
* Fully updates an existing department by ID.
* <p>
* The method loads the target department, replaces mutable fields ({@code name},
* {@code parentId}, {@code sort}, {@code status}), and refreshes {@code updatedAt} to the
* current time.
*
* @param id the ID of the department to update
* @param department the source data carrying new field values
* @return the managed and updated {@link Department} entity
* @throws BizException if the target department does not exist
*/
@Transactional
public Department fullUpdateById(Long id, Department department) {
var updatedAt = LocalDateTime.now();
var departmentToEdit = departmentRepository.findById(id)
.orElseThrow(() -> new BizException(
HttpStatus.NOT_FOUND,
"Department (ID: %d) to be edited not found.".formatted(department.getId()))
);
departmentToEdit.setName(department.getName());
departmentToEdit.setParentId(department.getParentId());
departmentToEdit.setSort(department.getSort());
departmentToEdit.setStatus(department.getStatus());
departmentToEdit.setUpdatedAt(updatedAt);
return departmentToEdit;
}
}
@@ -2,7 +2,7 @@ package com.onixbyte.helix.service;
import com.onixbyte.helix.domain.common.TreeNode;
import com.onixbyte.helix.domain.entity.Department;
import com.onixbyte.helix.domain.web.request.AddDepartmentRequest;
import com.onixbyte.helix.domain.web.request.DepartmentRequest;
import com.onixbyte.helix.enumeration.Status;
import com.onixbyte.helix.manager.DepartmentManager;
import com.onixbyte.helix.utils.TreeUtil;
@@ -35,7 +35,7 @@ public class DepartmentService {
}
@Transactional(rollbackFor = Throwable.class)
public Department addDepartment(AddDepartmentRequest request) {
public Department addDepartment(DepartmentRequest request) {
var createdAt = LocalDateTime.now();
var parentId = request.parentId();
@@ -51,4 +51,9 @@ public class DepartmentService {
.updatedAt(createdAt)
.build());
}
public Department editDepartment(Long id, DepartmentRequest request) {
return departmentManager.fullUpdateById(id, Department.builder()
.build());
}
}