Skip to content

Commit

Permalink
Merge pull request #6 from f-lab-edu/feature/1
Browse files Browse the repository at this point in the history
메서드의 파라미터 타입을 추상화 타입으로 변경
  • Loading branch information
shine-17 authored Dec 15, 2024
2 parents 95ba708 + 51a0a77 commit d7e5d7c
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 54 deletions.
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<>(){{
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);
}
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);
}
}

0 comments on commit d7e5d7c

Please sign in to comment.