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

회원의 대출 목록 조회 추가 #12

Merged
merged 2 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

import com.study.bookcafe.domain.borrow.Borrow;
import com.study.bookcafe.domain.borrow.Reservation;
import com.study.bookcafe.domain.borrow.BorrowDetails;

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

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

// 여러 대출 저장
// 도서 대출 저장
Borrow save(Borrow borrow);
List<Borrow> save(Collection<Borrow> borrows);
// 도서 대출 조회
List<BorrowDetails> findBorrows(long memberId);

// 도서 예약 저장
Reservation save(Reservation reservation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.study.bookcafe.domain.borrow.BorrowRepository;
import com.study.bookcafe.domain.borrow.Borrow;
import com.study.bookcafe.domain.borrow.Reservation;
import com.study.bookcafe.domain.borrow.BorrowDetails;
import org.springframework.stereotype.Service;

import java.util.Collection;
Expand All @@ -19,7 +20,7 @@ public BorrowServiceImpl(BorrowRepository borrowRepository) {
/**
* 새로운 대출을 저장한다.
*
* @param borrow 대출 정보
* @param borrow 대출 정보
* @return 생성한 대출 정보
*/
@Override
Expand All @@ -38,6 +39,17 @@ public List<Borrow> save(Collection<Borrow> borrows) {
return borrowRepository.save(borrows);
}

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

/**
* 새로운 예약을 저장한다.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.study.bookcafe.domain.borrow.Borrow;
import com.study.bookcafe.domain.borrow.Reservation;
import com.study.bookcafe.domain.member.Member;
import com.study.bookcafe.domain.borrow.BorrowDetails;

import java.util.Collection;
import java.util.List;
Expand All @@ -14,6 +15,8 @@ public interface MemberService {

// 도서 대출
List<Borrow> borrowBook(long memberId, Collection<Long> bookIds);
// 도서 대출 조회
List<BorrowDetails> findBorrows(long memberId);

// 도서 예약
Reservation reserveBook(long memberId, long bookId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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 org.springframework.stereotype.Service;

import java.util.Collection;
Expand Down Expand Up @@ -56,6 +57,17 @@ public List<Borrow> borrowBook(long memberId, Collection<Long> bookIds) {
return borrowService.save(borrows);
}

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

Choose a reason for hiding this comment

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

DDD 에서 보통 Query 와 Command 는 Service 레벨부터 분리합니다.
쿼리 책임과 커맨드 책임 자체를 분리하기 때문인데요. 한번 관련한 내용들을 찾아보고 리팩토링 해보세요


/**
* 회원이 도서 대출을 예약한다.
* @param memberId 회원 ID
Expand All @@ -81,6 +93,4 @@ public Reservation reserveBook(long memberId, long bookId) {

return borrowService.save(reservation);
}


}
14 changes: 0 additions & 14 deletions src/main/java/com/study/bookcafe/domain/book/Book.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.study.bookcafe.domain.book;

import com.study.bookcafe.interfaces.book.BooksReservationDetails;
import lombok.Builder;
import lombok.Getter;
import java.sql.Date;
import java.util.List;

@Builder
@Getter
Expand All @@ -18,9 +16,6 @@ public class Book {
private double price; // 도서 가격
private Inventory inventory; // 도서 상태 정보 (재고, 대출, 예약)

// 예약내역 목록
private List<BooksReservationDetails> reservations;

/**
* 도서가 대출 가능한 상태인지 확인한다.
*
Expand All @@ -29,13 +24,4 @@ public class Book {
public boolean canBorrow() {
return this.getInventory() != null && this.getInventory().isOnStock();
}

/**
* 도서의 예약 건수를 확인한다.
*
* @return 현재 도서 예약 건수
*/
public int getReservationCount() {
return this.getReservations() != null ? this.getReservations().size() : 0;
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/study/bookcafe/domain/book/Inventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
public class Inventory {
private int stock; // 재고
private int borrowed; // 대출 중인 권수
private int reservationCount; // 예약 건수

public Inventory(int stock) {
this.stock = stock;
this.borrowed = 0;
this.reservationCount = 0;
}

public boolean isOnStock() {
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/study/bookcafe/domain/borrow/BorrowDetails.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.study.bookcafe.domain.borrow;

import com.study.bookcafe.domain.book.Book;
import com.study.bookcafe.domain.member.Member;
import lombok.Builder;
import lombok.Getter;
import java.time.LocalDateTime;

@Builder
@Getter
public class BorrowDetails {
private long id; // 대출 ID
private Member member; // 회원
private Book book; // 도서
Copy link
Collaborator

Choose a reason for hiding this comment

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

MemberBook 객체는 도메인 객체죠?
쿼리 객체라면 도메인 객체 대신 쿼리 모델에 맞는 구성 객체들을 사용하는 방식이 될 것입니다.

private LocalDateTime time; // 대출 시간
private Period period; // 대출 기간
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
public interface BorrowRepository {

Borrow findById(long borrowId);
List<BorrowDetails> findByMemberId(long memberId);
Borrow save(Borrow borrow);
List<Borrow> save(Collection<Borrow> borrows);
Reservation save(Reservation reservation);
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/com/study/bookcafe/domain/member/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.study.bookcafe.domain.book.Book;
import com.study.bookcafe.domain.borrow.Borrow;
import com.study.bookcafe.domain.borrow.Reservation;
import com.study.bookcafe.interfaces.member.MembersReservationDetails;
import lombok.Builder;
import lombok.Getter;
import java.time.LocalDateTime;
Expand All @@ -22,9 +21,6 @@ public class Member {
private LocalDateTime createDate; // 회원 가입 일자
private LocalDateTime updateDate; // 회원 수정 일자

// 예약내역 목록
private List<MembersReservationDetails> reservations;

/**
* 회원이 대출 가능한 상태인지 알려준다.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.study.bookcafe.infrastructure.borrow;

import com.study.bookcafe.domain.book.Book;
import com.study.bookcafe.domain.borrow.Reservation;
import com.study.bookcafe.domain.member.Member;
import com.study.bookcafe.infrastructure.book.BookEntity;
import com.study.bookcafe.domain.borrow.Borrow;
import com.study.bookcafe.domain.borrow.BorrowRepository;
import com.study.bookcafe.infrastructure.member.MemberEntity;
import com.study.bookcafe.domain.borrow.BorrowDetails;
import com.study.bookcafe.interfaces.borrow.BorrowMapper;
import org.springframework.stereotype.Repository;

Expand Down Expand Up @@ -37,11 +40,31 @@ public TestBorrowRepository(BorrowMapper borrowMapper) {
put(3L, null);
}};

Map<Long, BorrowDetails> borrowDetails = new HashMap<>(){{
put(1L, BorrowDetails.builder()
.member(Member.builder().id(1).build())
.book(Book.builder().id(1).ISBN(9788936433598L).build())
.time(LocalDateTime.now())
.build());
put(2L, BorrowDetails.builder()
.member(Member.builder().id(1).build())
.book(Book.builder().id(2).ISBN(9788936433598L).build())
.time(LocalDateTime.now())
.build());
}};

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

@Override
public List<BorrowDetails> findByMemberId(long memberId) {
return borrowDetails.values().stream()
.filter(borrow -> borrow.getMember().getId() == memberId)
.toList();
}

public Borrow save(Borrow borrow) {
BorrowEntity borrowEntity = borrows.get(borrow.getId());
return borrowMapper.toBorrow(borrowEntity);
Expand All @@ -52,7 +75,7 @@ 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);
return borrowMapper.toBorrows(borrowEntities);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ public interface BorrowMapper {

@IterableMapping(qualifiedByName = "BorrowToBorrowEntity")
// List<Borrow> -> List<BorrowEntity>
List<BorrowEntity> toBorrowEntityList(List<Borrow> borrowList);
List<BorrowEntity> toBorrowEntities(List<Borrow> borrows);

@IterableMapping(qualifiedByName = "BorrowEntityToBorrow")
// List<BorrowEntity> -> List<Borrow>
List<Borrow> toBorrowList(List<BorrowEntity> borrowEntityList);
List<Borrow> toBorrows(List<BorrowEntity> borrowEntities);

@IterableMapping(qualifiedByName = "BorrowToBorrowDto")
// List<Borrow> -> List<BorrowDto>
List<BorrowDto> toBorrowDtoList(List<Borrow> borrowList);
List<BorrowDto> toBorrowDtos(List<Borrow> borrows);

@Named("ReservationEntityToReservation")
@Mapping(target = "id", source = "id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public ResponseEntity<MemberDto> member(@PathVariable long id) {
@ResponseBody
public ResponseEntity<List<BorrowDto>> borrowBook(@RequestBody RequestBorrowDto requestBorrowDto) {
List<Borrow> borrows = memberService.borrowBook(requestBorrowDto.getMemberId(), requestBorrowDto.getBookdIdList());
return ResponseEntity.ok(borrowMapper.toBorrowDtoList(borrows));
return ResponseEntity.ok(borrowMapper.toBorrowDtos(borrows));
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.study.bookcafe.borrow;
package com.study.bookcafe.member;

import com.google.gson.Gson;
import com.study.bookcafe.application.member.MemberService;
import com.study.bookcafe.domain.borrow.BorrowDetails;
import com.study.bookcafe.interfaces.common.JsonHelper;
import com.study.bookcafe.domain.borrow.Borrow;
import com.study.bookcafe.interfaces.borrow.BorrowMapper;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -19,8 +19,6 @@ public class BorrowTest {

@Autowired
private MemberService memberService;
@Autowired
private BorrowMapper borrowMapper;

private final Gson gson = JsonHelper.getGson();

Expand All @@ -37,4 +35,15 @@ public void testBorrowBook() {
borrows2.forEach(borrow -> assertThat(Borrow.successBorrow(borrow)).isEqualTo(true));

}

@Test
@DisplayName("회원의 대출 목록을 조회한다.")
public void testFindBorrows() {

long memberId = 1L;

List<BorrowDetails> borrows = memberService.findBorrows(memberId);

assertThat(borrows.size()).isEqualTo(2);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.study.bookcafe.borrow;
package com.study.bookcafe.member;

import com.study.bookcafe.domain.book.Book;
import com.study.bookcafe.domain.book.Inventory;
Expand Down
Loading