From 51a0a7779ad02189a054c2ca5b168087feaab8c2 Mon Sep 17 00:00:00 2001 From: dohun Date: Sun, 15 Dec 2024 23:36:40 +0900 Subject: [PATCH] =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=EC=9D=98=20?= =?UTF-8?q?=ED=8C=8C=EB=9D=BC=EB=AF=B8=ED=84=B0=20=ED=83=80=EC=9E=85?= =?UTF-8?q?=EC=9D=84=20=EC=B6=94=EC=83=81=ED=99=94=20=ED=83=80=EC=9E=85?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD=20service=EC=97=90?= =?UTF-8?q?=EC=84=9C=20mapper=20=EC=A0=9C=EA=B1=B0=20=EB=B0=8F=20repositor?= =?UTF-8?q?y=EC=97=90=EC=84=9C=20mapper=EB=A5=BC=20=EC=9D=B4=EC=9A=A9?= =?UTF-8?q?=ED=95=9C=20=EA=B0=9D=EC=B2=B4=20=EB=B3=80=ED=99=98=20=EC=88=98?= =?UTF-8?q?=ED=96=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../study/bookcafe/dao/BookRepository.java | 3 +- .../study/bookcafe/dao/BorrowRepository.java | 4 +- .../bookcafe/dao/TestBookRepository.java | 26 ++++++++--- .../bookcafe/dao/TestBorrowRepository.java | 43 ++++++++++++------- .../study/bookcafe/service/BookService.java | 3 +- .../bookcafe/service/BookServiceImpl.java | 12 +++--- .../study/bookcafe/service/BorrowService.java | 5 ++- .../bookcafe/service/BorrowServiceImpl.java | 15 +++---- .../study/bookcafe/service/MemberService.java | 3 +- .../bookcafe/service/MemberServiceImpl.java | 18 ++++---- 10 files changed, 78 insertions(+), 54 deletions(-) diff --git a/src/main/java/com/study/bookcafe/dao/BookRepository.java b/src/main/java/com/study/bookcafe/dao/BookRepository.java index 03b647b..6fe945e 100644 --- a/src/main/java/com/study/bookcafe/dao/BookRepository.java +++ b/src/main/java/com/study/bookcafe/dao/BookRepository.java @@ -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 findByIdList(List bookIdList); + List findByIdList(Collection bookIds); } diff --git a/src/main/java/com/study/bookcafe/dao/BorrowRepository.java b/src/main/java/com/study/bookcafe/dao/BorrowRepository.java index f7facdd..02fa92f 100644 --- a/src/main/java/com/study/bookcafe/dao/BorrowRepository.java +++ b/src/main/java/com/study/bookcafe/dao/BorrowRepository.java @@ -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 save(List borrowList); + List save(Collection borrows); } diff --git a/src/main/java/com/study/bookcafe/dao/TestBookRepository.java b/src/main/java/com/study/bookcafe/dao/TestBookRepository.java index 6e60ed6..3f8c7ae 100644 --- a/src/main/java/com/study/bookcafe/dao/TestBookRepository.java +++ b/src/main/java/com/study/bookcafe/dao/TestBookRepository.java @@ -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 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 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 findByIdList(List bookIdList) { - return bookIdList - .stream().filter(id -> books.containsKey(id)) + public List findByIdList(Collection bookIds) { + List bookEntities = bookIds.stream() + .filter(id -> books.containsKey(id)) .map(id -> books.get(id)).toList(); + return bookMapper.toBookList(bookEntities); } } diff --git a/src/main/java/com/study/bookcafe/dao/TestBorrowRepository.java b/src/main/java/com/study/bookcafe/dao/TestBorrowRepository.java index ecf6730..466fedb 100644 --- a/src/main/java/com/study/bookcafe/dao/TestBorrowRepository.java +++ b/src/main/java/com/study/bookcafe/dao/TestBorrowRepository.java @@ -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 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 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 save(List borrowList) { - return borrowList - .stream().filter(borrow -> borrows.containsKey(borrow.getId())) - .map(borrow -> borrows.get(borrow.getId())).toList(); + public List save(Collection borrows) { + List borrowEntities = borrows + .stream().filter(borrow -> this.borrows.containsKey(borrow.getId())) + .map(borrow -> this.borrows.get(borrow.getId())).toList(); + return borrowMapper.toBorrowList(borrowEntities); } } diff --git a/src/main/java/com/study/bookcafe/service/BookService.java b/src/main/java/com/study/bookcafe/service/BookService.java index 039a0aa..17b254f 100644 --- a/src/main/java/com/study/bookcafe/service/BookService.java +++ b/src/main/java/com/study/bookcafe/service/BookService.java @@ -2,6 +2,7 @@ import com.study.bookcafe.domain.Book; +import java.util.Collection; import java.util.List; public interface BookService { @@ -9,5 +10,5 @@ public interface BookService { Book findById(long bookId); // 도서 목록 조회 (id list) - List findByIdList(List bookIdList); + List findByIdList(Collection bookIds); } diff --git a/src/main/java/com/study/bookcafe/service/BookServiceImpl.java b/src/main/java/com/study/bookcafe/service/BookServiceImpl.java index f15847b..5d5d57f 100644 --- a/src/main/java/com/study/bookcafe/service/BookServiceImpl.java +++ b/src/main/java/com/study/bookcafe/service/BookServiceImpl.java @@ -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; } /** @@ -32,11 +30,11 @@ public Book findById(long bookId) { /** * 도서를 ID 목록으로 조회한다. * - * @param bookIdList 도서 ID 목록 + * @param bookIds 도서 ID 목록 * @return 도서 목록 */ @Override - public List findByIdList(List bookIdList) { - return bookRepository.findByIdList(bookIdList); + public List findByIdList(Collection bookIds) { + return bookRepository.findByIdList(bookIds); } } diff --git a/src/main/java/com/study/bookcafe/service/BorrowService.java b/src/main/java/com/study/bookcafe/service/BorrowService.java index ae955d2..04e9441 100644 --- a/src/main/java/com/study/bookcafe/service/BorrowService.java +++ b/src/main/java/com/study/bookcafe/service/BorrowService.java @@ -2,6 +2,7 @@ import com.study.bookcafe.domain.Borrow; +import java.util.Collection; import java.util.List; public interface BorrowService { @@ -9,5 +10,5 @@ public interface BorrowService { Borrow save(Borrow borrow); // 여러 대출 저장 - List save(List borrowList); -} + List save(Collection borrows); +} \ No newline at end of file diff --git a/src/main/java/com/study/bookcafe/service/BorrowServiceImpl.java b/src/main/java/com/study/bookcafe/service/BorrowServiceImpl.java index 73b12b8..f25de28 100644 --- a/src/main/java/com/study/bookcafe/service/BorrowServiceImpl.java +++ b/src/main/java/com/study/bookcafe/service/BorrowServiceImpl.java @@ -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; } /** @@ -31,12 +28,12 @@ public Borrow save(Borrow borrow) { /** * 새로운 여러 대출들을 저장한다. * - * @param borrowList 대출 목록 + * @param borrows 대출 목록 * @return 생성한 대출 정보 목록 */ @Override - public List save(List borrowList) { - List borrowEntityList = borrowMapper.toBorrowEntityList(borrowList); - return borrowMapper.toBorrowList(borrowEntityList); + public List save(Collection borrows) { + return borrowRepository.save(borrows); } + } diff --git a/src/main/java/com/study/bookcafe/service/MemberService.java b/src/main/java/com/study/bookcafe/service/MemberService.java index 9b8fd49..6df6bb5 100644 --- a/src/main/java/com/study/bookcafe/service/MemberService.java +++ b/src/main/java/com/study/bookcafe/service/MemberService.java @@ -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 { @@ -11,5 +12,5 @@ public interface MemberService { Member findById(long memberId); // 도서 대출 - List borrowBook(long memberId, List bookIdList); + List borrowBook(long memberId, Collection bookIds); } diff --git a/src/main/java/com/study/bookcafe/service/MemberServiceImpl.java b/src/main/java/com/study/bookcafe/service/MemberServiceImpl.java index ea843a0..4e37722 100644 --- a/src/main/java/com/study/bookcafe/service/MemberServiceImpl.java +++ b/src/main/java/com/study/bookcafe/service/MemberServiceImpl.java @@ -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 @@ -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; } /** @@ -39,17 +37,17 @@ public Member findById(long memberId) { * 회원이 도서를 대출한다. * * @param memberId 회원 ID - * @param bookIdList 도서 ID 목록 + * @param bookIds 도서 ID 목록 * @return 대출 정보 */ @Override - public List borrowBook(long memberId, List bookIdList) { + public List borrowBook(long memberId, Collection bookIds) { Member member = findById(memberId); - List bookList = bookService.findByIdList(bookIdList); - List borrowList = member.borrowBook(bookList); + List books = bookService.findByIdList(bookIds); + List borrows = member.borrowBook(books); - if(borrowList.isEmpty()) return borrowList; + if(borrows.isEmpty()) return borrows; - return borrowService.save(borrowList); + return borrowService.save(borrows); } } \ No newline at end of file