1717import com .back .domain .file .entity .FileAttachment ;
1818import com .back .domain .file .repository .AttachmentMappingRepository ;
1919import com .back .domain .file .repository .FileAttachmentRepository ;
20+ import com .back .domain .file .service .FileService ;
2021import com .back .domain .user .common .entity .User ;
2122import com .back .domain .user .common .repository .UserRepository ;
2223import com .back .global .exception .CustomException ;
@@ -40,6 +41,7 @@ public class PostService {
4041 private final PostCategoryRepository postCategoryRepository ;
4142 private final FileAttachmentRepository fileAttachmentRepository ;
4243 private final AttachmentMappingRepository attachmentMappingRepository ;
44+ private final FileService fileService ;
4345
4446 /**
4547 * 게시글 생성 서비스
@@ -183,7 +185,15 @@ public void deletePost(Long postId, Long userId) {
183185 throw new CustomException (ErrorCode .POST_NO_PERMISSION );
184186 }
185187
186- // AttachmentMapping 매핑 제거
188+ // 첨부 파일 삭제
189+ List <AttachmentMapping > mappings =
190+ attachmentMappingRepository .findAllByEntityTypeAndEntityId (EntityType .POST , post .getId ());
191+ for (AttachmentMapping mapping : mappings ) {
192+ FileAttachment fileAttachment = mapping .getFileAttachment ();
193+ if (fileAttachment != null ) {
194+ fileService .deleteFile (fileAttachment .getId (), userId );
195+ }
196+ }
187197 attachmentMappingRepository .deleteAllByEntityTypeAndEntityId (EntityType .POST , post .getId ());
188198
189199 // Post 삭제
0 commit comments