@@ -146,25 +146,70 @@ public PostResponse updatePost(Long postId, PostRequest request, Long userId) {
146146 }
147147
148148 // Post 업데이트
149- post .update (request .title (), request .content ());
149+ post .update (request .title (), request .content (), request . thumbnailUrl () );
150150
151151 // Category 매핑 업데이트
152152 List <PostCategory > categories = validateAndFindCategories (request .categoryIds ());
153153 post .updateCategories (categories );
154154
155- // AttachmentMapping 매핑
156- attachmentMappingRepository .deleteAllByEntityTypeAndEntityId (EntityType .POST , post .getId ());
157- List <FileAttachment > attachments = List .of ();
158- if (request .imageIds () != null && !request .imageIds ().isEmpty ()) {
159- attachments = validateAndFindAttachments (request .imageIds ());
160- attachments .forEach (attachment ->
161- attachmentMappingRepository .save (new AttachmentMapping (attachment , EntityType .POST , post .getId ()))
162- );
163- }
155+ // TODO: 리팩토링 필요
156+ // 첨부 이미지 갱신
157+ List <FileAttachment > attachments = updatePostAttachments (post , request .imageIds (), userId );
164158
165159 return PostResponse .from (post , attachments );
166160 }
167161
162+ /**
163+ * 첨부 이미지 매핑 갱신
164+ * - 이미지 변경 없음 → 유지
165+ * - 변경 있음 → 기존 파일 및 매핑 삭제 후 새로 저장
166+ * - 새로 추가됨 → 새 매핑 생성
167+ * - 제거됨 → 기존 매핑 삭제
168+ */
169+ private List <FileAttachment > updatePostAttachments (Post post , List <Long > newImageIds , Long userId ) {
170+ List <Long > newIds = (newImageIds != null ) ? newImageIds : List .of ();
171+
172+ List <AttachmentMapping > existingMappings =
173+ attachmentMappingRepository .findAllByEntityTypeAndEntityId (EntityType .POST , post .getId ());
174+ List <Long > existingIds = existingMappings .stream ()
175+ .map (m -> m .getFileAttachment ().getId ())
176+ .toList ();
177+
178+ // 변경 없음 → 유지
179+ if (existingIds .equals (newIds )) {
180+ return existingMappings .stream ()
181+ .map (AttachmentMapping ::getFileAttachment )
182+ .toList ();
183+ }
184+
185+ // 기존 첨부 삭제
186+ deletePostAttachments (post , userId );
187+
188+ // 새 첨부 매핑 등록
189+ if (newIds .isEmpty ()) return List .of ();
190+
191+ List <FileAttachment > attachments = validateAndFindAttachments (newIds );
192+ attachments .forEach (attachment ->
193+ attachmentMappingRepository .save (new AttachmentMapping (attachment , EntityType .POST , post .getId ()))
194+ );
195+ return attachments ;
196+ }
197+
198+ /**
199+ * 게시글 첨부파일 삭제 (S3 + 매핑)
200+ */
201+ private void deletePostAttachments (Post post , Long userId ) {
202+ List <AttachmentMapping > mappings =
203+ attachmentMappingRepository .findAllByEntityTypeAndEntityId (EntityType .POST , post .getId ());
204+ for (AttachmentMapping mapping : mappings ) {
205+ FileAttachment file = mapping .getFileAttachment ();
206+ if (file != null ) {
207+ fileService .deleteFile (file .getId (), userId );
208+ }
209+ }
210+ attachmentMappingRepository .deleteAllByEntityTypeAndEntityId (EntityType .POST , post .getId ());
211+ }
212+
168213 /**
169214 * 게시글 삭제 서비스
170215 *
@@ -205,6 +250,7 @@ public void deletePost(Long postId, Long userId) {
205250 * 카테고리 ID 유효성 검증 및 조회
206251 */
207252 private List <PostCategory > validateAndFindCategories (List <Long > categoryIds ) {
253+ if (categoryIds == null || categoryIds .isEmpty ()) return List .of ();
208254 return categoryIds .stream ()
209255 .map (id -> postCategoryRepository .findById (id )
210256 .orElseThrow (() -> new CustomException (ErrorCode .CATEGORY_NOT_FOUND )))
@@ -215,6 +261,7 @@ private List<PostCategory> validateAndFindCategories(List<Long> categoryIds) {
215261 * 첨부 파일 ID 유효성 검증 및 조회
216262 */
217263 private List <FileAttachment > validateAndFindAttachments (List <Long > imageIds ) {
264+ if (imageIds == null || imageIds .isEmpty ()) return List .of ();
218265 return imageIds .stream ()
219266 .map (id -> fileAttachmentRepository .findById (id )
220267 .orElseThrow (() -> new CustomException (ErrorCode .FILE_NOT_FOUND )))
0 commit comments