Skip to content

Commit 282ef52

Browse files
committed
Feat: 게시글 삭제 시 첨부 파일 삭제
1 parent 3bff555 commit 282ef52

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/main/java/com/back/domain/board/post/service/PostService.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.back.domain.file.entity.FileAttachment;
1818
import com.back.domain.file.repository.AttachmentMappingRepository;
1919
import com.back.domain.file.repository.FileAttachmentRepository;
20+
import com.back.domain.file.service.FileService;
2021
import com.back.domain.user.common.entity.User;
2122
import com.back.domain.user.common.repository.UserRepository;
2223
import 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

Comments
 (0)