Skip to content

Commit 7be13b5

Browse files
committed
fix(eidt post) : 필수 항목 작성 확인, 게시글 작성자 본인 확인
1 parent e7d0d4a commit 7be13b5

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

src/main/java/com/example/FixLog/exception/ErrorCode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public enum ErrorCode {
2424
TAG_NOT_FOUND(HttpStatus.NOT_FOUND, "없는 태그 번호입니다."),
2525
SORT_NOT_EXIST(HttpStatus.BAD_REQUEST, "사용할 수 없는 정렬입니다."),
2626
INVALID_PASSWORD(HttpStatus.UNAUTHORIZED, "비밀번호가 일치하지 않습니다."),
27+
POST_UPDATE_FORBIDDEN(HttpStatus.BAD_REQUEST, "본인 게시글만 수정할 수 있습니다."),
2728
REQUIRED_TAGS_MISSING(HttpStatus.BAD_REQUEST, "태그를 선택해주세요."),
2829
REQUIRED_CONTENT_MISSING(HttpStatus.BAD_REQUEST, "필수 본문이 입력되지 않았습니다."),
2930
SAME_AS_OLD_PASSWORD(HttpStatus.BAD_REQUEST, "다른 비밀번호 입력 바랍니다."),

src/main/java/com/example/FixLog/service/PostService.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,21 @@ else if (categories.size() > 1)
151151

152152
// 게시글 필수 항목 다 작성했는지
153153
private void validatePost(PostRequestDto postRequestDto){
154-
if (postRequestDto.getPostTitle().isBlank() | postRequestDto.getProblem().isBlank()
155-
| postRequestDto.getErrorMessage().isBlank() | postRequestDto.getEnvironment().isBlank()
156-
| postRequestDto.getReproduceCode().isBlank() | postRequestDto.getSolutionCode().isBlank())
154+
if (!StringUtils.hasText(postRequestDto.getPostTitle())
155+
|| !StringUtils.hasText(postRequestDto.getProblem())
156+
|| !StringUtils.hasText(postRequestDto.getErrorMessage())
157+
|| !StringUtils.hasText(postRequestDto.getEnvironment())
158+
|| !StringUtils.hasText(postRequestDto.getReproduceCode())
159+
|| !StringUtils.hasText(postRequestDto.getSolutionCode()))
157160
throw new CustomException(ErrorCode.REQUIRED_CONTENT_MISSING);
158161
}
159162
private void validatePost(NewPostRequestDto newPostRequestDto){
160-
if (newPostRequestDto.getPostTitle().isBlank() | newPostRequestDto.getProblem().isBlank()
161-
| newPostRequestDto.getErrorMessage().isBlank() | newPostRequestDto.getEnvironment().isBlank()
162-
| newPostRequestDto.getReproduceCode().isBlank() | newPostRequestDto.getSolutionCode().isBlank())
163+
if (!StringUtils.hasText(newPostRequestDto.getPostTitle())
164+
|| !StringUtils.hasText(newPostRequestDto.getProblem())
165+
|| !StringUtils.hasText(newPostRequestDto.getErrorMessage())
166+
|| !StringUtils.hasText(newPostRequestDto.getEnvironment())
167+
|| !StringUtils.hasText(newPostRequestDto.getReproduceCode())
168+
|| !StringUtils.hasText(newPostRequestDto.getSolutionCode()))
163169
throw new CustomException(ErrorCode.REQUIRED_CONTENT_MISSING);
164170
}
165171

@@ -176,12 +182,18 @@ public String uploadImage(MultipartFile imageFile){
176182
return "![image](" + imageUrl + ")";
177183
}
178184

185+
// 게시글 수정하기
179186
@Transactional
180187
public void editPost(Long postId, NewPostRequestDto newPostRequestDto) {
181188
Member member = memberService.getCurrentMemberInfo();
182189
Post post = postRepository.findById(postId)
183190
.orElseThrow(() -> new CustomException(ErrorCode.POST_NOT_FOUND));
184191

192+
// 게시글 작성자가 본인이 맞는지
193+
if (!member.getUserId().equals(post.getUserId().getUserId())) {
194+
throw new CustomException(ErrorCode.POST_UPDATE_FORBIDDEN);
195+
}
196+
185197
// 북마크 카테고리별로 선택 제한 두기
186198
List<Tag> tags = fetchAndValidateTags(newPostRequestDto.getTags());
187199

@@ -201,6 +213,8 @@ & compareTags(post.getPostTags(), tags)){
201213
}
202214

203215
// 필드 업데이트
216+
validatePost(newPostRequestDto);
217+
204218
post.changeTitle(newPostRequestDto.getPostTitle());
205219
post.changeCoverImage(newPostRequestDto.getCoverImageUrl());
206220
post.changeProblem(newPostRequestDto.getProblem());

0 commit comments

Comments
 (0)