Skip to content

Commit

Permalink
[J2KB-3rd-Season#25]feat: 게시글 수정 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
enjay27 committed Jun 11, 2021
1 parent 888e70b commit 7b06509
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,9 @@ public DefaultResponse<?> changePostLike(@PathVariable("id") Long postId, HttpSe
return postService.changePostLike(postId, request);
}

@PutMapping("/post/{id}/update")
public DefaultResponse<?> reply(@PathVariable("id") Long replyId, @RequestBody PostUpdateForm form, HttpServletRequest request) {
return postService.editPost(replyId, form, request);
}

}
25 changes: 25 additions & 0 deletions src/main/java/com/devin/dev/controller/post/PostUpdateForm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.devin.dev.controller.post;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Data
@NoArgsConstructor
public class PostUpdateForm {
private Long id;
private String title;
private String content;
private List<String> post_images = new ArrayList<>();
private List<String> post_tags = new ArrayList<>();

public PostUpdateForm(Long id, String title, String content, List<String> post_images, List<String> post_tags) {
this.id = id;
this.title = title;
this.content = content;
this.post_images = post_images;
this.post_tags = post_tags;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public Page<Post> findAllByTagId(Long id, Pageable pageable) {
}

@Override
public Optional<PostDetailsDto> findPostDetailsById(Long id, ReplyOrderCondition condition) {
public Optional<PostDetailsDto> findPostDetailsByIdWithUserType(Long id, UserStatus status, ReplyOrderCondition condition) {
Post result = queryFactory
.select(post)
.from(post)
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/com/devin/dev/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.devin.dev.controller.post.PostForm;
import com.devin.dev.controller.post.PostSearchCondition;
import com.devin.dev.controller.post.PostUpdateForm;
import com.devin.dev.controller.post.ReplyUpdateForm;
import com.devin.dev.controller.reply.ReplyOrderCondition;
import com.devin.dev.dto.post.PostDetailsDto;
import com.devin.dev.dto.post.PostInfoDto;
Expand Down Expand Up @@ -283,6 +285,58 @@ public DefaultResponse<?> changePostLike(Long postId, HttpServletRequest request
private boolean isNotSameUser(User firstUser, User secondUser) {
return !firstUser.getId().equals(secondUser.getId());
}

@Transactional
public DefaultResponse<?> editPost(Long postId, PostUpdateForm form, HttpServletRequest request) {
String token = tokenProvider.parseToken(request);
Long userId;
if (tokenProvider.validateToken(token)) {
userId = tokenProvider.getUserId(token);
} else {
return new DefaultResponse<>(StatusCode.FAIL_AUTH, ResponseMessage.NOT_FOUND_USER);
}
Optional<User> userOptional = userRepository.findById(userId);
if (userOptional.isEmpty()) {
return new DefaultResponse<>(StatusCode.NOT_EXIST, ResponseMessage.NOT_FOUND_USER);
}
Optional<Post> postOptional = postRepository.findById(postId);
if (postOptional.isEmpty()) {
return new DefaultResponse<>(StatusCode.NOT_EXIST, ResponseMessage.NOT_FOUND_POST);
}
Post post = postOptional.get();
List<Subject> postSubjects = subjectRepository.findByNameIn(form.getPost_tags());

// 기존 태그 및 이미지 경로 삭제
List<PostImage> postImages = postImageRepository.findByPost(post);
postImageRepository.deleteInBatch(postImages);

// 수정된 내용 반영
List<PostImage> newPostImages = PostImage.createPostImages(form.getPost_images());
post.setTitle(form.getTitle());
post.setContent(form.getContent());
List<PostTag> deleteList = new ArrayList<>();

for (PostTag tag : post.getTags()) {
if (!postSubjects.contains(tag.getTag())) {
deleteList.add(tag);
} else {
postSubjects.remove(tag.getTag());
}
}
List<PostTag> newPostTags = PostTag.createPostTags(postSubjects);
postTagRepository.deleteInBatch(deleteList);

post.setPostTags(newPostTags);
post.setPostImages(newPostImages);

// 저장
postImageRepository.saveAll(newPostImages);
postRepository.save(post);
postTagRepository.saveAll(newPostTags);

// 성공 메시지 및 코드 반환
return new DefaultResponse<>(StatusCode.SUCCESS, ResponseMessage.POST_EDIT_SUCCESS);
}
}


0 comments on commit 7b06509

Please sign in to comment.