feat: rename AddDepartmentRequest to DepartmentRequest and implement department editing functionality
This commit is contained in:
@@ -2,7 +2,7 @@ package com.onixbyte.helix.controller;
|
|||||||
|
|
||||||
import com.onixbyte.helix.domain.entity.Department;
|
import com.onixbyte.helix.domain.entity.Department;
|
||||||
import com.onixbyte.helix.domain.common.TreeNode;
|
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 com.onixbyte.helix.service.DepartmentService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
@@ -38,7 +38,15 @@ public class DepartmentController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public Department addDepartment(@Validated @RequestBody AddDepartmentRequest request) {
|
public Department addDepartment(@Validated @RequestBody DepartmentRequest request) {
|
||||||
return departmentService.addDepartment(request);
|
return departmentService.addDepartment(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{id:\\d+}")
|
||||||
|
public Department editDepartment(
|
||||||
|
@PathVariable Long id,
|
||||||
|
@Validated @RequestBody DepartmentRequest request
|
||||||
|
) {
|
||||||
|
return departmentService.editDepartment(id, request);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -26,7 +26,7 @@ import com.onixbyte.helix.enumeration.Status;
|
|||||||
import jakarta.validation.constraints.NotBlank;
|
import jakarta.validation.constraints.NotBlank;
|
||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
|
||||||
public record AddDepartmentRequest(
|
public record DepartmentRequest(
|
||||||
@NotNull(message = "Name of the department should not be null")
|
@NotNull(message = "Name of the department should not be null")
|
||||||
@NotBlank(message = "Name of the department should not be null")
|
@NotBlank(message = "Name of the department should not be null")
|
||||||
String name,
|
String name,
|
||||||
@@ -1,12 +1,16 @@
|
|||||||
package com.onixbyte.helix.manager;
|
package com.onixbyte.helix.manager;
|
||||||
|
|
||||||
import com.onixbyte.helix.domain.entity.Department;
|
import com.onixbyte.helix.domain.entity.Department;
|
||||||
|
import com.onixbyte.helix.exception.BizException;
|
||||||
import com.onixbyte.helix.repository.DepartmentRepository;
|
import com.onixbyte.helix.repository.DepartmentRepository;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@@ -34,4 +38,34 @@ public class DepartmentManager {
|
|||||||
public Department save(Department department) {
|
public Department save(Department department) {
|
||||||
return departmentRepository.save(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.common.TreeNode;
|
||||||
import com.onixbyte.helix.domain.entity.Department;
|
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.enumeration.Status;
|
||||||
import com.onixbyte.helix.manager.DepartmentManager;
|
import com.onixbyte.helix.manager.DepartmentManager;
|
||||||
import com.onixbyte.helix.utils.TreeUtil;
|
import com.onixbyte.helix.utils.TreeUtil;
|
||||||
@@ -35,7 +35,7 @@ public class DepartmentService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional(rollbackFor = Throwable.class)
|
@Transactional(rollbackFor = Throwable.class)
|
||||||
public Department addDepartment(AddDepartmentRequest request) {
|
public Department addDepartment(DepartmentRequest request) {
|
||||||
var createdAt = LocalDateTime.now();
|
var createdAt = LocalDateTime.now();
|
||||||
|
|
||||||
var parentId = request.parentId();
|
var parentId = request.parentId();
|
||||||
@@ -51,4 +51,9 @@ public class DepartmentService {
|
|||||||
.updatedAt(createdAt)
|
.updatedAt(createdAt)
|
||||||
.build());
|
.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Department editDepartment(Long id, DepartmentRequest request) {
|
||||||
|
return departmentManager.fullUpdateById(id, Department.builder()
|
||||||
|
.build());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user