Skip to content

Commit 7b06509

Browse files
committed
[J2KB-3rd-Season#25]feat: 게시글 수정 추가
1 parent 888e70b commit 7b06509

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

src/main/java/com/devin/dev/controller/post/PostController.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,9 @@ public DefaultResponse<?> changePostLike(@PathVariable("id") Long postId, HttpSe
6060
return postService.changePostLike(postId, request);
6161
}
6262

63+
@PutMapping("/post/{id}/update")
64+
public DefaultResponse<?> reply(@PathVariable("id") Long replyId, @RequestBody PostUpdateForm form, HttpServletRequest request) {
65+
return postService.editPost(replyId, form, request);
66+
}
67+
6368
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.devin.dev.controller.post;
2+
3+
import lombok.Data;
4+
import lombok.NoArgsConstructor;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
@Data
10+
@NoArgsConstructor
11+
public class PostUpdateForm {
12+
private Long id;
13+
private String title;
14+
private String content;
15+
private List<String> post_images = new ArrayList<>();
16+
private List<String> post_tags = new ArrayList<>();
17+
18+
public PostUpdateForm(Long id, String title, String content, List<String> post_images, List<String> post_tags) {
19+
this.id = id;
20+
this.title = title;
21+
this.content = content;
22+
this.post_images = post_images;
23+
this.post_tags = post_tags;
24+
}
25+
}

src/main/java/com/devin/dev/repository/post/PostRepositoryQueryImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public Page<Post> findAllByTagId(Long id, Pageable pageable) {
122122
}
123123

124124
@Override
125-
public Optional<PostDetailsDto> findPostDetailsById(Long id, ReplyOrderCondition condition) {
125+
public Optional<PostDetailsDto> findPostDetailsByIdWithUserType(Long id, UserStatus status, ReplyOrderCondition condition) {
126126
Post result = queryFactory
127127
.select(post)
128128
.from(post)

src/main/java/com/devin/dev/service/PostService.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.devin.dev.controller.post.PostForm;
44
import com.devin.dev.controller.post.PostSearchCondition;
5+
import com.devin.dev.controller.post.PostUpdateForm;
6+
import com.devin.dev.controller.post.ReplyUpdateForm;
57
import com.devin.dev.controller.reply.ReplyOrderCondition;
68
import com.devin.dev.dto.post.PostDetailsDto;
79
import com.devin.dev.dto.post.PostInfoDto;
@@ -283,6 +285,58 @@ public DefaultResponse<?> changePostLike(Long postId, HttpServletRequest request
283285
private boolean isNotSameUser(User firstUser, User secondUser) {
284286
return !firstUser.getId().equals(secondUser.getId());
285287
}
288+
289+
@Transactional
290+
public DefaultResponse<?> editPost(Long postId, PostUpdateForm form, HttpServletRequest request) {
291+
String token = tokenProvider.parseToken(request);
292+
Long userId;
293+
if (tokenProvider.validateToken(token)) {
294+
userId = tokenProvider.getUserId(token);
295+
} else {
296+
return new DefaultResponse<>(StatusCode.FAIL_AUTH, ResponseMessage.NOT_FOUND_USER);
297+
}
298+
Optional<User> userOptional = userRepository.findById(userId);
299+
if (userOptional.isEmpty()) {
300+
return new DefaultResponse<>(StatusCode.NOT_EXIST, ResponseMessage.NOT_FOUND_USER);
301+
}
302+
Optional<Post> postOptional = postRepository.findById(postId);
303+
if (postOptional.isEmpty()) {
304+
return new DefaultResponse<>(StatusCode.NOT_EXIST, ResponseMessage.NOT_FOUND_POST);
305+
}
306+
Post post = postOptional.get();
307+
List<Subject> postSubjects = subjectRepository.findByNameIn(form.getPost_tags());
308+
309+
// 기존 태그 및 이미지 경로 삭제
310+
List<PostImage> postImages = postImageRepository.findByPost(post);
311+
postImageRepository.deleteInBatch(postImages);
312+
313+
// 수정된 내용 반영
314+
List<PostImage> newPostImages = PostImage.createPostImages(form.getPost_images());
315+
post.setTitle(form.getTitle());
316+
post.setContent(form.getContent());
317+
List<PostTag> deleteList = new ArrayList<>();
318+
319+
for (PostTag tag : post.getTags()) {
320+
if (!postSubjects.contains(tag.getTag())) {
321+
deleteList.add(tag);
322+
} else {
323+
postSubjects.remove(tag.getTag());
324+
}
325+
}
326+
List<PostTag> newPostTags = PostTag.createPostTags(postSubjects);
327+
postTagRepository.deleteInBatch(deleteList);
328+
329+
post.setPostTags(newPostTags);
330+
post.setPostImages(newPostImages);
331+
332+
// 저장
333+
postImageRepository.saveAll(newPostImages);
334+
postRepository.save(post);
335+
postTagRepository.saveAll(newPostTags);
336+
337+
// 성공 메시지 및 코드 반환
338+
return new DefaultResponse<>(StatusCode.SUCCESS, ResponseMessage.POST_EDIT_SUCCESS);
339+
}
286340
}
287341

288342

0 commit comments

Comments
 (0)