Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

도서 대출 연장 기능 추가 및 테스트 #14

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
15 changes: 5 additions & 10 deletions src/main/java/com/study/bookcafe/domain/borrow/Borrow.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.study.bookcafe.domain.book.Book;
import com.study.bookcafe.domain.member.Member;
import lombok.*;
import java.time.LocalDate;

import java.time.LocalDateTime;

@Builder
Expand Down Expand Up @@ -60,7 +60,7 @@ public boolean haveExtendableCount() {
public void extend() {
if (!canExtend()) return;

Period extendedPeriod = this.getPeriod().getExtended(this.getMember().getLevel());
Period extendedPeriod = this.getPeriod().createExtended(this.getMember().getLevel());

extendPeriod(extendedPeriod);
increaseExtendCount();
Expand All @@ -74,13 +74,8 @@ public void extend() {
*
* @return 대출 연장 가능한지 여부
*/
public boolean isPassHalfofPeriod() {
LocalDate now = LocalDate.now();

int halfPeriod = this.getMember().getLevel().getBorrowPeriod() * 7 / 2;
LocalDate targetDate = this.getPeriod().getFrom().plusDays(halfPeriod);

return now.isEqual(targetDate) || now.isAfter(targetDate);
public boolean isExtendableDate() {
return this.getPeriod().isExtendable();
}

private boolean canExtend() {
Expand All @@ -91,7 +86,7 @@ private boolean canExtend() {
if (this.getBook().haveReservation()) return false;

// 대출 연장이 가능한 날짜가 아니므로 불가
if (!isPassHalfofPeriod()) return false;
if (!isExtendableDate()) return false;

return true;
}
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/com/study/bookcafe/domain/borrow/Period.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

import com.study.bookcafe.domain.member.Level;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import java.time.LocalDate;

@EqualsAndHashCode
@Getter
public class Period {
/*
- 대출 일자가 반납 일자보다 더 이전인 것을 보장할 책임
Expand Down Expand Up @@ -35,8 +33,16 @@ public static Period of(@NonNull LocalDate from, Level level) {
return new Period(from, level);
}

public Period getExtended(Level level) {
return new Period(from, this.to.plusWeeks(level.getExtendPeriod()));
public Period createExtended(Level level) {
return new Period(from, to.plusWeeks(level.getExtendPeriod()));
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • createExtended
  • addExtension
  • withExtension

이렇게 고민했었는데 매개변수를 받아서 연장된 Period 인스턴스를 새로 만든다는 의미에 createExtended 가 가장 적절하다고 생각해서 작성했습니다.

토모님은 이 메서드명을 어떻게 지으실지 궁금합니다 ㅎㅎ

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저라면.. 그냥 extend?

Period 의 역할이니 한번 읽어보면 period create extended 랑 period extend 어느쪽 문장이 편한가요?

혹은 도메인 한정적이어도 된다면 renew 도 좋겠네요 ㅎㅎ

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Period 의 역할이니 한번 읽어보면 period create extended 랑 period extend 어느쪽 문장이 편한가요?

오.. 이렇게 읽어보니 period extend가 더 편합니다.
저는 기간 연장된 객체를 반환한다 라고 생각해서 extend()는 command 성격이라 배제를 했었습니다.. ㅎㅎ

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그래서 get 을 붙이셨군요 ㅎㅎ

예를들어, LocalDate#plusWeeks(int) 메서드는 어떻게 생각하시나요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

불변객체일 경우는 다르군요..! LocalDate.plusWeeks() 같은 경우는 LocalDate가 불변객체이기 때문에 상태 값을 변경하려면 새로운 인스턴스를 만드는 방법밖에 없어서 저렇게 사용한 것 같습니다. 그렇다면 Period도 같은 경우겠네요!


public boolean isExtendable() {
long epochDay = (from.toEpochDay() + to.toEpochDay()) / 2;
LocalDate targetDate = LocalDate.ofEpochDay(epochDay);
LocalDate now = LocalDate.now();

return targetDate.isEqual(now) || targetDate.isBefore(now);
}

}
2 changes: 1 addition & 1 deletion src/main/java/com/study/bookcafe/domain/member/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
public enum Level {

LIBRARIAN(2, 10,null, 1, 2, 1), // 사서 회원
WORM(1, 5,null, 1, 1, 1), // 책벌레 회원
WORM(1, 5,null, 1, 2, 1), // 책벌레 회원
BASIC(0, 3, WORM, 1, 1, 1); // 일반 회원

private final int value; // 등급 값
Expand Down
22 changes: 19 additions & 3 deletions src/test/java/com/study/bookcafe/borrow/PeriodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public void ofExceptionTest() {
public void checkBorrowExtensionsPeriodByMemberShipLevel() {
LocalDate borrowDate = LocalDate.now();

Period basicExtendedPeriod = Period.of(borrowDate, Level.BASIC).getExtended(Level.BASIC);
Period wormExtendedPeriod = Period.of(borrowDate, Level.WORM).getExtended(Level.WORM);
Period librarianExtendedPeriod = Period.of(borrowDate, Level.LIBRARIAN).getExtended(Level.LIBRARIAN);
Period basicExtendedPeriod = Period.of(borrowDate, Level.BASIC).createExtended(Level.BASIC);
Period wormExtendedPeriod = Period.of(borrowDate, Level.WORM).createExtended(Level.WORM);
Period librarianExtendedPeriod = Period.of(borrowDate, Level.LIBRARIAN).createExtended(Level.LIBRARIAN);

Period newBasicPeriod = new Period(borrowDate, borrowDate.plusWeeks(Level.BASIC.getBorrowPeriod()).plusWeeks(Level.BASIC.getExtendPeriod()));
Period newWormPeriod = new Period(borrowDate, borrowDate.plusWeeks(Level.WORM.getBorrowPeriod()).plusWeeks(Level.WORM.getExtendPeriod()));
Expand All @@ -49,4 +49,20 @@ public void checkBorrowExtensionsPeriodByMemberShipLevel() {
assertThat(librarianExtendedPeriod).isEqualTo(newLibrarianPeriod);

}

@Test
@DisplayName("대출 연장은 대출 기간의 50%가 경과해야 가능하다.")
public void checkPassHalfOfBorrowPeriod() {
LocalDate now = LocalDate.now();

Period period1 = Period.of(now.minusDays(3), Level.BASIC);
Period period2 = Period.of(now.minusDays(2), Level.BASIC);
Period period3 = Period.of(now.minusDays(7), Level.WORM);
Period period4 = Period.of(now.minusDays(6), Level.WORM);

assertThat(period1.isExtendable()).isTrue();
assertThat(period2.isExtendable()).isFalse();
assertThat(period3.isExtendable()).isTrue();
assertThat(period4.isExtendable()).isFalse();
}
}
15 changes: 9 additions & 6 deletions src/test/java/com/study/bookcafe/member/BorrowTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import com.study.bookcafe.application.command.member.MemberService;
import com.study.bookcafe.application.query.member.MemberQueryService;
import com.study.bookcafe.domain.borrow.Borrow;
import com.study.bookcafe.domain.borrow.Period;
import com.study.bookcafe.infrastructure.query.borrow.TestBorrowQueryStorage;
import com.study.bookcafe.interfaces.borrow.BorrowDto;
import com.study.bookcafe.query.borrow.BorrowDetails;
import com.study.bookcafe.interfaces.common.JsonHelper;
import org.junit.jupiter.api.DisplayName;
Expand Down Expand Up @@ -60,19 +60,22 @@ public void testFindBorrows() {
@DisplayName("회원이 도서 대출을 연장한다.")
public void extendBorrow() {

// before
LocalDate targetBefore = LocalDate.now().minusDays(3);
LocalDate targetAfter = targetBefore.plusWeeks(2);
Period before = new Period(targetBefore, targetAfter);

// after
long memberId = 1L;
long bookId = 1L;

Optional<Borrow> targetBorrow = borrowService.findBorrowByMemberIdAndBookId(memberId, bookId, true);
Borrow borrow = targetBorrow.orElseThrow();
LocalDate before = borrow.getPeriod().getTo();

// extend borrow
memberService.extendBook(memberId, bookId);
Period after = TestBorrowQueryStorage.borrowDtos.get(borrow.getId()).getPeriod();

BorrowDto extendedBorrow = TestBorrowQueryStorage.borrowDtos.get(borrow.getId());
LocalDate after = extendedBorrow.getPeriod().getTo();

assertThat(before.plusWeeks(1)).isEqualTo(after);
assertThat(before).isEqualTo(after);
}
}
Loading