Skip to content

Commit

Permalink
Handle error IncorrectResultSizeDataAccessException (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
inh2613 authored Nov 8, 2023
2 parents fc1e9f6 + f6307d4 commit bf42d02
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.swmaestro.repl.gifthub.giftcard.repository;

import java.util.Optional;
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.swmaestro.repl.gifthub.giftcard.entity.Giftcard;

public interface GiftcardRepository extends JpaRepository<Giftcard, String> {
boolean existsByVoucherId(Long id);

Optional<Giftcard> findAllByVoucherId(Long id);
List<Giftcard> findAllByVoucherId(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;
import java.util.UUID;

Expand Down Expand Up @@ -76,7 +77,9 @@ public Giftcard read(String id) {

public Giftcard read(Long voucherId) {
return giftCardRepository.findAllByVoucherId(voucherId)
.stream()
.filter(giftcard -> giftcard.getExpiresAt().isAfter(LocalDateTime.now()))
.findFirst()
.orElseThrow(() -> new BusinessException("존재하지 않는 기프트 카드입니다.", StatusEnum.NOT_FOUND));
}

Expand Down Expand Up @@ -154,9 +157,11 @@ public boolean isExist(String id) {
*/
public boolean isExist(Long voucherId) {
if (giftCardRepository.existsByVoucherId(voucherId)) {
if (giftCardRepository.findAllByVoucherId(voucherId).get().getExpiresAt().isAfter(LocalDateTime.now())) {
return true;
}
List<Giftcard> giftCards = giftCardRepository.findAllByVoucherId(voucherId);

return giftCards.stream()
.anyMatch(giftcard -> giftcard.getExpiresAt().isAfter(LocalDateTime.now()));

}
return false;
}
Expand Down

0 comments on commit bf42d02

Please sign in to comment.