|
8 | 8 | import com.example.FixLog.domain.post.PostTag; |
9 | 9 | import com.example.FixLog.domain.tag.Tag; |
10 | 10 | import com.example.FixLog.domain.tag.TagCategory; |
| 11 | +import com.example.FixLog.dto.post.NewPostRequestDto; |
11 | 12 | import com.example.FixLog.dto.post.PostDto; |
12 | 13 | import com.example.FixLog.dto.post.PostRequestDto; |
13 | 14 | import com.example.FixLog.dto.post.PostResponseDto; |
@@ -62,7 +63,7 @@ public String getDefaultImage(String image){ |
62 | 63 | return imageUrl; |
63 | 64 | } |
64 | 65 |
|
65 | | - // 게시글 생성하기 |
| 66 | + // 게시글 작성하기 |
66 | 67 | @Transactional |
67 | 68 | public void createPost(PostRequestDto postRequestDto){ |
68 | 69 | Member member = memberService.getCurrentMemberInfo(); |
@@ -99,18 +100,6 @@ public void createPost(PostRequestDto postRequestDto){ |
99 | 100 | postRepository.save(newPost); |
100 | 101 | } |
101 | 102 |
|
102 | | - // 이미지 파일 마크다운으로 변경 |
103 | | - public String uploadImage(MultipartFile imageFile){ |
104 | | - SecurityContextHolder.getContext().getAuthentication(); |
105 | | - |
106 | | - if (imageFile == null || imageFile.isEmpty()){ |
107 | | - throw new CustomException(ErrorCode.IMAGE_UPLOAD_FAILED); |
108 | | - } |
109 | | - |
110 | | - String imageUrl = s3Service.upload(imageFile, "post-image"); |
111 | | - return ""; |
112 | | - } |
113 | | - |
114 | 103 | // 태그 다 선택 했는지 |
115 | 104 | private List<Tag> fetchAndValidateTags(List<Long> tagIds){ |
116 | 105 | // 태그 ID로 Tag 엔티티 조회 |
@@ -160,6 +149,78 @@ private void validatePost(PostRequestDto postRequestDto){ |
160 | 149 | | postRequestDto.getReproduceCode().isBlank() | postRequestDto.getSolutionCode().isBlank()) |
161 | 150 | throw new CustomException(ErrorCode.REQUIRED_CONTENT_MISSING); |
162 | 151 | } |
| 152 | + private void validatePost(NewPostRequestDto newPostRequestDto){ |
| 153 | + if (newPostRequestDto.getPostTitle().isBlank() | newPostRequestDto.getProblem().isBlank() |
| 154 | + | newPostRequestDto.getErrorMessage().isBlank() | newPostRequestDto.getEnvironment().isBlank() |
| 155 | + | newPostRequestDto.getReproduceCode().isBlank() | newPostRequestDto.getSolutionCode().isBlank()) |
| 156 | + throw new CustomException(ErrorCode.REQUIRED_CONTENT_MISSING); |
| 157 | + } |
| 158 | + |
| 159 | + // 이미지 파일 마크다운으로 변경 |
| 160 | + @Transactional |
| 161 | + public String uploadImage(MultipartFile imageFile){ |
| 162 | + SecurityContextHolder.getContext().getAuthentication(); |
| 163 | + |
| 164 | + if (imageFile == null || imageFile.isEmpty()){ |
| 165 | + throw new CustomException(ErrorCode.IMAGE_UPLOAD_FAILED); |
| 166 | + } |
| 167 | + |
| 168 | + String imageUrl = s3Service.upload(imageFile, "post-image"); |
| 169 | + return ""; |
| 170 | + } |
| 171 | + |
| 172 | + @Transactional |
| 173 | + public void editPost(Long postId, NewPostRequestDto newPostRequestDto) { |
| 174 | + Member member = memberService.getCurrentMemberInfo(); |
| 175 | + Post post = postRepository.findById(postId) |
| 176 | + .orElseThrow(() -> new CustomException(ErrorCode.POST_NOT_FOUND)); |
| 177 | + |
| 178 | + // 북마크 카테고리별로 선택 제한 두기 |
| 179 | + List<Tag> tags = fetchAndValidateTags(newPostRequestDto.getTags()); |
| 180 | + |
| 181 | + // 아무것도 변경이 없으면 예외처리 |
| 182 | + if (Objects.equals(post.getPostTitle(), newPostRequestDto.getPostTitle()) |
| 183 | + & Objects.equals(post.getCoverImage(), newPostRequestDto.getCoverImageUrl()) |
| 184 | + & Objects.equals(post.getProblem(), newPostRequestDto.getProblem()) |
| 185 | + & Objects.equals(post.getErrorMessage(), newPostRequestDto.getErrorMessage()) |
| 186 | + & Objects.equals(post.getEnvironment(), newPostRequestDto.getEnvironment()) |
| 187 | + & Objects.equals(post.getReproduceCode(), newPostRequestDto.getReproduceCode()) |
| 188 | + & Objects.equals(post.getSolutionCode(), newPostRequestDto.getSolutionCode()) |
| 189 | + & Objects.equals(post.getCauseAnalysis(), newPostRequestDto.getCauseAnalysis()) |
| 190 | + & Objects.equals(post.getReferenceLink(), newPostRequestDto.getReferenceLink()) |
| 191 | + & Objects.equals(post.getExtraContent(), newPostRequestDto.getExtraContent()) |
| 192 | + & compareTags(post.getPostTags(), tags)){ |
| 193 | + throw new CustomException(ErrorCode.NO_CONTENT_CHANGED); |
| 194 | + } |
| 195 | + |
| 196 | + // 필드 업데이트 |
| 197 | + post.changeTitle(newPostRequestDto.getPostTitle()); |
| 198 | + post.changeCoverImage(newPostRequestDto.getCoverImageUrl()); |
| 199 | + post.changeProblem(newPostRequestDto.getProblem()); |
| 200 | + post.changeErrorMessage(newPostRequestDto.getErrorMessage()); |
| 201 | + post.changeEnvironment(newPostRequestDto.getEnvironment()); |
| 202 | + post.changeReproduceCode(newPostRequestDto.getReproduceCode()); |
| 203 | + post.changeSolutionCode(newPostRequestDto.getSolutionCode()); |
| 204 | + post.changeCauseAnalysis(newPostRequestDto.getCauseAnalysis()); |
| 205 | + post.changeReferenceLink(newPostRequestDto.getReferenceLink()); |
| 206 | + post.changeExtraContent(newPostRequestDto.getExtraContent()); |
| 207 | + post.updateEditedAt(LocalDateTime.now()); |
| 208 | + |
| 209 | + // 태그 저장 |
| 210 | + post.clearTags(); // 기존 태그 다 제거 |
| 211 | + for (Tag tag : tags) { |
| 212 | + PostTag postTag = new PostTag(post, tag); |
| 213 | + post.getPostTags().add(postTag); |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + private boolean compareTags(List<PostTag> currentPostTags, List<Tag> newTags) { |
| 218 | + List<Tag> currentTags = currentPostTags.stream() |
| 219 | + .map(PostTag::getTagId) |
| 220 | + .toList(); |
| 221 | + |
| 222 | + return new HashSet<>(currentTags).equals(new HashSet<>(newTags)); |
| 223 | + } |
163 | 224 |
|
164 | 225 | // 게시글 조회하기 |
165 | 226 | public PostResponseDto viewPost(Long postId){ |
|
0 commit comments