Skip to content

Commit

Permalink
refactor: Gift isUsed 검증 로직, 도메인 객체 멤버로 변경 #44
Browse files Browse the repository at this point in the history
  • Loading branch information
PgmJun committed Feb 8, 2024
1 parent 068049c commit 3258a28
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,8 @@ public GiftGenerateResponse generateGift(final Long memberId) {

public void useGift(final Long memberId, final String giftCode) {
Gift gift = GiftServiceUtils.findByGiftId(giftRepository, giftCode);
validateGiftIsNotUsed(gift);

gift.use(memberId);
log.info(String.format("[기프트 사용] 기프트 (GIFT_ID: %d)가 회원 (MEMBER_ID: %d)에 의해 사용되었습니다.", gift.getId(), memberId));
}

private void validateGiftIsNotUsed(Gift gift) {
if(gift.isUsed()) {
throw new ValidationException(ErrorCode.ALREADY_USED_GIFT_EXCEPTION,
String.format("이미 사용된 기프트 (GIFT_ID: %d) 입니다.", gift.getId()));
}
}
}
10 changes: 8 additions & 2 deletions src/main/java/com/nice/petudio/domain/gift/Gift.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.nice.petudio.domain.gift;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.nice.petudio.common.exception.error.ErrorCode;
import com.nice.petudio.common.exception.model.ValidationException;
import com.nice.petudio.domain.base.BaseEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
Expand Down Expand Up @@ -54,11 +56,15 @@ public static Gift newInstance(Long memberId, String code) {
}

public void use(final Long userId) {
validateNotUsed();
this.userId = userId;
isUsed = true;
}

public boolean isUsed() {
return isUsed;
private void validateNotUsed() {
if(this.isUsed) {
throw new ValidationException(ErrorCode.ALREADY_USED_GIFT_EXCEPTION,
String.format("이미 사용된 기프트 (GIFT_ID: %d) 입니다.", this.id));
}
}
}

0 comments on commit 3258a28

Please sign in to comment.