Skip to content

Commit

Permalink
🔨 fix: 잘못된 검증 로직 삭제 및 예외 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
strongmhk committed Dec 26, 2024
1 parent 9a63075 commit edf6241
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public enum CommunityPostErrorStatus implements BaseErrorCodeInterface {
_INVALID_POST_STATUS(HttpStatus.BAD_REQUEST, "POST_4003", "유효하지 않은 게시글 상태값입니다."),
_ALREADY_EXIST_POST_LIKES(HttpStatus.BAD_REQUEST, "POST_4004", "이미 좋아요한 게시글입니다."),
_ALREADY_EXIST_POST_BOOKMARK(HttpStatus.BAD_REQUEST, "POST_4005", "이미 북마크한 게시글입니다."),
_BOOKMARK_CNT_NEGATIVE(HttpStatus.BAD_REQUEST, "POST_4006", "북마크 수는 0보다 작을 수 없습니다."),
_LIKE_CNT_NEGATIVE(HttpStatus.BAD_REQUEST, "POST_4007", "좋아요 수는 0보다 작을 수 없습니다."),
_POP_SCORE_NEGATIVE(HttpStatus.BAD_REQUEST, "POST_4007", "인기점수는 0보다 작을 수 없습니다."),

_NOT_AUTHORIZED(HttpStatus.FORBIDDEN, "POST_4031", "해당 게시글에 접근 권한이 없습니다.")
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import gaji.service.domain.common.service.HashtagService;
import gaji.service.domain.enums.CategoryEnum;
import gaji.service.domain.enums.PostTypeEnum;
import gaji.service.domain.post.code.CommunityPostErrorStatus;
import gaji.service.domain.post.converter.CommunityCommentConverter;
import gaji.service.domain.post.converter.CommunityPostConverter;
import gaji.service.domain.post.entity.CommnuityPost;
Expand All @@ -24,6 +25,7 @@
import gaji.service.domain.post.web.dto.CommunityPostResponseDTO;
import gaji.service.domain.user.entity.User;
import gaji.service.domain.user.service.UserQueryService;
import gaji.service.global.exception.RestApiException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -82,8 +84,6 @@ public CommunityPostResponseDTO.PostIdResponseDTO editPost(Long userId, Long pos
// 작성자 검증
communityPostQueryService.validPostWriter(userId, findPost);



return null;
}

Expand Down Expand Up @@ -150,14 +150,15 @@ public void cancelbookmarkCommunityPost(Long userId, Long postId) {
User findUser = userQueryService.findUserById(userId);
CommnuityPost findPost = communityPostQueryService.findPostByPostId(postId);

// 검증
communityPostQueryService.validPostWriter(findUser.getId(), findPost);

// 삭제
postBookmarkRepository.deleteByUserAndPost(findUser, findPost);

// 게시글 북마크 수 감소
findPost.decreaseBookmarkCnt();

if (findPost.getBookmarkCnt() < 0) {
throw new RestApiException(CommunityPostErrorStatus._BOOKMARK_CNT_NEGATIVE);
}
}

@Override
Expand All @@ -182,13 +183,19 @@ public void cancelLikeCommunityPost(Long userId, Long postId) {
User findUser = userQueryService.findUserById(userId);
CommnuityPost findPost = communityPostQueryService.findPostByPostId(postId);

// TODO: like owner인지 검증

// 삭제
postLikesRepository.deleteByUserAndPost(findUser, findPost);

// 좋아요 수, 인기점수 감소
findPost.decreaseLikeCnt();
findPost.decreasePopularityScoreByLike();

if (findPost.getBookmarkCnt() < 0) {
throw new RestApiException(CommunityPostErrorStatus._LIKE_CNT_NEGATIVE);
}

if (findPost.getPopularityScore() < 0) {
throw new RestApiException(CommunityPostErrorStatus._LIKE_CNT_NEGATIVE);
}
}
}

0 comments on commit edf6241

Please sign in to comment.