77import com .back .domain .board .post .dto .PostListResponse ;
88import com .back .domain .board .post .dto .PostRequest ;
99import com .back .domain .board .post .dto .PostResponse ;
10+ import com .back .domain .board .post .entity .PostCategoryMapping ;
1011import com .back .domain .board .post .repository .PostBookmarkRepository ;
1112import com .back .domain .board .post .repository .PostCategoryRepository ;
1213import com .back .domain .board .post .repository .PostLikeRepository ;
1314import com .back .domain .board .post .repository .PostRepository ;
15+ import com .back .domain .file .entity .AttachmentMapping ;
16+ import com .back .domain .file .entity .EntityType ;
17+ import com .back .domain .file .entity .FileAttachment ;
18+ import com .back .domain .file .repository .AttachmentMappingRepository ;
19+ import com .back .domain .file .repository .FileAttachmentRepository ;
1420import com .back .domain .user .common .entity .User ;
1521import com .back .domain .user .common .repository .UserRepository ;
1622import com .back .global .exception .CustomException ;
@@ -32,6 +38,8 @@ public class PostService {
3238 private final PostBookmarkRepository postBookmarkRepository ;
3339 private final UserRepository userRepository ;
3440 private final PostCategoryRepository postCategoryRepository ;
41+ private final FileAttachmentRepository fileAttachmentRepository ;
42+ private final AttachmentMappingRepository attachmentMappingRepository ;
3543
3644 /**
3745 * 게시글 생성 서비스
@@ -47,15 +55,24 @@ public PostResponse createPost(PostRequest request, Long userId) {
4755
4856 // Post 생성
4957 Post post = new Post (user , request .title (), request .content (), request .thumbnailUrl ());
50- Post saved = postRepository .save (post );
58+ postRepository .save (post );
5159
5260 // Category 매핑
5361 if (request .categoryIds () != null ) {
5462 List <PostCategory > categories = validateAndFindCategories (request .categoryIds ());
55- saved . updateCategories ( categories );
63+ categories . forEach ( category -> new PostCategoryMapping ( post , category ) );
5664 }
5765
58- return PostResponse .from (saved );
66+ // AttachmentMapping 매핑
67+ List <FileAttachment > attachments = List .of ();
68+ if (request .imageIds () != null && !request .imageIds ().isEmpty ()) {
69+ attachments = validateAndFindAttachments (request .imageIds ());
70+ for (FileAttachment attachment : attachments ) {
71+ attachmentMappingRepository .save (new AttachmentMapping (attachment , EntityType .POST , post .getId ()));
72+ }
73+ }
74+
75+ return PostResponse .from (post , attachments );
5976 }
6077
6178 /**
@@ -86,15 +103,22 @@ public PostDetailResponse getPost(Long postId, Long userId) {
86103 Post post = postRepository .findById (postId )
87104 .orElseThrow (() -> new CustomException (ErrorCode .POST_NOT_FOUND ));
88105
106+ // 첨부파일 조회
107+ List <FileAttachment > attachments = attachmentMappingRepository
108+ .findAllByEntityTypeAndEntityId (EntityType .POST , post .getId ())
109+ .stream ()
110+ .map (AttachmentMapping ::getFileAttachment )
111+ .toList ();
112+
89113 // 로그인 사용자 추가 데이터 설정 (좋아요, 북마크 여부)
90114 if (userId != null ) {
91115 boolean likedByMe = postLikeRepository .existsByUserIdAndPostId (userId , post .getId ());
92116 boolean bookmarkedByMe = postBookmarkRepository .existsByUserIdAndPostId (userId , post .getId ());
93- return PostDetailResponse .from (post , likedByMe , bookmarkedByMe );
117+ return PostDetailResponse .from (post , attachments , likedByMe , bookmarkedByMe );
94118 }
95119
96120 // 비로그인 사용자는 기본 응답 반환
97- return PostDetailResponse .from (post );
121+ return PostDetailResponse .from (post , attachments );
98122 }
99123
100124 /**
@@ -126,7 +150,17 @@ public PostResponse updatePost(Long postId, PostRequest request, Long userId) {
126150 List <PostCategory > categories = validateAndFindCategories (request .categoryIds ());
127151 post .updateCategories (categories );
128152
129- return PostResponse .from (post );
153+ // AttachmentMapping 매핑
154+ attachmentMappingRepository .deleteAllByEntityTypeAndEntityId (EntityType .POST , post .getId ());
155+ List <FileAttachment > attachments = List .of ();
156+ if (request .imageIds () != null && !request .imageIds ().isEmpty ()) {
157+ attachments = validateAndFindAttachments (request .imageIds ());
158+ attachments .forEach (attachment ->
159+ attachmentMappingRepository .save (new AttachmentMapping (attachment , EntityType .POST , post .getId ()))
160+ );
161+ }
162+
163+ return PostResponse .from (post , attachments );
130164 }
131165
132166 /**
@@ -149,6 +183,9 @@ public void deletePost(Long postId, Long userId) {
149183 throw new CustomException (ErrorCode .POST_NO_PERMISSION );
150184 }
151185
186+ // AttachmentMapping 매핑 제거
187+ attachmentMappingRepository .deleteAllByEntityTypeAndEntityId (EntityType .POST , post .getId ());
188+
152189 // Post 삭제
153190 post .remove ();
154191 postRepository .delete (post );
@@ -158,11 +195,19 @@ public void deletePost(Long postId, Long userId) {
158195 * 카테고리 ID 유효성 검증 및 조회
159196 */
160197 private List <PostCategory > validateAndFindCategories (List <Long > categoryIds ) {
161- List <PostCategory > categories = postCategoryRepository .findAllById (categoryIds );
198+ return categoryIds .stream ()
199+ .map (id -> postCategoryRepository .findById (id )
200+ .orElseThrow (() -> new CustomException (ErrorCode .CATEGORY_NOT_FOUND )))
201+ .toList ();
202+ }
162203
163- if (categories .size () != categoryIds .size ()) {
164- throw new CustomException (ErrorCode .CATEGORY_NOT_FOUND );
165- }
166- return categories ;
204+ /**
205+ * 첨부 파일 ID 유효성 검증 및 조회
206+ */
207+ private List <FileAttachment > validateAndFindAttachments (List <Long > imageIds ) {
208+ return imageIds .stream ()
209+ .map (id -> fileAttachmentRepository .findById (id )
210+ .orElseThrow (() -> new CustomException (ErrorCode .FILE_NOT_FOUND )))
211+ .toList ();
167212 }
168213}
0 commit comments