[엔티티 리스너 추가] BaseEntityListener 클래스를 추가하여 BaseEntity의 생성자 및 수정자 정보를 자동으로 설정하도록 개선. FileServiceImpl 및 MemberServiceImpl에서 수동으로 설정하던 부분 제거.
This commit is contained in:
		@@ -144,7 +144,6 @@ public class FileServiceImpl implements FileService {
 | 
				
			|||||||
            
 | 
					            
 | 
				
			||||||
            // DB에 파일 정보 저장
 | 
					            // DB에 파일 정보 저장
 | 
				
			||||||
            File file = createFileEntity(originalFileName, storedFileName, targetLocation, multipartFile, description, groupOid);
 | 
					            File file = createFileEntity(originalFileName, storedFileName, targetLocation, multipartFile, description, groupOid);
 | 
				
			||||||
            file.setCreator(SecurityUtils.getCurrentUserOid(), SecurityUtils.getCurrentUserId());
 | 
					 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
            return fileRepository.save(file);
 | 
					            return fileRepository.save(file);
 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
@@ -193,16 +192,12 @@ public class FileServiceImpl implements FileService {
 | 
				
			|||||||
        File file = fileRepository.findByOidAndUseFlagTrue(oid)
 | 
					        File file = fileRepository.findByOidAndUseFlagTrue(oid)
 | 
				
			||||||
                .orElseThrow(() -> new ApiException(ApiResponseCode.FILE_NOT_FOUND));
 | 
					                .orElseThrow(() -> new ApiException(ApiResponseCode.FILE_NOT_FOUND));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        Long currentUserOid = SecurityUtils.getCurrentUserOid();
 | 
					 | 
				
			||||||
        String currentUserId = SecurityUtils.getCurrentUserId();
 | 
					        String currentUserId = SecurityUtils.getCurrentUserId();
 | 
				
			||||||
        // 현재 사용자가 파일 소유자인지 확인
 | 
					        // 현재 사용자가 파일 소유자인지 확인
 | 
				
			||||||
        if (currentUserId == null || !currentUserId.equals(file.getCreatedId())) {
 | 
					        if (currentUserId == null || !currentUserId.equals(file.getCreatedId())) {
 | 
				
			||||||
            throw new ApiException(ApiResponseCode.COMMON_FORBIDDEN);
 | 
					            throw new ApiException(ApiResponseCode.COMMON_FORBIDDEN);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
        // 수정자 정보 업데이트
 | 
					 | 
				
			||||||
        file.setUpdater(currentUserOid, currentUserId);
 | 
					 | 
				
			||||||
        
 | 
					 | 
				
			||||||
        // 논리적 삭제: use_flag를 false로 변경
 | 
					        // 논리적 삭제: use_flag를 false로 변경
 | 
				
			||||||
        file.setUseFlag(false);
 | 
					        file.setUseFlag(false);
 | 
				
			||||||
        fileRepository.save(file);
 | 
					        fileRepository.save(file);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -8,6 +8,7 @@ import com.bio.bio_backend.domain.base.member.repository.MemberRepository;
 | 
				
			|||||||
import com.bio.bio_backend.global.exception.ApiException;
 | 
					import com.bio.bio_backend.global.exception.ApiException;
 | 
				
			||||||
import com.bio.bio_backend.global.constants.ApiResponseCode;
 | 
					import com.bio.bio_backend.global.constants.ApiResponseCode;
 | 
				
			||||||
import com.bio.bio_backend.global.constants.AppConstants;
 | 
					import com.bio.bio_backend.global.constants.AppConstants;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import lombok.RequiredArgsConstructor;
 | 
					import lombok.RequiredArgsConstructor;
 | 
				
			||||||
import lombok.extern.slf4j.Slf4j;
 | 
					import lombok.extern.slf4j.Slf4j;
 | 
				
			||||||
import org.springframework.security.core.userdetails.UserDetails;
 | 
					import org.springframework.security.core.userdetails.UserDetails;
 | 
				
			||||||
@@ -19,8 +20,6 @@ import org.springframework.transaction.annotation.Transactional;
 | 
				
			|||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
import java.util.Map;
 | 
					import java.util.Map;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import static com.bio.bio_backend.global.utils.OidUtils.generateOid;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
@Service
 | 
					@Service
 | 
				
			||||||
@RequiredArgsConstructor
 | 
					@RequiredArgsConstructor
 | 
				
			||||||
@Slf4j
 | 
					@Slf4j
 | 
				
			||||||
@@ -54,9 +53,10 @@ public class MemberServiceImpl implements MemberService {
 | 
				
			|||||||
                .role(MemberRole.getDefault())
 | 
					                .role(MemberRole.getDefault())
 | 
				
			||||||
                .build();
 | 
					                .build();
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
        Long oid = generateOid();
 | 
					        member.setCreatedOid(AppConstants.ADMIN_OID);
 | 
				
			||||||
        member.setOid(oid);
 | 
					        member.setCreatedId(AppConstants.ADMIN_USER_ID);
 | 
				
			||||||
        member.setCreator(AppConstants.ADMIN_OID, AppConstants.ADMIN_USER_ID);
 | 
					        member.setUpdatedOid(AppConstants.ADMIN_OID);
 | 
				
			||||||
 | 
					        member.setUpdatedId(AppConstants.ADMIN_USER_ID);
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
        Member savedMember = memberRepository.save(member);
 | 
					        Member savedMember = memberRepository.save(member);
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -17,8 +17,6 @@ public class ApiResponseDto<T> {
 | 
				
			|||||||
    private String description;
 | 
					    private String description;
 | 
				
			||||||
    private T data;
 | 
					    private T data;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private static final int SUCCESS = 200;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private ApiResponseDto(int code, String message, String description, T data){
 | 
					    private ApiResponseDto(int code, String message, String description, T data){
 | 
				
			||||||
        this.code = code;
 | 
					        this.code = code;
 | 
				
			||||||
        this.message = message;
 | 
					        this.message = message;
 | 
				
			||||||
@@ -27,11 +25,11 @@ public class ApiResponseDto<T> {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public static <T> ApiResponseDto<T> success(ApiResponseCode responseCode, T data) {
 | 
					    public static <T> ApiResponseDto<T> success(ApiResponseCode responseCode, T data) {
 | 
				
			||||||
        return new ApiResponseDto<T>(SUCCESS, responseCode.name(), responseCode.getDescription(), data);
 | 
					        return new ApiResponseDto<T>(responseCode.getStatusCode(), responseCode.name(), responseCode.getDescription(), data);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public static <T> ApiResponseDto<T> success(ApiResponseCode responseCode) {
 | 
					    public static <T> ApiResponseDto<T> success(ApiResponseCode responseCode) {
 | 
				
			||||||
        return new ApiResponseDto<T>(SUCCESS, responseCode.name(), responseCode.getDescription(), null);
 | 
					        return new ApiResponseDto<T>(responseCode.getStatusCode(), responseCode.name(), responseCode.getDescription(), null);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public static <T> ApiResponseDto<T> fail(ApiResponseCode responseCode, T data) {
 | 
					    public static <T> ApiResponseDto<T> fail(ApiResponseCode responseCode, T data) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -9,7 +9,7 @@ import org.springframework.data.jpa.domain.support.AuditingEntityListener;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import java.time.LocalDateTime;
 | 
					import java.time.LocalDateTime;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import static com.bio.bio_backend.global.utils.OidUtils.generateOid;
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * 모든 엔티티가 상속받는 기본 엔티티 클래스
 | 
					 * 모든 엔티티가 상속받는 기본 엔티티 클래스
 | 
				
			||||||
@@ -18,7 +18,7 @@ import static com.bio.bio_backend.global.utils.OidUtils.generateOid;
 | 
				
			|||||||
@Getter
 | 
					@Getter
 | 
				
			||||||
@Setter
 | 
					@Setter
 | 
				
			||||||
@MappedSuperclass
 | 
					@MappedSuperclass
 | 
				
			||||||
@EntityListeners(AuditingEntityListener.class)
 | 
					@EntityListeners({AuditingEntityListener.class, BaseEntityListener.class})
 | 
				
			||||||
public abstract class BaseEntity {
 | 
					public abstract class BaseEntity {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Id
 | 
					    @Id
 | 
				
			||||||
@@ -45,31 +45,4 @@ public abstract class BaseEntity {
 | 
				
			|||||||
    @Column(name = "updated_id")
 | 
					    @Column(name = "updated_id")
 | 
				
			||||||
    private String updatedId;
 | 
					    private String updatedId;
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    @PrePersist
 | 
					 | 
				
			||||||
    protected void onCreate() {
 | 
					 | 
				
			||||||
        if(this.oid == null) this.oid = generateOid();
 | 
					 | 
				
			||||||
        if(this.createdOid != null && this.updatedOid == null) this.updatedOid = this.createdOid;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    
 | 
					 | 
				
			||||||
    /**
 | 
					 | 
				
			||||||
     * 생성자 정보를 설정합니다.
 | 
					 | 
				
			||||||
     * @param createdOid 생성자 OID
 | 
					 | 
				
			||||||
     * @param createdId 생성자 ID
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    public void setCreator(Long createdOid, String createdId) {
 | 
					 | 
				
			||||||
        this.createdOid = createdOid;
 | 
					 | 
				
			||||||
        this.createdId = createdId;
 | 
					 | 
				
			||||||
        this.updatedOid = createdOid;
 | 
					 | 
				
			||||||
        this.updatedId = createdId;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    
 | 
					 | 
				
			||||||
    /**
 | 
					 | 
				
			||||||
     * 수정자 정보를 설정합니다.
 | 
					 | 
				
			||||||
     * @param updatedOid 수정자 OID
 | 
					 | 
				
			||||||
     * @param updatedId 수정자 ID
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    public void setUpdater(Long updatedOid, String updatedId) {
 | 
					 | 
				
			||||||
        this.updatedOid = updatedOid;
 | 
					 | 
				
			||||||
        this.updatedId = updatedId;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -0,0 +1,61 @@
 | 
				
			|||||||
 | 
					package com.bio.bio_backend.global.entity;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.bio.bio_backend.global.utils.SecurityUtils;
 | 
				
			||||||
 | 
					import jakarta.persistence.PrePersist;
 | 
				
			||||||
 | 
					import jakarta.persistence.PreUpdate;
 | 
				
			||||||
 | 
					import lombok.extern.slf4j.Slf4j;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import static com.bio.bio_backend.global.utils.OidUtils.generateOid;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * BaseEntity의 createdOid와 updatedOid 필드를 자동으로 설정하는 엔티티 리스너
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					@Slf4j
 | 
				
			||||||
 | 
					public class BaseEntityListener {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @PrePersist
 | 
				
			||||||
 | 
					    public void prePersist(BaseEntity entity) {
 | 
				
			||||||
 | 
					        if (entity.getOid() == null) {
 | 
				
			||||||
 | 
					            entity.setOid(generateOid());
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
 | 
					            String currentUserId = SecurityUtils.getCurrentUserId();
 | 
				
			||||||
 | 
					            Long currentUserOid = SecurityUtils.getCurrentUserOid();
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					            if (currentUserOid != null) {
 | 
				
			||||||
 | 
					                entity.setCreatedOid(currentUserOid);
 | 
				
			||||||
 | 
					                entity.setUpdatedOid(currentUserOid);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					            if (currentUserId != null) {
 | 
				
			||||||
 | 
					                entity.setCreatedId(currentUserId);
 | 
				
			||||||
 | 
					                entity.setUpdatedId(currentUserId);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        } catch (SecurityException | IllegalStateException e) {
 | 
				
			||||||
 | 
					            log.warn("등록자 정보 설정 실패: {}", e.getMessage());
 | 
				
			||||||
 | 
					        } catch (Exception e) {
 | 
				
			||||||
 | 
					            log.error("등록자 정보 설정 오류: {}", e.getMessage(), e);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @PreUpdate
 | 
				
			||||||
 | 
					    public void preUpdate(BaseEntity entity) {
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
 | 
					            String currentUserId = SecurityUtils.getCurrentUserId();
 | 
				
			||||||
 | 
					            Long currentUserOid = SecurityUtils.getCurrentUserOid();
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					            if (currentUserOid != null) {
 | 
				
			||||||
 | 
					                entity.setUpdatedOid(currentUserOid);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					            if (currentUserId != null) {
 | 
				
			||||||
 | 
					                entity.setUpdatedId(currentUserId);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        } catch (SecurityException | IllegalStateException e) {
 | 
				
			||||||
 | 
					            log.warn("수정자 정보 설정 실패: {}", e.getMessage());
 | 
				
			||||||
 | 
					        } catch (Exception e) {
 | 
				
			||||||
 | 
					            log.error("수정자 정보 설정 오류: {}", e.getMessage(), e);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user