Skip to content

Commit

Permalink
Merge pull request #4 from f-lab-edu/feature/1
Browse files Browse the repository at this point in the history
[#1] 대출 기간 객체 분리
  • Loading branch information
shine-17 authored Dec 9, 2024
2 parents 1e041d8 + a0678ce commit 5f5dea7
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 24 deletions.
24 changes: 8 additions & 16 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies {
developmentOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
implementation 'org.springframework.boot:spring-boot-starter-validation'

// https://mvnrepository.com/artifact/com.google.code.gson/gson
implementation group: 'com.google.code.gson', name: 'gson', version: '2.10.1'
Expand All @@ -34,22 +35,13 @@ dependencies {

// mapstruct
// https://mvnrepository.com/artifact/org.mapstruct/mapstruct-processor
// annotationProcessor("org.mapstruct:mapstruct-processor:1.5.3.Final")
// // https://mvnrepository.com/artifact/org.mapstruct/mapstruct-processor
// testAnnotationProcessor("org.mapstruct:mapstruct-processor:1.5.3.Final")
// // https://mvnrepository.com/artifact/org.mapstruct/mapstruct
// implementation("org.mapstruct:mapstruct:1.5.3.Final")
// // https://mvnrepository.com/artifact/org.projectlombok/lombok-mapstruct-binding
// implementation("org.projectlombok:lombok-mapstruct-binding:0.2.0")

// implementation 'org.mapstruct:mapstruct:1.5.5.Final'
// annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.5.Final'
// implementation 'org.mapstruct:mapstruct-jdk8:1.5.5.Final'
// implementation 'org.mapstruct:mapstruct-spring-extensions:1.5.5.Final'


implementation 'org.mapstruct:mapstruct:1.5.5.Final'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.5.Final'
annotationProcessor("org.mapstruct:mapstruct-processor:1.5.3.Final")
// https://mvnrepository.com/artifact/org.mapstruct/mapstruct-processor
testAnnotationProcessor("org.mapstruct:mapstruct-processor:1.5.3.Final")
// https://mvnrepository.com/artifact/org.mapstruct/mapstruct
implementation("org.mapstruct:mapstruct:1.5.3.Final")
// https://mvnrepository.com/artifact/org.projectlombok/lombok-mapstruct-binding
implementation("org.projectlombok:lombok-mapstruct-binding:0.2.0")



Expand Down
18 changes: 10 additions & 8 deletions src/main/java/com/study/bookcafe/domain/Borrow.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.study.bookcafe.domain;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import java.sql.Timestamp;
import com.study.bookcafe.vo.Period;
import lombok.*;

import java.time.LocalDateTime;

@Builder
@Getter
Expand All @@ -14,12 +13,15 @@ public class Borrow {
private long id; // 대출 ID
private Member member; // 회원
private Book book; // 도서
private Timestamp borrowDate; // 대출 날짜
private Timestamp returnDate; // 반납 날짜
// private Timestamp borrowDate; // 대출 날짜
// private Timestamp returnDate; // 반납 날짜

private Period period; // 대출 기간

public Borrow(Member member, Book book) {
public Borrow(Member member, Book book, LocalDateTime from) {
this.member = member;
this.book = book;
this.period = new Period(from);
}

/**
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/study/bookcafe/vo/Period.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.study.bookcafe.vo;

import lombok.EqualsAndHashCode;
import lombok.NonNull;
import java.time.LocalDateTime;

@EqualsAndHashCode
public class Period {
/*
- 대출 일자가 반납 일자보다 더 이전인 것을 보장할 책임
- 대출 기간(대출 일자 ~ 반납 일자)을 생성할 책임
- 연장 여부에 따라 반납 일자가 변경
- 기본: 1주, 연장 시: 1주 추가, 총 2주
*/

private final LocalDateTime from; // 대출 일자
private final LocalDateTime to; // 반납 일자

public Period(@NonNull LocalDateTime from) {
this.from = from;
this.to = this.from.plusWeeks(1);
}

public Period(@NonNull LocalDateTime from, @NonNull LocalDateTime to) {
if(from.isAfter(to)) throw new IllegalArgumentException("반납 일자는 대출 일자보다 이후여야 합니다.");

this.from = from;
this.to = to;
}
}
32 changes: 32 additions & 0 deletions src/test/java/com/study/bookcafe/borrow/PeriodTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.study.bookcafe.borrow;

import com.study.bookcafe.vo.Period;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.time.LocalDateTime;

import static org.assertj.core.api.Assertions.*;

public class PeriodTest {

LocalDateTime date1 = LocalDateTime.of(2024, 12, 7, 12, 0, 0);
LocalDateTime date2 = LocalDateTime.of(2024, 12, 8, 12, 0, 0);

@Test
@DisplayName("반납일자는 대출일자보다 이후여야 한다.")
public void createPeriodTest() {
Period period1 = new Period(date1, date2);
Period period2 = new Period(date2, date2);

assertThat(period1).isNotNull();
assertThat(period2).isNotNull();
}

@Test
@DisplayName("반납일자가 대출일자보다 이전이거나 반납일자 또는 대출일자가 null이면 예외가 발생한다.")
public void createPeriodExceptionTest() {
Assertions.assertThrows(IllegalArgumentException.class, () -> new Period(date2, date1));
Assertions.assertThrows(NullPointerException.class, () -> new Period(null, null));
}
}

0 comments on commit 5f5dea7

Please sign in to comment.