Skip to content

Commit

Permalink
fix: handle error non-unique-result-exception
Browse files Browse the repository at this point in the history
  • Loading branch information
inh2613 committed Nov 8, 2023
1 parent fc1e9f6 commit 0e8d354
Show file tree
Hide file tree
Showing 2 changed files with 13 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,14 @@ 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;
}
// 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 0e8d354

Please sign in to comment.