-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
362 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.study.bookcafe.common; | ||
|
||
import com.google.gson.Gson; | ||
|
||
public class ApiResult { | ||
private boolean isSuccess; | ||
private String message; | ||
private Object data; | ||
|
||
public ApiResult(boolean isSuccess, String message) { | ||
this.isSuccess = isSuccess; | ||
this.message = message; | ||
} | ||
public ApiResult(boolean isSuccess, Object data, String message) { | ||
this.isSuccess = isSuccess; | ||
this.data = data; | ||
this.message = message; | ||
} | ||
|
||
public boolean isSuccess() { | ||
return isSuccess; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new Gson().toJson(this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.study.bookcafe.dao; | ||
|
||
import com.study.bookcafe.dto.BookDTO; | ||
import org.springframework.stereotype.Component; | ||
import java.sql.Date; | ||
import java.time.LocalDate; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Component | ||
public class BookDAO { | ||
|
||
Map<Long, BookDTO> books = new HashMap<>(){{ | ||
put(1L, new BookDTO(9788936433598L, "채식주의자", "한강", "창비", Date.valueOf(LocalDate.of(2007, 10, 30)), 35000)); | ||
put(2L, new BookDTO(9788936433598L, "채식주의자", "한강", "창비", Date.valueOf(LocalDate.of(2018, 4, 25)), 13000)); | ||
}}; | ||
|
||
public BookDTO findById(long bookId) { | ||
// 추후 DB 연결 후 재작성 | ||
return books.get(bookId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.study.bookcafe.dao; | ||
|
||
import com.study.bookcafe.dto.BorrowDTO; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Component | ||
public class BorrowDAO { | ||
Map<Long, BorrowDTO> borrows = new HashMap<>(){{ | ||
put(1L, new BorrowDTO(1, 1, 9788936433598L)); | ||
put(2L, new BorrowDTO(1, 2, 9788936433598L)); | ||
put(3L, null); | ||
}}; | ||
|
||
public BorrowDTO find(long memberId, long bookId, long ISBN) { | ||
// 추후 DB 연결 후 재작성 | ||
return borrows.get(memberId); | ||
} | ||
|
||
public void save(BorrowDTO borrowDTO) { | ||
// 추후 DB 연결 후 작성 | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/study/bookcafe/dao/GeneralMemberDAO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.study.bookcafe.dao; | ||
|
||
import com.study.bookcafe.dto.Level; | ||
import com.study.bookcafe.dto.MemberDTO; | ||
import org.springframework.stereotype.Component; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Component | ||
public class GeneralMemberDAO implements MemberDAO { | ||
|
||
Map<Long, MemberDTO> members = new HashMap<>(){{ | ||
put(1L, new MemberDTO("슈카", Level.BASIC, 0)); | ||
put(2L, new MemberDTO("머스크", Level.WORM, 3)); | ||
put(3L, new MemberDTO("트럼프", Level.LIBRARIAN, 5)); | ||
}}; | ||
|
||
@Override | ||
public MemberDTO findById(long memberId) { | ||
// 추후 DB 연결 후 재작성 | ||
return members.get(memberId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.study.bookcafe.dao; | ||
|
||
import com.study.bookcafe.dto.MemberDTO; | ||
|
||
public interface MemberDAO { | ||
MemberDTO findById(long memberId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.study.bookcafe.dto; | ||
|
||
import java.sql.Date; | ||
|
||
public class BookDTO { | ||
// 도서 한 권에 대한 정보 | ||
private long bookId; // 도서 번호 | ||
|
||
// 같은 도서가 여러 권일 때, 재고나 대출 권수를 상태로 저장해야할지? or 각 도서를 다른 레코드로 저장해야할지? | ||
|
||
// 같은 도서 여러 권을 ISBN으로 묶어 하나의 도서에 대한 정보 | ||
private long ISBN; // 국제표준도서번호 | ||
private String title; // 도서 명 | ||
private String author; // 저자 명 | ||
private String publisher; // 출판사 | ||
private Date publishDate; // 출판일 | ||
private double price; // 도서 가격 | ||
|
||
private boolean isBorrow = true; // 현재 대출 여부 | ||
private int inventory; // 재고 | ||
private int borrowed; // 대출 중인 권수 | ||
private int reservationCount; // 예약 수 | ||
|
||
public BookDTO(long ISBN, String title, String author, String publisher, Date publishDate, double price) { | ||
this.ISBN = ISBN; | ||
this.title = title; | ||
this.author = author; | ||
this.publisher = publisher; | ||
this.publishDate = publishDate; | ||
this.price = price; | ||
} | ||
|
||
public BookDTO(String title, int inventory, int borrowed, int reservationCount) { | ||
this.title = title; | ||
this.inventory = inventory; | ||
this.borrowed = borrowed; | ||
this.reservationCount = reservationCount; | ||
} | ||
|
||
public long getISBN() { | ||
return ISBN; | ||
} | ||
|
||
private int getBorrowed() { | ||
return borrowed; | ||
} | ||
|
||
public boolean canBorrow() { | ||
return isBorrow; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.study.bookcafe.dto; | ||
|
||
import java.sql.Timestamp; | ||
import java.time.LocalDateTime; | ||
|
||
public class BorrowDTO { | ||
private long borrowId; // 대출 ID | ||
private long memberId; // 회원 ID | ||
private long bookId; // 도서 ID | ||
private long ISBN; // 국제표준도서번호 | ||
|
||
private Timestamp borrowDate; // 대출 날짜 | ||
private Timestamp returnDate; // 반납 날짜 | ||
|
||
public BorrowDTO(long memberId, long bookId, long ISBN) { | ||
this.memberId = memberId; | ||
this.bookId = bookId; | ||
this.ISBN = ISBN; | ||
this.borrowDate = Timestamp.valueOf(LocalDateTime.now()); | ||
this.returnDate = Timestamp.valueOf(LocalDateTime.now().plusWeeks(1)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.study.bookcafe.dto; | ||
|
||
public enum Level { | ||
|
||
LIBRARIAN(2, 10,null), // 사서 회원 | ||
WORM(1, 5,null), // 책벌레 회원 | ||
BASIC(0, 3, WORM); // 일반 회원 | ||
|
||
private final int value; // 등급 값 | ||
private final int maximumBorrowCount; // 등급 별 최대 대출 권수 | ||
private final Level next; // 다음 등급 | ||
|
||
Level(int value, int maximumBorrowCount, Level next) { | ||
this.value = value; | ||
this.maximumBorrowCount = maximumBorrowCount; | ||
this.next = next; | ||
} | ||
|
||
/** | ||
* 회원의 대출 가능 권수를 알려준다. | ||
* | ||
* @param borrowCount (현재 회원의 현재대출권수) | ||
* @return 회원의 최대 대출 권수 - 회원의 현재 대출 권수 | ||
*/ | ||
public boolean isBookBorrowCountLeft(int borrowCount) { | ||
return this.maximumBorrowCount - borrowCount > 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.study.bookcafe.dto; | ||
|
||
public class MemberDTO { | ||
private long memberId; // 회원 ID | ||
private String memberName; // 회원 이름 | ||
Level level; // 회원 등급 | ||
int borrowCount; // 현재 대출 권수 | ||
|
||
public MemberDTO(String memberName, Level level, int borrowCount) { | ||
this.memberName = memberName; | ||
this.level = level; | ||
this.borrowCount = borrowCount; | ||
} | ||
|
||
private int getBorrowCount() { | ||
return borrowCount; | ||
} | ||
|
||
/** | ||
* 회원이 대출 가능한 상태인지 알려준다. | ||
* | ||
* @return 현재 회원의 대출 가능 권수가 남았는지 여부 | ||
*/ | ||
public boolean canBorrow() { | ||
return level.isBookBorrowCountLeft(getBorrowCount()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.study.bookcafe.service; | ||
|
||
import com.study.bookcafe.common.ApiResult; | ||
|
||
public interface BorrowService { | ||
ApiResult borrowBook(long memberId, long bookId); | ||
} |
68 changes: 68 additions & 0 deletions
68
src/main/java/com/study/bookcafe/service/BorrowServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.study.bookcafe.service; | ||
|
||
import com.study.bookcafe.common.ApiResult; | ||
import com.study.bookcafe.dao.BookDAO; | ||
import com.study.bookcafe.dao.BorrowDAO; | ||
import com.study.bookcafe.dao.MemberDAO; | ||
import com.study.bookcafe.dto.BookDTO; | ||
import com.study.bookcafe.dto.BorrowDTO; | ||
import com.study.bookcafe.dto.MemberDTO; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class BorrowServiceImpl implements BorrowService { | ||
|
||
private final MemberDAO memberDAO; | ||
private final BookDAO bookDAO; | ||
private final BorrowDAO borrowDAO; | ||
|
||
public BorrowServiceImpl(MemberDAO memberDAO, BookDAO bookDAO, BorrowDAO borrowDAO) { | ||
this.memberDAO = memberDAO; | ||
this.bookDAO = bookDAO; | ||
this.borrowDAO = borrowDAO; | ||
} | ||
|
||
/** | ||
* 회원이 도서를 대출한다. | ||
* | ||
* @param memberId 회원 ID | ||
* @param bookId 도서 ID | ||
* @return 도서 대출 결과(성공여부, 데이터, 결과메세지) | ||
*/ | ||
@Override | ||
public ApiResult borrowBook(long memberId, long bookId) { | ||
|
||
/* | ||
1. 회원이 대출 가능한 상태인지 확인 | ||
2. 회원이 대출하려는 도서가 대출 가능한 상태인지 확인 | ||
3. 회원이 도서를 대출 | ||
4. 회원-도서(도서번호, ISBN) 매핑된 대출 데이터 추가 | ||
동시성 이슈 | ||
트랜잭션 처리 필요 | ||
*/ | ||
|
||
MemberDTO member = memberDAO.findById(memberId); | ||
if(!member.canBorrow()) { | ||
return new ApiResult(false, "현재 회원님의 대출 가능 권수가 없습니다."); | ||
} | ||
|
||
BookDTO book = bookDAO.findById(bookId); | ||
if(!book.canBorrow()) { | ||
return new ApiResult(false, "현재 해당 도서는 모두 대출 중입니다."); | ||
} | ||
|
||
BorrowDTO borrowDTO = borrowDAO.find(memberId, bookId, book.getISBN()); | ||
if(borrowDTO != null) { | ||
return new ApiResult(false, "이미 해당 도서를 대출 중입니다."); | ||
} | ||
|
||
BorrowDTO newBorrowDTO = new BorrowDTO(memberId, bookId, book.getISBN()); | ||
borrowDAO.save(newBorrowDTO); | ||
|
||
return new ApiResult(true, newBorrowDTO,"해당 도서에 대한 대출이 완료되었습니다."); | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.