-
Notifications
You must be signed in to change notification settings - Fork 0
[FEAT] 게시글 수정하기 #93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FEAT] 게시글 수정하기 #93
Changes from all commits
524e680
10030fd
d2d3fe5
07170a4
82c5f55
387097a
566b405
eef549c
cc66255
3289b54
ace983a
78eb814
2510cf2
fa574df
9c012c2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package com.example.FixLog.dto.post; | ||
|
|
||
| import lombok.Getter; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Getter | ||
| public class NewPostRequestDto { | ||
| private String postTitle; | ||
| private String coverImageUrl; | ||
| private String problem; | ||
| private String errorMessage; | ||
| private String environment; | ||
| private String reproduceCode; | ||
| private String solutionCode; | ||
| private String causeAnalysis; | ||
| private String referenceLink; | ||
| private String extraContent; | ||
|
|
||
| private List<Long> tags; | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.example.FixLog.domain.post.PostTag; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.example.FixLog.domain.tag.Tag; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.example.FixLog.domain.tag.TagCategory; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.example.FixLog.dto.post.NewPostRequestDto; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.example.FixLog.dto.post.PostDto; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.example.FixLog.dto.post.PostRequestDto; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.example.FixLog.dto.post.PostResponseDto; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -62,7 +63,7 @@ public String getDefaultImage(String image){ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return imageUrl; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 게시글 생성하기 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 게시글 작성하기 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Transactional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void createPost(PostRequestDto postRequestDto){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member member = memberService.getCurrentMemberInfo(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -99,18 +100,6 @@ public void createPost(PostRequestDto postRequestDto){ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| postRepository.save(newPost); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 이미지 파일 마크다운으로 변경 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public String uploadImage(MultipartFile imageFile){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SecurityContextHolder.getContext().getAuthentication(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (imageFile == null || imageFile.isEmpty()){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new CustomException(ErrorCode.IMAGE_UPLOAD_FAILED); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String imageUrl = s3Service.upload(imageFile, "post-image"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ""; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 태그 다 선택 했는지 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private List<Tag> fetchAndValidateTags(List<Long> tagIds){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 태그 ID로 Tag 엔티티 조회 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -160,6 +149,78 @@ private void validatePost(PostRequestDto postRequestDto){ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | postRequestDto.getReproduceCode().isBlank() | postRequestDto.getSolutionCode().isBlank()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new CustomException(ErrorCode.REQUIRED_CONTENT_MISSING); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private void validatePost(NewPostRequestDto newPostRequestDto){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (newPostRequestDto.getPostTitle().isBlank() | newPostRequestDto.getProblem().isBlank() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | newPostRequestDto.getErrorMessage().isBlank() | newPostRequestDto.getEnvironment().isBlank() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | newPostRequestDto.getReproduceCode().isBlank() | newPostRequestDto.getSolutionCode().isBlank()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new CustomException(ErrorCode.REQUIRED_CONTENT_MISSING); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 이미지 파일 마크다운으로 변경 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Transactional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public String uploadImage(MultipartFile imageFile){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SecurityContextHolder.getContext().getAuthentication(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+162
to
+163
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 사용하지 않는 인증 정보 조회를 제거하세요. 인증 정보를 가져오지만 사용하지 않고 있습니다. @Transactional
public String uploadImage(MultipartFile imageFile){
- SecurityContextHolder.getContext().getAuthentication();
-
if (imageFile == null || imageFile.isEmpty()){
throw new CustomException(ErrorCode.IMAGE_UPLOAD_FAILED);
}🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (imageFile == null || imageFile.isEmpty()){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new CustomException(ErrorCode.IMAGE_UPLOAD_FAILED); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String imageUrl = s3Service.upload(imageFile, "post-image"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ""; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Transactional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void editPost(Long postId, NewPostRequestDto newPostRequestDto) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member member = memberService.getCurrentMemberInfo(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Post post = postRepository.findById(postId) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .orElseThrow(() -> new CustomException(ErrorCode.POST_NOT_FOUND)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 북마크 카테고리별로 선택 제한 두기 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| List<Tag> tags = fetchAndValidateTags(newPostRequestDto.getTags()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+172
to
+180
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 게시글 작성자 권한 검증이 필요합니다. 현재 사용자가 게시글 작성자인지 확인하는 로직이 없습니다. @Transactional
public void editPost(Long postId, NewPostRequestDto newPostRequestDto) {
Member member = memberService.getCurrentMemberInfo();
Post post = postRepository.findById(postId)
.orElseThrow(() -> new CustomException(ErrorCode.POST_NOT_FOUND));
+ // 게시글 작성자 확인
+ if (!post.getUserId().equals(member)) {
+ throw new CustomException(ErrorCode.ACCESS_DENIED);
+ }
+
// 북마크 카테고리별로 선택 제한 두기
List<Tag> tags = fetchAndValidateTags(newPostRequestDto.getTags());🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 아무것도 변경이 없으면 예외처리 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (Objects.equals(post.getPostTitle(), newPostRequestDto.getPostTitle()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| & Objects.equals(post.getCoverImage(), newPostRequestDto.getCoverImageUrl()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| & Objects.equals(post.getProblem(), newPostRequestDto.getProblem()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| & Objects.equals(post.getErrorMessage(), newPostRequestDto.getErrorMessage()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| & Objects.equals(post.getEnvironment(), newPostRequestDto.getEnvironment()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| & Objects.equals(post.getReproduceCode(), newPostRequestDto.getReproduceCode()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| & Objects.equals(post.getSolutionCode(), newPostRequestDto.getSolutionCode()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| & Objects.equals(post.getCauseAnalysis(), newPostRequestDto.getCauseAnalysis()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| & Objects.equals(post.getReferenceLink(), newPostRequestDto.getReferenceLink()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| & Objects.equals(post.getExtraContent(), newPostRequestDto.getExtraContent()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| & compareTags(post.getPostTags(), tags)){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new CustomException(ErrorCode.NO_CONTENT_CHANGED); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+182
to
+194
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 논리 연산자를 사용하세요. 비트 연산자( // 아무것도 변경이 없으면 예외처리
if (Objects.equals(post.getPostTitle(), newPostRequestDto.getPostTitle())
- & Objects.equals(post.getCoverImage(), newPostRequestDto.getCoverImageUrl())
- & Objects.equals(post.getProblem(), newPostRequestDto.getProblem())
- & Objects.equals(post.getErrorMessage(), newPostRequestDto.getErrorMessage())
- & Objects.equals(post.getEnvironment(), newPostRequestDto.getEnvironment())
- & Objects.equals(post.getReproduceCode(), newPostRequestDto.getReproduceCode())
- & Objects.equals(post.getSolutionCode(), newPostRequestDto.getSolutionCode())
- & Objects.equals(post.getCauseAnalysis(), newPostRequestDto.getCauseAnalysis())
- & Objects.equals(post.getReferenceLink(), newPostRequestDto.getReferenceLink())
- & Objects.equals(post.getExtraContent(), newPostRequestDto.getExtraContent())
- & compareTags(post.getPostTags(), tags)){
+ && Objects.equals(post.getCoverImage(), newPostRequestDto.getCoverImageUrl())
+ && Objects.equals(post.getProblem(), newPostRequestDto.getProblem())
+ && Objects.equals(post.getErrorMessage(), newPostRequestDto.getErrorMessage())
+ && Objects.equals(post.getEnvironment(), newPostRequestDto.getEnvironment())
+ && Objects.equals(post.getReproduceCode(), newPostRequestDto.getReproduceCode())
+ && Objects.equals(post.getSolutionCode(), newPostRequestDto.getSolutionCode())
+ && Objects.equals(post.getCauseAnalysis(), newPostRequestDto.getCauseAnalysis())
+ && Objects.equals(post.getReferenceLink(), newPostRequestDto.getReferenceLink())
+ && Objects.equals(post.getExtraContent(), newPostRequestDto.getExtraContent())
+ && compareTags(post.getPostTags(), tags)){
throw new CustomException(ErrorCode.NO_CONTENT_CHANGED);
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 필드 업데이트 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| post.changeTitle(newPostRequestDto.getPostTitle()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| post.changeCoverImage(newPostRequestDto.getCoverImageUrl()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| post.changeProblem(newPostRequestDto.getProblem()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| post.changeErrorMessage(newPostRequestDto.getErrorMessage()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| post.changeEnvironment(newPostRequestDto.getEnvironment()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| post.changeReproduceCode(newPostRequestDto.getReproduceCode()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| post.changeSolutionCode(newPostRequestDto.getSolutionCode()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| post.changeCauseAnalysis(newPostRequestDto.getCauseAnalysis()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| post.changeReferenceLink(newPostRequestDto.getReferenceLink()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| post.changeExtraContent(newPostRequestDto.getExtraContent()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| post.updateEditedAt(LocalDateTime.now()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 태그 저장 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| post.clearTags(); // 기존 태그 다 제거 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (Tag tag : tags) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PostTag postTag = new PostTag(post, tag); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| post.getPostTags().add(postTag); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private boolean compareTags(List<PostTag> currentPostTags, List<Tag> newTags) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| List<Tag> currentTags = currentPostTags.stream() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .map(PostTag::getTagId) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .toList(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return new HashSet<>(currentTags).equals(new HashSet<>(newTags)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 게시글 조회하기 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public PostResponseDto viewPost(Long postId){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
논리 연산자를 사용하세요.
비트 연산자(
|) 대신 논리 연산자(||)를 사용해야 short-circuit evaluation이 가능합니다.private void validatePost(NewPostRequestDto newPostRequestDto){ - if (newPostRequestDto.getPostTitle().isBlank() | newPostRequestDto.getProblem().isBlank() - | newPostRequestDto.getErrorMessage().isBlank() | newPostRequestDto.getEnvironment().isBlank() - | newPostRequestDto.getReproduceCode().isBlank() | newPostRequestDto.getSolutionCode().isBlank()) + if (newPostRequestDto.getPostTitle().isBlank() || newPostRequestDto.getProblem().isBlank() + || newPostRequestDto.getErrorMessage().isBlank() || newPostRequestDto.getEnvironment().isBlank() + || newPostRequestDto.getReproduceCode().isBlank() || newPostRequestDto.getSolutionCode().isBlank()) throw new CustomException(ErrorCode.REQUIRED_CONTENT_MISSING); }📝 Committable suggestion
🤖 Prompt for AI Agents