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 4 commits
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: 3 additions & 3 deletions src/main/java/com/study/bookcafe/dao/BookRepository.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.study.bookcafe.dao;

import com.study.bookcafe.entity.BookEntity;
import com.study.bookcafe.domain.Book;

import java.util.List;

public interface BookRepository {
BookEntity findById(long bookId);
List<BookEntity> findByIdList(List<Long> bookIdList);
Book findById(long bookId);
List<Book> findByIdList(List<Long> bookIdList);
}
9 changes: 4 additions & 5 deletions src/main/java/com/study/bookcafe/dao/BorrowRepository.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.study.bookcafe.dao;

import com.study.bookcafe.entity.BorrowEntity;

import com.study.bookcafe.domain.Borrow;
import java.util.List;

public interface BorrowRepository {

BorrowEntity findById(long borrowId);
BorrowEntity save(BorrowEntity borrowEntity);
List<BorrowEntity> save(List<BorrowEntity> borrowEntityList);
Borrow findById(long borrowId);
Borrow save(Borrow borrow);
List<Borrow> save(List<Borrow> borrowList);
Copy link
Collaborator

Choose a reason for hiding this comment

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

파라미터 타입과 리턴 타입에 List 를 쓰는게 놓을까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

대출이 이뤄질 때 회원과 도서가 1:N 이라고 생각해 Member의 대출 로직에서 List를 리턴하게 했었습니다. 그 다음 대출 정보를 각각 하나씩 저장하는 것보다 List로 한 번에 저장하는 것이 성능면으로 더 좋다고 생각해서 파라미터로 List를 받게 했었습니다.

단점

  1. 회원과 도서가 1:1인 대출 정보를 저장할 때 List로 변환하여 전달해야 함.
  2. 각각의 대출정보에 대한 디버깅이나 예외처리가 어려울 수 있을 것 같음.

해결방법

1:N, 1:1 인 대출정보를 모두 받을 수 있는 메서드를 선언한다.

List<Borrow> save(List<Borrow> borrowList);
Borrow save(Borrow borrow);

-> 회원이 대출받을 수 있는 도서의 수는 제한되어있기 때문에 List로 받아서 저장해도 성능면에서 별 차이가 없을 것 같아 좋은 방법은 아닌 것 같습니다.

Copy link
Collaborator

@f-lab-tomo f-lab-tomo Dec 10, 2024

Choose a reason for hiding this comment

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

오호 그렇군요 혹시 List 대신 Collection 이나 Iterable 을 쓰는 건 어때요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

앗 그 생각은 못 해봤네요... List 대신 Collection을 사용함으로써 결합도를 낮출 수 있어서 더 좋은 선택인 것 같습니다.

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.

넵. 파라미터는 Collection 타입으로 받고, 리턴타입도 Collection 타입으로 리턴해서 호출한 클라이언트 쪽에서 원하는 Collection의 하위 타입으로 변환할 수 있게 하는 것이 좋을 것 같습니다.

Copy link
Collaborator

Choose a reason for hiding this comment

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

리턴 타입을 Collection 으로 하면 받는 곳에서 원하는 하위 타입으로 쓸 수 있을까요? 한 번 해보면 재밌을 것 같아요

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

호출한 쪽에서 Collection으로 받고 Stream을 이용해서 List나 Set 또는 Array로 변환해서 사용하면 조금 번거롭더라도 특정 구현체에 대해서 종속되지 않으니 괜찮다고 생각했습니다..!

Copy link
Collaborator

Choose a reason for hiding this comment

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

in out 에 대해 오늘 시간에 다뤄볼 수 있겠네요! ㅋㅋ


}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.study.bookcafe.dao;

import com.study.bookcafe.domain.Level;
import com.study.bookcafe.entity.MemberEntity;
import com.study.bookcafe.domain.Member;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
Expand All @@ -10,14 +10,14 @@
@Repository
public class GeneralMemberRepository implements MemberRepository {

Map<Long, MemberEntity> members = new HashMap<>(){{
put(1L, MemberEntity.builder().id(1L).name("슈카").level(Level.BASIC).borrowCount(0).build());
put(2L, MemberEntity.builder().id(2L).name("머스크").level(Level.WORM).borrowCount(3).build());
put(3L, MemberEntity.builder().id(3L).name("트럼프").level(Level.LIBRARIAN).borrowCount(5).build());
Map<Long, Member> members = new HashMap<>(){{
put(1L, Member.builder().id(1L).name("슈카").level(Level.BASIC).borrowCount(0).build());
put(2L, Member.builder().id(2L).name("머스크").level(Level.WORM).borrowCount(3).build());
put(3L, Member.builder().id(3L).name("트럼프").level(Level.LIBRARIAN).borrowCount(5).build());
}};

@Override
public MemberEntity findById(long memberId) {
public Member findById(long memberId) {
return members.get(memberId);
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/study/bookcafe/dao/MemberRepository.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.study.bookcafe.dao;

import com.study.bookcafe.entity.MemberEntity;
import com.study.bookcafe.domain.Member;

public interface MemberRepository {
MemberEntity findById(long memberId);
Member findById(long memberId);
}
12 changes: 6 additions & 6 deletions src/main/java/com/study/bookcafe/dao/TestBookRepository.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.study.bookcafe.dao;

import com.study.bookcafe.domain.Book;
import com.study.bookcafe.domain.Inventory;
import com.study.bookcafe.entity.BookEntity;
import org.springframework.stereotype.Repository;

import java.sql.Date;
Expand All @@ -12,19 +12,19 @@

@Repository
public class TestBookRepository implements BookRepository {
public static Map<Long, BookEntity> books = new HashMap<>(){{
put(1L, BookEntity.builder().id(1L).ISBN(9788936433598L).title("채식주의자").author("한강").publisher("창비").publishDate(Date.valueOf(LocalDate.of(2007, 10, 30)))
public static Map<Long, Book> books = new HashMap<>(){{
put(1L, Book.builder().id(1L).ISBN(9788936433598L).title("채식주의자").author("한강").publisher("창비").publishDate(Date.valueOf(LocalDate.of(2007, 10, 30)))
.price(35000).inventory(new Inventory(5)).build());
put(2L, BookEntity.builder().id(2L).ISBN(9788954651134L).title("흰").author("한강").publisher("문학동네").publishDate(Date.valueOf(LocalDate.of(2018, 4, 25)))
put(2L, Book.builder().id(2L).ISBN(9788954651134L).title("흰").author("한강").publisher("문학동네").publishDate(Date.valueOf(LocalDate.of(2018, 4, 25)))
.price(13000).inventory(new Inventory(0)).build());
}};

public BookEntity findById(long bookId) {
public Book findById(long bookId) {
return books.get(bookId);
}

@Override
public List<BookEntity> findByIdList(List<Long> bookIdList) {
public List<Book> findByIdList(List<Long> bookIdList) {
return bookIdList
.stream().filter(id -> books.containsKey(id))
.map(id -> books.get(id)).toList();
Expand Down
30 changes: 15 additions & 15 deletions src/main/java/com/study/bookcafe/dao/TestBorrowRepository.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.study.bookcafe.dao;

import com.study.bookcafe.entity.BookEntity;
import com.study.bookcafe.entity.BorrowEntity;
import com.study.bookcafe.entity.MemberEntity;
import com.study.bookcafe.domain.Book;
import com.study.bookcafe.domain.Borrow;
import com.study.bookcafe.domain.Member;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
Expand All @@ -11,27 +11,27 @@

@Repository
public class TestBorrowRepository implements BorrowRepository {
Map<Long, BorrowEntity> borrows = new HashMap<>(){{
put(1L, BorrowEntity.builder()
.member(MemberEntity.builder().id(1).build())
.book(BookEntity.builder().id(1).ISBN(9788936433598L).build()).build());
put(2L, BorrowEntity.builder()
.member(MemberEntity.builder().id(1).build())
.book(BookEntity.builder().id(2).ISBN(9788936433598L).build()).build());
Map<Long, Borrow> borrows = new HashMap<>(){{
put(1L, Borrow.builder()
.member(Member.builder().id(1).build())
.book(Book.builder().id(1).ISBN(9788936433598L).build()).build());
put(2L, Borrow.builder()
.member(Member.builder().id(1).build())
.book(Book.builder().id(2).ISBN(9788936433598L).build()).build());
put(3L, null);
}};

public BorrowEntity findById(long borrowId) {
public Borrow findById(long borrowId) {
return borrows.get(borrowId);
}

public BorrowEntity save(BorrowEntity borrowEntity) {
return borrowEntity;
public Borrow save(Borrow borrow) {
return borrow;
}

@Override
public List<BorrowEntity> save(List<BorrowEntity> borrowEntityList) {
return borrowEntityList
public List<Borrow> save(List<Borrow> borrowList) {
return borrowList
.stream().filter(borrow -> borrows.containsKey(borrow.getId()))
.map(borrow -> borrows.get(borrow.getId())).toList();
}
Expand Down
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
7 changes: 2 additions & 5 deletions src/main/java/com/study/bookcafe/service/BookServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.study.bookcafe.dao.BookRepository;
import com.study.bookcafe.domain.Book;
import com.study.bookcafe.entity.BookEntity;
import com.study.bookcafe.mapper.BookMapper;
import org.springframework.stereotype.Service;

Expand All @@ -27,8 +26,7 @@ public BookServiceImpl(BookRepository bookRepository, BookMapper bookMapper) {
*/
@Override
public Book findById(long bookId) {
BookEntity bookEntity = bookRepository.findById(bookId);
return bookMapper.toBook(bookEntity);
return bookRepository.findById(bookId);
}

/**
Expand All @@ -39,7 +37,6 @@ public Book findById(long bookId) {
*/
@Override
public List<Book> findByIdList(List<Long> bookIdList) {
List<BookEntity> BookEntityList = bookRepository.findByIdList(bookIdList);
return bookMapper.toBookList(BookEntityList);
return bookRepository.findByIdList(bookIdList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public BorrowServiceImpl(BorrowRepository borrowRepository, BorrowMapper borrowM
*/
@Override
public Borrow save(Borrow borrow) {
BorrowEntity borrowEntity = borrowRepository.save(borrowMapper.toBorrowEntity(borrow));
return borrowMapper.toBorrow(borrowEntity);
return borrowRepository.save(borrow);
}
/**
* 새로운 여러 대출들을 저장한다.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.study.bookcafe.domain.Book;
import com.study.bookcafe.domain.Borrow;
import com.study.bookcafe.domain.Member;
import com.study.bookcafe.entity.MemberEntity;
import com.study.bookcafe.mapper.MemberMapper;
import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -33,8 +32,7 @@ public MemberServiceImpl(MemberRepository memberRepository, BorrowService borrow
*/
@Override
public Member findById(long memberId) {
MemberEntity memberEntity = memberRepository.findById(memberId);
return memberMapper.toMember(memberEntity);
return memberRepository.findById(memberId);
}

/**
Expand Down
Loading
Loading