Skip to content

Commit

Permalink
CQRS 패턴 적용에 따른 리팩토링
Browse files Browse the repository at this point in the history
- 레이어에서 command와 query로 분리
- 쿼리 객체에 맞는 구성 객체 추가 (MemberView, BookView)
  • Loading branch information
shine-17 committed Jan 7, 2025
1 parent 78f7261 commit f86ab66
Show file tree
Hide file tree
Showing 63 changed files with 665 additions and 370 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.study.bookcafe.application.book;

import com.study.bookcafe.domain.book.Book;
package com.study.bookcafe.application.command.book;

import com.study.bookcafe.domain.command.book.Book;
import java.util.Collection;
import java.util.List;

public interface BookService {

// 도서 조회 (id)
Book findById(long bookId);

// 도서 목록 조회 (id list)
List<Book> findByIds(Collection<Long> bookIds);

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.study.bookcafe.application.book;
package com.study.bookcafe.application.command.book;

import com.study.bookcafe.domain.book.BookRepository;
import com.study.bookcafe.domain.book.Book;
import com.study.bookcafe.domain.command.book.Book;
import com.study.bookcafe.domain.command.book.BookRepository;
import org.springframework.stereotype.Service;

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.study.bookcafe.application.command.borrow;

import com.study.bookcafe.domain.command.borrow.Borrow;
import com.study.bookcafe.domain.command.borrow.Reservation;
import java.util.Collection;

public interface BorrowService {

// 도서 대출 저장
void save(Borrow borrow);
void save(Collection<Borrow> borrows);

// 도서 예약 저장
void save(Reservation reservation);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.study.bookcafe.application.command.borrow;

import com.study.bookcafe.domain.command.borrow.BorrowRepository;
import com.study.bookcafe.domain.command.borrow.Borrow;
import com.study.bookcafe.domain.command.borrow.Reservation;
import org.springframework.stereotype.Service;
import java.util.Collection;

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

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

/**
* 새로운 대출을 저장한다.
*
* @param borrow 대출 정보
* @return 생성한 대출 정보
*/
@Override
public void save(Borrow borrow) {
borrowRepository.save(borrow);
}

/**
* 새로운 여러 대출들을 저장한다.
*
* @param borrows 대출 목록
* @return 생성한 대출 정보 목록
*/
@Override
public void save(Collection<Borrow> borrows) {
borrowRepository.save(borrows);
}

/**
* 새로운 예약을 저장한다.
*
* @param reservation 예악 정보
* @return 생성한 예약 정보
*/
@Override
public void save(Reservation reservation) {
borrowRepository.save(reservation);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.study.bookcafe.application.command.member;

import com.study.bookcafe.domain.command.member.Member;
import java.util.Collection;

public interface MemberService {

// 회원 조회 (id)
Member findById(long memberId);

// 도서 대출
void borrowBook(long memberId, Collection<Long> bookIds);

// 도서 예약
void reserveBook(long memberId, long bookId);
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.study.bookcafe.application.member;
package com.study.bookcafe.application.command.member;

import com.study.bookcafe.application.book.BookService;
import com.study.bookcafe.application.borrow.BorrowService;
import com.study.bookcafe.application.command.book.BookService;
import com.study.bookcafe.application.command.borrow.BorrowService;
import com.study.bookcafe.application.exception.BorrowableException;
import com.study.bookcafe.application.exception.NonBorrowableMemberException;
import com.study.bookcafe.domain.borrow.Reservation;
import com.study.bookcafe.domain.member.MemberRepository;
import com.study.bookcafe.domain.book.Book;
import com.study.bookcafe.domain.borrow.Borrow;
import com.study.bookcafe.domain.member.Member;
import com.study.bookcafe.domain.borrow.BorrowDetails;
import com.study.bookcafe.domain.command.book.Book;
import com.study.bookcafe.domain.command.borrow.Borrow;
import com.study.bookcafe.domain.command.borrow.Reservation;
import com.study.bookcafe.domain.command.member.Member;
import com.study.bookcafe.domain.command.member.MemberRepository;
import com.study.bookcafe.domain.query.borrow.BorrowDetails;
import org.springframework.stereotype.Service;

import java.util.Collection;
Expand Down Expand Up @@ -44,38 +44,25 @@ public Member findById(long memberId) {
*
* @param memberId 회원 ID
* @param bookIds 도서 ID 목록
* @return 대출
*/
@Override
public List<Borrow> borrowBook(long memberId, Collection<Long> bookIds) {
public void borrowBook(long memberId, Collection<Long> bookIds) {
Member member = findById(memberId);
List<Book> books = bookService.findByIds(bookIds);
List<Borrow> borrows = member.borrowBook(books);

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

return borrowService.save(borrows);
}

/**
* 회원의 대출 목록을 조회한다.
*
* @param memberId 회원 ID
* @return 대출 목록
*/
@Override
public List<BorrowDetails> findBorrows(long memberId) {
return borrowService.findBorrows(memberId);
borrowService.save(borrows);
}

/**
* 회원이 도서 대출을 예약한다.
* @param memberId 회원 ID
* @param bookId 도서 ID
* @return 예약
*/
@Override
public Reservation reserveBook(long memberId, long bookId) {
public void reserveBook(long memberId, long bookId) {
Member member = findById(memberId);
Book book = bookService.findById(bookId);

Expand All @@ -91,6 +78,6 @@ public Reservation reserveBook(long memberId, long bookId) {

Reservation reservation = member.reserveBook(book);

return borrowService.save(reservation);
borrowService.save(reservation);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.study.bookcafe.application.query.book;

import com.study.bookcafe.domain.query.book.BookView;

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

public interface BookQueryService {

// 도서 조회 (id)
BookView findById(long bookId);

// 도서 목록 조회 (id list)
List<BookView> findByIds(Collection<Long> bookIds);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.study.bookcafe.application.query.book;

import com.study.bookcafe.domain.query.book.BookQueryRepository;
import com.study.bookcafe.domain.query.book.BookView;
import org.springframework.stereotype.Service;
import java.util.Collection;
import java.util.List;

@Service
public class BookQueryServiceImpl implements BookQueryService {

private final BookQueryRepository bookQueryRepository;

public BookQueryServiceImpl(BookQueryRepository bookQueryRepository) {
this.bookQueryRepository = bookQueryRepository;
}

/**
* 도서를 ID로 조회한다.
*
* @param bookId 도서 ID
* @return 도서
*/
@Override
public BookView findById(long bookId) {
return bookQueryRepository.findById(bookId);
}

/**
* 도서를 ID 목록으로 조회한다.
*
* @param bookIds 도서 ID 목록
* @return 도서 목록
*/
@Override
public List<BookView> findByIds(Collection<Long> bookIds) {
return bookQueryRepository.findByIds(bookIds);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.study.bookcafe.application.query.borrow;

import com.study.bookcafe.domain.query.borrow.BorrowDetails;
import java.util.List;

public interface BorrowQueryService {

// 도서 대출 조회
List<BorrowDetails> findBorrows(long memberId);

}
Loading

0 comments on commit f86ab66

Please sign in to comment.