TA 역할 Spring 경계 smoke #6
1 changed files with 79 additions and 0 deletions
|
|
@ -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<RoleMatrixResponse> 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<String> permissionIds) {
|
||||
log.info("Syncing permissions for role matrix: {}", roleMatrixId);
|
||||
RoleMatrix matrix = roleMatrixRepository.findById(roleMatrixId)
|
||||
.orElseThrow(() -> new RoleMatrixNotFoundException(roleMatrixId));
|
||||
matrix.syncPermissions(permissionIds);
|
||||
roleMatrixRepository.save(matrix);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue