55import com .back .domain .file .entity .FileAttachment ;
66import com .back .domain .file .repository .AttachmentMappingRepository ;
77import com .back .domain .file .repository .FileAttachmentRepository ;
8+ import com .back .domain .file .service .FileService ;
89import com .back .global .exception .CustomException ;
910import com .back .global .exception .ErrorCode ;
1011import lombok .RequiredArgsConstructor ;
@@ -25,6 +26,7 @@ public class RoomThumbnailService {
2526
2627 private final FileAttachmentRepository fileAttachmentRepository ;
2728 private final AttachmentMappingRepository attachmentMappingRepository ;
29+ private final FileService fileService ;
2830
2931 /**
3032 * 방 생성 시 썸네일 매핑 생성
@@ -60,42 +62,83 @@ public String createThumbnailMapping(Long roomId, Long thumbnailAttachmentId) {
6062
6163 /**
6264 * 방 수정 시 썸네일 변경
63- * 1. 기존 매핑 삭제
65+ * 1. 기존 매핑 및 파일 삭제 (S3 + FileAttachment + Mapping)
6466 * 2. 새 매핑 생성
6567 *
6668 * @param roomId 방 ID
6769 * @param newThumbnailAttachmentId 새 썸네일 파일 ID (null이면 변경 없음)
70+ * @param userId 요청자 ID (파일 삭제 권한 검증용)
6871 * @return 새 썸네일 URL (null이면 변경 없음)
6972 */
7073 @ Transactional
71- public String updateThumbnailMapping (Long roomId , Long newThumbnailAttachmentId ) {
74+ public String updateThumbnailMapping (Long roomId , Long newThumbnailAttachmentId , Long userId ) {
7275 if (newThumbnailAttachmentId == null ) {
7376 // null이면 썸네일 변경 없음
7477 return null ;
7578 }
7679
77- // 기존 매핑 모두 삭제
80+ // 기존 매핑 및 파일 삭제 (S3 + FileAttachment 포함)
81+ List <AttachmentMapping > mappings = attachmentMappingRepository
82+ .findAllByEntityTypeAndEntityId (EntityType .STUDY_ROOM , roomId );
83+
84+ for (AttachmentMapping mapping : mappings ) {
85+ FileAttachment oldAttachment = mapping .getFileAttachment ();
86+ if (oldAttachment != null ) {
87+ // FileService를 사용하여 S3 파일 + FileAttachment 삭제
88+ try {
89+ fileService .deleteFile (oldAttachment .getId (), userId );
90+ log .info ("기존 썸네일 파일 삭제 - RoomId: {}, AttachmentId: {}" ,
91+ roomId , oldAttachment .getId ());
92+ } catch (Exception e ) {
93+ log .error ("썸네일 파일 삭제 실패 - RoomId: {}, AttachmentId: {}, Error: {}" ,
94+ roomId , oldAttachment .getId (), e .getMessage ());
95+ }
96+ }
97+ }
98+
99+ // 매핑 삭제
78100 attachmentMappingRepository .deleteAllByEntityTypeAndEntityId (
79101 EntityType .STUDY_ROOM , roomId );
80102
81- log .info ("기존 썸네일 매핑 삭제 - RoomId: {}" , roomId );
103+ log .info ("기존 썸네일 매핑 및 파일 삭제 완료 - RoomId: {}" , roomId );
82104
83105 // 새 매핑 생성
84106 return createThumbnailMapping (roomId , newThumbnailAttachmentId );
85107 }
86108
87109 /**
88- * 방 삭제 시 썸네일 매핑 삭제
110+ * 방 삭제 시 썸네일 매핑 및 파일 삭제
111+ * S3 파일 + FileAttachment + AttachmentMapping 모두 삭제
89112 *
90113 * @param roomId 방 ID
114+ * @param userId 요청자 ID (파일 삭제 권한 검증용)
91115 */
92116 @ Transactional
93- public void deleteThumbnailMapping (Long roomId ) {
94- // 연결된 파일 매핑 모두 삭제
117+ public void deleteThumbnailMapping (Long roomId , Long userId ) {
118+ // 매핑 조회
119+ List <AttachmentMapping > mappings = attachmentMappingRepository
120+ .findAllByEntityTypeAndEntityId (EntityType .STUDY_ROOM , roomId );
121+
122+ // S3 파일 + FileAttachment 삭제
123+ for (AttachmentMapping mapping : mappings ) {
124+ FileAttachment fileAttachment = mapping .getFileAttachment ();
125+ if (fileAttachment != null ) {
126+ try {
127+ fileService .deleteFile (fileAttachment .getId (), userId );
128+ log .info ("썸네일 파일 삭제 완료 - RoomId: {}, AttachmentId: {}" ,
129+ roomId , fileAttachment .getId ());
130+ } catch (Exception e ) {
131+ log .error ("썸네일 파일 삭제 실패 - RoomId: {}, AttachmentId: {}, Error: {}" ,
132+ roomId , fileAttachment .getId (), e .getMessage ());
133+ }
134+ }
135+ }
136+
137+ // AttachmentMapping 삭제
95138 attachmentMappingRepository .deleteAllByEntityTypeAndEntityId (
96139 EntityType .STUDY_ROOM , roomId );
97140
98- log .info ("방 삭제 - 썸네일 매핑 삭제 완료 - RoomId: {}" , roomId );
141+ log .info ("방 삭제 - 썸네일 매핑 및 파일 삭제 완료 - RoomId: {}" , roomId );
99142 }
100143
101144 /**
0 commit comments