diff --git a/src/main/java/org/swmaestro/repl/gifthub/giftcard/entity/Giftcard.java b/src/main/java/org/swmaestro/repl/gifthub/giftcard/entity/Giftcard.java index 7839656e..3c972ef3 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/giftcard/entity/Giftcard.java +++ b/src/main/java/org/swmaestro/repl/gifthub/giftcard/entity/Giftcard.java @@ -44,6 +44,9 @@ public class Giftcard { @Column(length = 60, nullable = false) private String password; + @Column + private Integer invalidPasswordCount; + @Builder public Giftcard(String id, Voucher voucher, String message, LocalDateTime expiresAt, String password) { this.id = id; @@ -51,9 +54,26 @@ public Giftcard(String id, Voucher voucher, String message, LocalDateTime expire this.message = message; this.password = password; this.expiresAt = expiresAt; + this.invalidPasswordCount = 0; } - public void expire() { + public Giftcard expire() { this.expiresAt = LocalDateTime.now(); + return this; + } + + public Giftcard increaseInvalidPasswordCount() { + this.invalidPasswordCount++; + return this; + + } + + public Giftcard resetInvalidPasswordCount() { + this.invalidPasswordCount = 0; + return this; + } + + public boolean isEnable() { + return this.expiresAt.isAfter(LocalDateTime.now()); } } diff --git a/src/main/java/org/swmaestro/repl/gifthub/giftcard/service/GiftcardService.java b/src/main/java/org/swmaestro/repl/gifthub/giftcard/service/GiftcardService.java index 8f2b369d..ae7f8857 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/giftcard/service/GiftcardService.java +++ b/src/main/java/org/swmaestro/repl/gifthub/giftcard/service/GiftcardService.java @@ -78,14 +78,21 @@ public Giftcard read(String id) { public GiftcardResponseDto read(String id, String password) { Giftcard giftcard = read(id); - if (giftcard.getExpiresAt().isBefore(LocalDateTime.now())) { - throw new BusinessException("만료된 링크입니다.", StatusEnum.BAD_REQUEST); + if (!giftcard.isEnable()) { + throw new BusinessException("기간이 만료된 링크입니다.", StatusEnum.BAD_REQUEST); + } + + if (giftcard.getInvalidPasswordCount() >= 10) { + throw new BusinessException("비밀번호를 10회 이상 틀렸습니다. 이 링크는 더 이상 사용할 수 없습니다.", StatusEnum.FORBIDDEN); } if (!decryptPassword(giftcard.getPassword()).equals(password)) { + save(giftcard.increaseInvalidPasswordCount()); throw new BusinessException("비밀번호가 일치하지 않습니다.", StatusEnum.FORBIDDEN); } + save(giftcard.resetInvalidPasswordCount()); + return GiftcardResponseDto.builder() .sender(giftcard.getVoucher().getUser().getNickname()) .message(giftcard.getMessage()) @@ -95,6 +102,10 @@ public GiftcardResponseDto read(String id, String password) { .build(); } + public Giftcard save(Giftcard giftcard) { + return giftCardRepository.save(giftcard); + } + /** * 기프트 카드의 소유자를 변경합니다. * @param giftcardId: 변경할 기프트 카드의 id @@ -112,9 +123,7 @@ public Giftcard changeVoucherUser(String giftcardId, String username) { User newUser = userService.read(username); updatedVoucher.setUser(newUser); voucherRepository.save(updatedVoucher); - - giftcard.expire(); - giftCardRepository.save(giftcard); + giftCardRepository.save(giftcard.expire()); return giftcard; }