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

메서드의 파라미터 타입을 추상화 타입으로 변경 #6

Merged
merged 1 commit into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion src/main/java/com/study/bookcafe/dao/BookRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import com.study.bookcafe.domain.Book;

import java.util.Collection;
import java.util.List;

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

import com.study.bookcafe.domain.Borrow;

import java.util.Collection;
import java.util.List;

public interface BorrowRepository {

Borrow findById(long borrowId);
Borrow save(Borrow borrow);
List<Borrow> save(List<Borrow> borrowList);
List<Borrow> save(Collection<Borrow> borrows);

}
26 changes: 19 additions & 7 deletions src/main/java/com/study/bookcafe/dao/TestBookRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,43 @@

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

import java.sql.Date;
import java.time.LocalDate;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class TestBookRepository implements BookRepository {
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)))

private final BookMapper bookMapper;

public TestBookRepository(BookMapper bookMapper) {
this.bookMapper = bookMapper;
}

public static Map<Long, BookEntity> books = new HashMap<>(){{
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.

이전 방식이 books의 타입이 <Long, Book> 이었던 코드를 말씀하시는 건가요?! 테스트 객체지만 추후에 DB와는 Entity로 주고받기 때문에 미리 느낌을 보려고 변경해봤습니다 ㅎㅎ

put(1L, BookEntity.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, Book.builder().id(2L).ISBN(9788954651134L).title("흰").author("한강").publisher("문학동네").publishDate(Date.valueOf(LocalDate.of(2018, 4, 25)))
put(2L, BookEntity.builder().id(2L).ISBN(9788954651134L).title("흰").author("한강").publisher("문학동네").publishDate(Date.valueOf(LocalDate.of(2018, 4, 25)))
.price(13000).inventory(new Inventory(0)).build());
}};

public Book findById(long bookId) {
return books.get(bookId);
BookEntity bookEntity = books.get(bookId);
return bookMapper.toBook(bookEntity);
}

@Override
public List<Book> findByIdList(List<Long> bookIdList) {
return bookIdList
.stream().filter(id -> books.containsKey(id))
public List<Book> findByIdList(Collection<Long> bookIds) {
List<BookEntity> bookEntities = bookIds.stream()
.filter(id -> books.containsKey(id))
.map(id -> books.get(id)).toList();
return bookMapper.toBookList(bookEntities);
}
}
43 changes: 28 additions & 15 deletions src/main/java/com/study/bookcafe/dao/TestBorrowRepository.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,51 @@
package com.study.bookcafe.dao;

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

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class TestBorrowRepository implements BorrowRepository {
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());

private final BorrowMapper borrowMapper;

public TestBorrowRepository(BorrowMapper borrowMapper) {
this.borrowMapper = borrowMapper;
}

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());
put(3L, null);
}};

public Borrow findById(long borrowId) {
return borrows.get(borrowId);
BorrowEntity borrowEntity = borrows.get(borrowId);
return borrowMapper.toBorrow(borrowEntity);
}

public Borrow save(Borrow borrow) {
return borrow;
BorrowEntity borrowEntity = borrows.get(borrow.getId());
return borrowMapper.toBorrow(borrowEntity);
}

@Override
public List<Borrow> save(List<Borrow> borrowList) {
return borrowList
.stream().filter(borrow -> borrows.containsKey(borrow.getId()))
.map(borrow -> borrows.get(borrow.getId())).toList();
public List<Borrow> save(Collection<Borrow> borrows) {
List<BorrowEntity> borrowEntities = borrows
.stream().filter(borrow -> this.borrows.containsKey(borrow.getId()))
.map(borrow -> this.borrows.get(borrow.getId())).toList();
return borrowMapper.toBorrowList(borrowEntities);
}
}
3 changes: 2 additions & 1 deletion src/main/java/com/study/bookcafe/service/BookService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import com.study.bookcafe.domain.Book;

import java.util.Collection;
import java.util.List;

public interface BookService {
// 도서 조회 (id)
Book findById(long bookId);

// 도서 목록 조회 (id list)
List<Book> findByIdList(List<Long> bookIdList);
List<Book> findByIdList(Collection<Long> bookIds);
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.

코드를 변경하면서 메서드명을 보고도 아무생각없이 지나쳤던 제 자신을 반성합니다... 😥

}
12 changes: 5 additions & 7 deletions src/main/java/com/study/bookcafe/service/BookServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@

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

import java.util.Collection;
import java.util.List;

@Service
public class BookServiceImpl implements BookService {

private final BookRepository bookRepository;
private final BookMapper bookMapper;

public BookServiceImpl(BookRepository bookRepository, BookMapper bookMapper) {
public BookServiceImpl(BookRepository bookRepository) {
this.bookRepository = bookRepository;
this.bookMapper = bookMapper;
}

/**
Expand All @@ -32,11 +30,11 @@ public Book findById(long bookId) {
/**
* 도서를 ID 목록으로 조회한다.
*
* @param bookIdList 도서 ID 목록
* @param bookIds 도서 ID 목록
* @return 도서 목록
*/
@Override
public List<Book> findByIdList(List<Long> bookIdList) {
return bookRepository.findByIdList(bookIdList);
public List<Book> findByIdList(Collection<Long> bookIds) {
return bookRepository.findByIdList(bookIds);
}
}
5 changes: 3 additions & 2 deletions src/main/java/com/study/bookcafe/service/BorrowService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import com.study.bookcafe.domain.Borrow;

import java.util.Collection;
import java.util.List;

public interface BorrowService {
// 대출 저장
Borrow save(Borrow borrow);

// 여러 대출 저장
List<Borrow> save(List<Borrow> borrowList);
}
List<Borrow> save(Collection<Borrow> borrows);
}
15 changes: 6 additions & 9 deletions src/main/java/com/study/bookcafe/service/BorrowServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@

import com.study.bookcafe.dao.BorrowRepository;
import com.study.bookcafe.domain.Borrow;
import com.study.bookcafe.entity.BorrowEntity;
import com.study.bookcafe.mapper.BorrowMapper;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

@Service
public class BorrowServiceImpl implements BorrowService {
private final BorrowRepository borrowRepository;
private final BorrowMapper borrowMapper;

public BorrowServiceImpl(BorrowRepository borrowRepository, BorrowMapper borrowMapper) {
public BorrowServiceImpl(BorrowRepository borrowRepository) {
this.borrowRepository = borrowRepository;
this.borrowMapper = borrowMapper;
}

/**
Expand All @@ -31,12 +28,12 @@ public Borrow save(Borrow borrow) {
/**
* 새로운 여러 대출들을 저장한다.
*
* @param borrowList 대출 목록
* @param borrows 대출 목록
* @return 생성한 대출 정보 목록
*/
@Override
public List<Borrow> save(List<Borrow> borrowList) {
List<BorrowEntity> borrowEntityList = borrowMapper.toBorrowEntityList(borrowList);
return borrowMapper.toBorrowList(borrowEntityList);
public List<Borrow> save(Collection<Borrow> borrows) {
return borrowRepository.save(borrows);
}

}
3 changes: 2 additions & 1 deletion src/main/java/com/study/bookcafe/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.study.bookcafe.domain.Borrow;
import com.study.bookcafe.domain.Member;

import java.util.Collection;
import java.util.List;

public interface MemberService {
Expand All @@ -11,5 +12,5 @@ public interface MemberService {
Member findById(long memberId);

// 도서 대출
List<Borrow> borrowBook(long memberId, List<Long> bookIdList);
List<Borrow> borrowBook(long memberId, Collection<Long> bookIds);
}
18 changes: 8 additions & 10 deletions src/main/java/com/study/bookcafe/service/MemberServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import com.study.bookcafe.domain.Book;
import com.study.bookcafe.domain.Borrow;
import com.study.bookcafe.domain.Member;
import com.study.bookcafe.mapper.MemberMapper;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

@Service
Expand All @@ -15,13 +15,11 @@ public class MemberServiceImpl implements MemberService {
private final MemberRepository memberRepository;
private final BorrowService borrowService;
private final BookService bookService;
private final MemberMapper memberMapper;

public MemberServiceImpl(MemberRepository memberRepository, BorrowService borrowService, BookService bookService, MemberMapper memberMapper) {
public MemberServiceImpl(MemberRepository memberRepository, BorrowService borrowService, BookService bookService) {
this.memberRepository = memberRepository;
this.borrowService = borrowService;
this.bookService = bookService;
this.memberMapper = memberMapper;
}

/**
Expand All @@ -39,17 +37,17 @@ public Member findById(long memberId) {
* 회원이 도서를 대출한다.
*
* @param memberId 회원 ID
* @param bookIdList 도서 ID 목록
* @param bookIds 도서 ID 목록
* @return 대출 정보
*/
@Override
public List<Borrow> borrowBook(long memberId, List<Long> bookIdList) {
public List<Borrow> borrowBook(long memberId, Collection<Long> bookIds) {
Member member = findById(memberId);
List<Book> bookList = bookService.findByIdList(bookIdList);
List<Borrow> borrowList = member.borrowBook(bookList);
List<Book> books = bookService.findByIdList(bookIds);
List<Borrow> borrows = member.borrowBook(books);

if(borrowList.isEmpty()) return borrowList;
if(borrows.isEmpty()) return borrows;

return borrowService.save(borrowList);
return borrowService.save(borrows);
}
}
Loading