Skip to content

Commit

Permalink
[feat] #71 scrap 중복 예외 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
RyuKwanKon committed Apr 1, 2024
1 parent 0ca5c91 commit b8404c5
Showing 1 changed file with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import org.gachon.checkmate.domain.scrap.dto.support.ScrapSearchCondition;
import org.gachon.checkmate.domain.scrap.entity.Scrap;
import org.gachon.checkmate.domain.scrap.repository.ScrapRepository;
import org.gachon.checkmate.global.error.exception.ConflictException;
import org.gachon.checkmate.global.error.exception.EntityNotFoundException;
import org.gachon.checkmate.global.error.exception.InvalidValueException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -48,6 +50,7 @@ public PostSearchResponseDto getScrapPosts(Long userId, Pageable pageable) {
public void creatScrapPost(Long userId, ScrapRequestDto scrapRequestDto) {
User user = findUserOrThrow(userId);
Post post = findPostOrThrow(scrapRequestDto.postId());
validateDuplicateScrap(post.getId(), user.getId());
createScrapAndSave(user, post);
}

Expand All @@ -74,6 +77,15 @@ private void createScrapAndSave(User user, Post post) {
scrapRepository.save(scrap);
}

private void validateDuplicateScrap(Long postId, Long userId) {
if(existPostInScrap(postId, userId))
throw new ConflictException(DUPLICATE_SCRAP);
}

private boolean existPostInScrap(Long postId, Long userId) {
return scrapRepository.existsByPostIdAndUserId(postId, userId);
}

private CheckList getCheckList(Long userId) {
return checkListRepository.findByUserId(userId)
.orElseThrow(() -> new EntityNotFoundException(CHECK_LIST_NOT_FOUND));
Expand Down

0 comments on commit b8404c5

Please sign in to comment.