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

[#1] repository에서 도메인 모델 분리 #5

Merged
merged 5 commits into from
Dec 11, 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
6 changes: 1 addition & 5 deletions src/main/java/com/study/bookcafe/domain/Borrow.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.study.bookcafe.vo.Period;
import lombok.*;

import java.time.LocalDateTime;

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

private Period period; // 대출 기간

public Borrow(Member member, Book book, LocalDateTime from) {
public Borrow(@NonNull Member member, @NonNull Book book, LocalDateTime from) {
this.member = member;
this.book = book;
this.period = new Period(from);
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/com/study/bookcafe/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import lombok.Builder;
import lombok.Getter;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -16,8 +15,8 @@ public class Member {
private Level level; // 회원 등급
private int borrowCount; // 현재 대출 권수

private LocalDate createDate; // 회원 가입 일자
private LocalDate updateDate; // 회원 수정 일자
private LocalDateTime createDate; // 회원 가입 일자
private LocalDateTime updateDate; // 회원 수정 일자

/**
* 회원이 대출 가능한 상태인지 알려준다.
Expand All @@ -39,7 +38,7 @@ public List<Borrow> borrowBook(List<Book> bookList) {
// 대출 가능한 도서만 목록에 담기
return bookList.stream()
.filter(Book::canBorrow)
.map(book -> new Borrow(this, book))
.map(book -> new Borrow(this, book, LocalDateTime.now()))
.toList();
}
}
11 changes: 4 additions & 7 deletions src/main/java/com/study/bookcafe/dto/BorrowDto.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.study.bookcafe.dto;

import com.study.bookcafe.vo.Period;
import lombok.*;

import java.sql.Timestamp;
import java.time.LocalDateTime;

@Builder
Expand All @@ -14,13 +13,11 @@ public class BorrowDto {
private long id; // 대출 ID
private MemberDto member; // 회원
private BookDto book; // 도서
private Timestamp borrowDate; // 대출 날짜
private Timestamp returnDate; // 반납 날짜
private Period period; // 대출 기간

public BorrowDto(MemberDto member, BookDto book) {
public BorrowDto(MemberDto member, BookDto book, LocalDateTime from) {
this.member = member;
this.book = book;
this.borrowDate = Timestamp.valueOf(LocalDateTime.now());
this.returnDate = Timestamp.valueOf(LocalDateTime.now().plusWeeks(1));
this.period = new Period(from);
}
}
9 changes: 5 additions & 4 deletions src/main/java/com/study/bookcafe/entity/BorrowEntity.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package com.study.bookcafe.entity;

import com.study.bookcafe.vo.Period;
import lombok.Builder;
import lombok.Getter;

import java.sql.Timestamp;
import lombok.NonNull;

@Builder
@Getter
public class BorrowEntity {
private long id; // 대출 ID
@NonNull
private MemberEntity member; // 회원
@NonNull
private BookEntity book; // 도서
private Timestamp borrowDate; // 대출 날짜
private Timestamp returnDate; // 반납 날짜
private Period period; // 대출 기간
}
20 changes: 12 additions & 8 deletions src/main/java/com/study/bookcafe/mapper/BorrowMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,39 @@ public interface BorrowMapper {
@Mapping(target = "id", source = "id")
@Mapping(target = "member", source = "member", qualifiedByName = {"MemberMapper", "MemberToMemberDto"})
@Mapping(target = "book", source = "book", qualifiedByName = {"BookMapper", "BookToBookDto"})
@Mapping(target = "borrowDate", source = "borrowDate")
@Mapping(target = "returnDate", source = "returnDate")
// @Mapping(target = "borrowDate", source = "borrowDate")
// @Mapping(target = "returnDate", source = "returnDate")
@Mapping(target = "period", source = "period")
// Borrow -> BorrowDto
BorrowDto toBorrowDto(Borrow borrow);

@Name("BorrowDtoToBorrow")
@Mapping(target = "id", source = "id")
@Mapping(target = "member", source = "member", qualifiedByName = {"MemberMapper", "MemberDtoToMember"})
@Mapping(target = "book", source = "book", qualifiedByName = {"BookMapper", "BookDtoToBook"})
@Mapping(target = "borrowDate", source = "borrowDate")
@Mapping(target = "returnDate", source = "returnDate")
// @Mapping(target = "borrowDate", source = "borrowDate")
// @Mapping(target = "returnDate", source = "returnDate")
@Mapping(target = "period", source = "period")
// BorrowDto -> Borrow
Borrow toBorrow(BorrowDto borrowDto);

@Named("BorrowToBorrowEntity")
@Mapping(target = "id", source = "id")
@Mapping(target = "member", source = "member", qualifiedByName = {"MemberMapper", "MemberToMemberEntity"})
@Mapping(target = "book", source = "book", qualifiedByName = {"BookMapper", "BookToBookEntity"})
@Mapping(target = "borrowDate", source = "borrowDate")
@Mapping(target = "returnDate", source = "returnDate")
// @Mapping(target = "borrowDate", source = "borrowDate")
// @Mapping(target = "returnDate", source = "returnDate")
@Mapping(target = "period", source = "period")
// Borrow -> BorrowEntity
BorrowEntity toBorrowEntity(Borrow borrow);

@Named("BorrowEntityToBorrow")
@Mapping(target = "id", source = "id")
@Mapping(target = "member", source = "member", qualifiedByName = {"MemberMapper", "MemberEntityToMember"})
@Mapping(target = "book", source = "book", qualifiedByName = {"BookMapper", "BookEntityToBook"})
@Mapping(target = "borrowDate", source = "borrowDate")
@Mapping(target = "returnDate", source = "returnDate")
// @Mapping(target = "borrowDate", source = "borrowDate")
// @Mapping(target = "returnDate", source = "returnDate")
@Mapping(target = "period", source = "period")
// BorrowEntity -> Borrow
Borrow toBorrow(BorrowEntity borrowEntity);

Expand Down
25 changes: 21 additions & 4 deletions src/test/java/com/study/bookcafe/borrow/MapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@
import com.study.bookcafe.entity.BorrowEntity;
import com.study.bookcafe.entity.MemberEntity;
import com.study.bookcafe.mapper.BorrowMapper;
import com.study.bookcafe.vo.Period;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mapstruct.factory.Mappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.LocalDateTime;

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

@SpringBootTest
public class MapperTest {
@Autowired
Expand All @@ -30,42 +35,54 @@ public class MapperTest {
public void checkMapperBorrowToBorrowDto() {
Borrow borrow = Borrow.builder()
.member(Member.builder().id(1).build())
.book(Book.builder().id(1).ISBN(9788936433598L).build()).build();
.book(Book.builder().id(1).ISBN(9788936433598L).build())
.period(new Period(LocalDateTime.now()))
.build();
BorrowDto borrowDto = borrowMapper.toBorrowDto(borrow);

System.out.println(gson.toJson(borrowDto));
assertThat(borrowDto).isNotNull();
}

@Test
@DisplayName("Mapper 테스트 : BorrowDto -> Borrow")
public void checkMapperBorrowDtoToBorrow() {
BorrowDto borrowDto = BorrowDto.builder()
.member(MemberDto.builder().id(1).build())
.book(BookDto.builder().id(1).ISBN(9788936433598L).build()).build();
.book(BookDto.builder().id(1).ISBN(9788936433598L).build())
.period(new Period(LocalDateTime.now()))
Copy link
Collaborator

Choose a reason for hiding this comment

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

혹시 Factory 패턴을 아시나요?

.build();
Borrow borrow = borrowMapper.toBorrow(borrowDto);

System.out.println(gson.toJson(borrow));
assertThat(borrow).isNotNull();
}

@Test
@DisplayName("Mapper 테스트 : Borrow -> BorrowEntity")
public void checkMapperBorrowToBorrowEntity() {
Borrow borrow = Borrow.builder()
.member(Member.builder().id(1).build())
.book(Book.builder().id(1).ISBN(9788936433598L).build()).build();
.book(Book.builder().id(1).ISBN(9788936433598L).build())
.period(new Period(LocalDateTime.now()))
.build();
BorrowEntity borrowEntity = borrowMapper.toBorrowEntity(borrow);

System.out.println(gson.toJson(borrowEntity));
assertThat(borrowEntity).isNotNull();
}

@Test
@DisplayName("Mapper 테스트 : BorrowEntity -> Borrow")
public void checkMapperBorrowEntityToBorrow() {
BorrowEntity borrowEntity = BorrowEntity.builder()
.member(MemberEntity.builder().id(1).build())
.book(BookEntity.builder().id(1).ISBN(9788936433598L).build()).build();
.book(BookEntity.builder().id(1).ISBN(9788936433598L).build())
.period(new Period(LocalDateTime.now()))
.build();
Borrow borrow = borrowMapper.toBorrow(borrowEntity);

System.out.println(gson.toJson(borrow));
assertThat(borrow).isNotNull();
}
}
Loading