|
24 | 24 | import com.back.global.file.dto.UploadedFileDto; |
25 | 25 | import com.back.global.file.service.FileService; |
26 | 26 | import com.back.global.rq.Rq; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.NoSuchElementException; |
| 30 | +import java.util.Optional; |
| 31 | +import java.util.function.Function; |
| 32 | +import java.util.stream.Collectors; |
27 | 33 | import lombok.RequiredArgsConstructor; |
28 | 34 | import org.springframework.stereotype.Service; |
29 | 35 | import org.springframework.transaction.annotation.Transactional; |
30 | 36 | import org.springframework.transaction.support.TransactionSynchronization; |
31 | 37 | import org.springframework.transaction.support.TransactionSynchronizationManager; |
32 | 38 | import org.springframework.web.multipart.MultipartFile; |
33 | 39 |
|
34 | | -import java.util.*; |
35 | | -import java.util.function.Function; |
36 | | -import java.util.stream.Collectors; |
37 | | - |
38 | 40 | @Service |
39 | 41 | @RequiredArgsConstructor |
40 | 42 | public class PostService { |
@@ -158,39 +160,35 @@ public PostResponseDto updatePost(Long postId, PostUpdateRequestDto reqBody, Lis |
158 | 160 | Map<Long, PostImage> existingById = post.getImages().stream() |
159 | 161 | .collect(Collectors.toMap(PostImage::getId, Function.identity())); |
160 | 162 |
|
161 | | - // 삭제할 이미지 찾기 |
162 | | - List<PostImage> toDelete = post.getImages().stream() |
| 163 | + // 삭제될 이미지 (DB + S3) |
| 164 | + List<PostImage> toRemove = post.getImages().stream() |
163 | 165 | .filter(img -> !keepIds.contains(img.getId())) |
164 | 166 | .toList(); |
165 | 167 |
|
166 | | - // 최종 이미지 리스트 구성 |
167 | | - List<PostImage> finalImages = new ArrayList<>(); |
| 168 | + List<String> deleteKeysAfterCommit = toRemove.stream() |
| 169 | + .map(PostImage::getFileName) |
| 170 | + .toList(); |
| 171 | + |
| 172 | + toRemove.forEach(img -> img.updatePost(null)); // 관계 해제 |
| 173 | + post.getImages().removeAll(toRemove); // orphanRemoval 트리거 |
| 174 | + |
| 175 | + // 유지할 이미지 정렬 |
168 | 176 | int order = 0; |
169 | 177 | for (Long keepId : keepIds) { |
170 | 178 | PostImage img = existingById.get(keepId); |
171 | | - if (img != null) { |
172 | | - img.updateSortOrder(order++); |
173 | | - finalImages.add(img); |
174 | | - } |
| 179 | + if (img != null) img.updateSortOrder(order++); |
175 | 180 | } |
| 181 | + |
| 182 | + // 새 이미지 추가 |
176 | 183 | for (UploadedFileDto u : uploaded) { |
177 | | - finalImages.add(PostImage.builder() |
| 184 | + post.getImages().add(PostImage.builder() |
178 | 185 | .post(post) |
179 | 186 | .fileName(u.fileName()) |
180 | 187 | .url(u.url()) |
181 | 188 | .sortOrder(order++) |
182 | | - .build() |
183 | | - ); |
| 189 | + .build()); |
184 | 190 | } |
185 | 191 |
|
186 | | - // 삭제 예정 key 모음 |
187 | | - List<String> deleteKeysAfterCommit = toDelete.stream() |
188 | | - .map(PostImage::getFileName) |
189 | | - .toList(); |
190 | | - |
191 | | - // DB에 반영 |
192 | | - post.updateImages(finalImages); |
193 | | - |
194 | 192 | // 트랜잭션 완료 후 처리 |
195 | 193 | TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() { |
196 | 194 | @Override |
@@ -272,6 +270,17 @@ public PostLikeResponseDto toggleLike(Long postId) { |
272 | 270 | } |
273 | 271 | } |
274 | 272 |
|
| 273 | + // 사용자가 해당 게시글 여부 확인 로직 |
| 274 | + @Transactional(readOnly = true) |
| 275 | + public Boolean getLike(Long postId) { |
| 276 | + User user = rq.getActor(); |
| 277 | + |
| 278 | + Post post = postRepository.findById(postId) |
| 279 | + .orElseThrow(() -> new NoSuchElementException("해당 게시글을 찾을 수 없습니다. ID: " + postId)); |
| 280 | + |
| 281 | + return postLikeRepository.existsByPostAndUser(post, user); |
| 282 | + } |
| 283 | + |
275 | 284 | // 태그 추가 메서드 |
276 | 285 | private void addTag(List<String> tagNames, Post post) { |
277 | 286 | for (String tagName : tagNames) { |
|
0 commit comments