Skip to content

[#1] 대출 기간 객체 분리 #4

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

Merged
merged 2 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
42 changes: 42 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,42 @@
package com.study.bookcafe.vo;

import lombok.NonNull;
import java.time.LocalDateTime;
import java.util.Objects;

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;
}

@Override
public boolean equals(Object o) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

롬복을 쓰지 않았던가요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

롬복으로 대체할 수 있는 어노테이션을 처음 알아서 적용했습니다 ㅎㅎ

if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Period period = (Period) o;
return Objects.equals(from, period.from) && Objects.equals(to, period.to);
}

@Override
public int hashCode() {
return Objects.hash(from, 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));
}
}
Loading