diff --git a/src/main/java/com/bio/bio_backend/domain/base/file/service/FileServiceImpl.java b/src/main/java/com/bio/bio_backend/domain/base/file/service/FileServiceImpl.java index 99b1af7..e2413fb 100644 --- a/src/main/java/com/bio/bio_backend/domain/base/file/service/FileServiceImpl.java +++ b/src/main/java/com/bio/bio_backend/domain/base/file/service/FileServiceImpl.java @@ -144,7 +144,6 @@ public class FileServiceImpl implements FileService { // DB에 파일 정보 저장 File file = createFileEntity(originalFileName, storedFileName, targetLocation, multipartFile, description, groupOid); - file.setCreator(SecurityUtils.getCurrentUserOid(), SecurityUtils.getCurrentUserId()); return fileRepository.save(file); @@ -192,17 +191,13 @@ public class FileServiceImpl implements FileService { public void deleteFile(Long oid) { File file = fileRepository.findByOidAndUseFlagTrue(oid) .orElseThrow(() -> new ApiException(ApiResponseCode.FILE_NOT_FOUND)); - - Long currentUserOid = SecurityUtils.getCurrentUserOid(); + String currentUserId = SecurityUtils.getCurrentUserId(); // 현재 사용자가 파일 소유자인지 확인 if (currentUserId == null || !currentUserId.equals(file.getCreatedId())) { throw new ApiException(ApiResponseCode.COMMON_FORBIDDEN); } - // 수정자 정보 업데이트 - file.setUpdater(currentUserOid, currentUserId); - // 논리적 삭제: use_flag를 false로 변경 file.setUseFlag(false); fileRepository.save(file); diff --git a/src/main/java/com/bio/bio_backend/domain/base/member/service/MemberServiceImpl.java b/src/main/java/com/bio/bio_backend/domain/base/member/service/MemberServiceImpl.java index 56d4e0e..55d1176 100644 --- a/src/main/java/com/bio/bio_backend/domain/base/member/service/MemberServiceImpl.java +++ b/src/main/java/com/bio/bio_backend/domain/base/member/service/MemberServiceImpl.java @@ -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.constants.ApiResponseCode; import com.bio.bio_backend.global.constants.AppConstants; + import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.security.core.userdetails.UserDetails; @@ -19,8 +20,6 @@ import org.springframework.transaction.annotation.Transactional; import java.util.List; import java.util.Map; -import static com.bio.bio_backend.global.utils.OidUtils.generateOid; - @Service @RequiredArgsConstructor @Slf4j @@ -53,10 +52,11 @@ public class MemberServiceImpl implements MemberService { .email(memberDto.getEmail()) .role(MemberRole.getDefault()) .build(); - - Long oid = generateOid(); - member.setOid(oid); - member.setCreator(AppConstants.ADMIN_OID, AppConstants.ADMIN_USER_ID); + + member.setCreatedOid(AppConstants.ADMIN_OID); + member.setCreatedId(AppConstants.ADMIN_USER_ID); + member.setUpdatedOid(AppConstants.ADMIN_OID); + member.setUpdatedId(AppConstants.ADMIN_USER_ID); Member savedMember = memberRepository.save(member); diff --git a/src/main/java/com/bio/bio_backend/global/dto/ApiResponseDto.java b/src/main/java/com/bio/bio_backend/global/dto/ApiResponseDto.java index d3538ee..105f20c 100644 --- a/src/main/java/com/bio/bio_backend/global/dto/ApiResponseDto.java +++ b/src/main/java/com/bio/bio_backend/global/dto/ApiResponseDto.java @@ -17,8 +17,6 @@ public class ApiResponseDto { private String description; private T data; - private static final int SUCCESS = 200; - private ApiResponseDto(int code, String message, String description, T data){ this.code = code; this.message = message; @@ -27,11 +25,11 @@ public class ApiResponseDto { } public static ApiResponseDto success(ApiResponseCode responseCode, T data) { - return new ApiResponseDto(SUCCESS, responseCode.name(), responseCode.getDescription(), data); + return new ApiResponseDto(responseCode.getStatusCode(), responseCode.name(), responseCode.getDescription(), data); } public static ApiResponseDto success(ApiResponseCode responseCode) { - return new ApiResponseDto(SUCCESS, responseCode.name(), responseCode.getDescription(), null); + return new ApiResponseDto(responseCode.getStatusCode(), responseCode.name(), responseCode.getDescription(), null); } public static ApiResponseDto fail(ApiResponseCode responseCode, T data) { diff --git a/src/main/java/com/bio/bio_backend/global/entity/BaseEntity.java b/src/main/java/com/bio/bio_backend/global/entity/BaseEntity.java index 402de9b..a4ae824 100644 --- a/src/main/java/com/bio/bio_backend/global/entity/BaseEntity.java +++ b/src/main/java/com/bio/bio_backend/global/entity/BaseEntity.java @@ -9,7 +9,7 @@ import org.springframework.data.jpa.domain.support.AuditingEntityListener; 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 @Setter @MappedSuperclass -@EntityListeners(AuditingEntityListener.class) +@EntityListeners({AuditingEntityListener.class, BaseEntityListener.class}) public abstract class BaseEntity { @Id @@ -45,31 +45,4 @@ public abstract class BaseEntity { @Column(name = "updated_id") 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; - } } diff --git a/src/main/java/com/bio/bio_backend/global/entity/BaseEntityListener.java b/src/main/java/com/bio/bio_backend/global/entity/BaseEntityListener.java new file mode 100644 index 0000000..91c7010 --- /dev/null +++ b/src/main/java/com/bio/bio_backend/global/entity/BaseEntityListener.java @@ -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); + } + } +}