From d5ebb8f74db700de09fcde8a41f4d8d1e0e396e4 Mon Sep 17 00:00:00 2001 From: forge-bot Date: Tue, 14 Jul 2026 06:31:57 +0000 Subject: [PATCH] =?UTF-8?q?TA=20=EC=97=AD=ED=95=A0=20Spring=20=EA=B2=BD?= =?UTF-8?q?=EA=B3=84=20smoke=20(role-ta-live-1522-001)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rolematrix/service/RoleMatrixService.java | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/main/java/com/klaroworks/runtime/rolematrix/service/RoleMatrixService.java diff --git a/src/main/java/com/klaroworks/runtime/rolematrix/service/RoleMatrixService.java b/src/main/java/com/klaroworks/runtime/rolematrix/service/RoleMatrixService.java new file mode 100644 index 0000000..0853957 --- /dev/null +++ b/src/main/java/com/klaroworks/runtime/rolematrix/service/RoleMatrixService.java @@ -0,0 +1,79 @@ +package com.klaroworks.runtime.rolematrix.service; + +import com.klaroworks.runtime.rolematrix.domain.RoleMatrix; +import com.klaroworks.runtime.rolematrix.dto.CreateRoleMatrixCommand; +import com.klaroworks.runtime.rolematrix.dto.RoleMatrixResponse; +import com.klaroworks.runtime.rolematrix.dto.UpdateRoleMatrixCommand; +import com.klaroworks.runtime.rolematrix.exception.DomainException; +import com.klaroworks.runtime.rolematrix.repository.RoleMatrixRepository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +/** 역할 매트릭스 서비스 - 비즈니스 로직 및 트랜잭션 경계 관리 */ +@Service +@Transactional(readOnly = true) +public class RoleMatrixService { + + private static final Logger log = LoggerFactory.getLogger(RoleMatrixService.class); + private final RoleMatrixRepository roleMatrixRepository; + + public RoleMatrixService(RoleMatrixRepository roleMatrixRepository) { + this.roleMatrixRepository = roleMatrixRepository; + } + + public List findAll() { + log.debug("Finding all role matrices"); + return roleMatrixRepository.findAll().stream().map(RoleMatrixResponse::from).toList(); + } + + public RoleMatrixResponse findById(Long id) { + log.debug("Finding role matrix by id: {}", id); + return roleMatrixRepository.findById(id) + .map(RoleMatrixResponse::from) + .orElseThrow(() -> new RoleMatrixNotFoundException(id)); + } + + @Transactional + public RoleMatrixResponse create(CreateRoleMatrixCommand command) { + log.info("Creating role matrix: {}", command.name()); + if (roleMatrixRepository.existsByName(command.name())) { + throw new DuplicateRoleMatrixException(command.name()); + } + RoleMatrix saved = roleMatrixRepository.save(RoleMatrix.create(command.name(), command.description())); + log.info("Role matrix created with id: {}", saved.getId()); + return RoleMatrixResponse.from(saved); + } + + @Transactional + public RoleMatrixResponse update(Long id, UpdateRoleMatrixCommand command) { + log.info("Updating role matrix: {}", id); + RoleMatrix matrix = roleMatrixRepository.findById(id) + .orElseThrow(() -> new RoleMatrixNotFoundException(id)); + if (command.name() != null && !command.name().equals(matrix.getName()) && roleMatrixRepository.existsByName(command.name())) { + throw new DuplicateRoleMatrixException(command.name()); + } + matrix.update(command.name(), command.description()); + return RoleMatrixResponse.from(roleMatrixRepository.save(matrix)); + } + + @Transactional + public void delete(Long id) { + log.info("Deleting role matrix: {}", id); + if (!roleMatrixRepository.existsById(id)) throw new RoleMatrixNotFoundException(id); + roleMatrixRepository.deleteById(id); + } + + @Transactional(propagation = Propagation.REQUIRES_NEW) + public void syncPermissions(Long roleMatrixId, List permissionIds) { + log.info("Syncing permissions for role matrix: {}", roleMatrixId); + RoleMatrix matrix = roleMatrixRepository.findById(roleMatrixId) + .orElseThrow(() -> new RoleMatrixNotFoundException(roleMatrixId)); + matrix.syncPermissions(permissionIds); + roleMatrixRepository.save(matrix); + } +}